fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
6.33k stars 204 forks source link

True open-ended metadata? #858

Closed jhanstra closed 1 month ago

jhanstra commented 1 month ago

Hi there! I've been really loving Valibot but i can't figure out the best way to add unique metadata to a schema. I have at least one use case for this and there are probably many others - mine is that I would like to reuse the schema to determine which React component type to render for an input element, so I want to pass down a component: 'textarea' or multiline: true property. My understanding of metadata in Valibot as far as I can tell is that the only use case that was imagined was a description for the field. And even the API is just v.description(), not v.metadata() or similar. I'm wondering if you've had other requests for a true metadata function that anything can be passed into?

I did see that you can in fact pass an object into the v.description() and it tacks that whole object on, which is a workaround that works. BUT, when I try to use the JSON schema generator in the other package, that breaks since now I have an object as my description instead of just a string. Thoughts?

Again thanks so much for all your work on this I'm a big fan!

fabian-hiller commented 1 month ago

Thanks for your message! We added a v.metadata({ ... }) action in v0.42.0. Feel free to give me feedback about it. https://valibot.dev/api/metadata/

jhanstra commented 1 month ago

Oh shoot! Sorry for not checking the most recent docs! Wow y'all move fast, I was on 0.39.0. Thank you so much, this looks like what I want - I'll update to that 😄

imolorhe commented 3 weeks ago

@fabian-hiller it's not clear how to access the metadata on the schema

fabian-hiller commented 3 weeks ago

Here is an example. See this playground.

import * as v from 'valibot';

const Schema = v.pipe(v.string(), v.description('This is a test'));

const description = Schema.pipe.find(
  (item): item is v.DescriptionAction<string, 'This is a test'> =>
    item.type === 'description',
)?.description;