ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).
https://docs.superstructjs.org
MIT License
6.97k stars 223 forks source link

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

Closed Mr-M1M3 closed 2 years ago

Mr-M1M3 commented 2 years ago

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.

arturmuller commented 2 years ago

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!

Mr-M1M3 commented 2 years ago

Thanks @arturmuller. I get it now!!!