ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).

Home Page:https://docs.superstructjs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What does first parameter of `define()` function do?

Mr-M1M3 opened this issue · comments

Ho there, while reading docs, I came to know that we can define our own custom types using define() function. However, I couldn't find any explanation of the parameters it takes. It would be a great help if you tell me about the parameter.

Hi @Mr-M1M3 — if you're still curious — the first param acts as a human-readable label for the Struct you are creating. You can see it in error messages.

For example:

import isUuid from 'is-uuid'

const uuid = define('uuid', (value) => isUuid.v4(value)); // The label is set to `uuid`
const [error, value] = validate("Z2C46V", uuid);

console.log(error.message) // Prints: 'Expected a value of type `uuid`, but received: `"Z2C46V"`'

Hope this helps!

Thanks @arturmuller. I get it now!!!