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

Is there a way to get the metadata as type? #841

Closed JustGreg closed 1 month ago

JustGreg commented 2 months ago

I want to do something like this:

const UserSchema = v.pipe(v.object({
    email: v.pipe(v.string(), v.email()),
    password: v.pipe(v.string(), v.minLength(8)),
  }),
  v.title('user')
);

type UserTitle = v.InferMeta<typeof Schema, 'title'>
// UserTitle = 'user'
fabian-hiller commented 2 months ago

We do not have a utility type for this yet, but this should work. Check it out in our playground.

import * as v from 'valibot';

const UserSchema = v.pipe(
  v.object({
    email: v.pipe(v.string(), v.email()),
    password: v.pipe(v.string(), v.minLength(8)),
  }),
  v.title('user'),
);

type UserTitle = Extract<
  (typeof UserSchema)['pipe'][number],
  { type: 'title' }
>['title'];