Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial variant schema #461

Open
frenzzy opened this issue Feb 28, 2024 · 1 comment
Open

Partial variant schema #461

frenzzy opened this issue Feb 28, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@frenzzy
Copy link

frenzzy commented Feb 28, 2024

Hello, thank you for Valibot!

Is there a way to extract a partial schema from a variant type like this one:

import * as v from 'valibot';

const VariantScheme = v.variant('type', [
  v.object({
    type: v.literal('foo'),
    data: v.string(),
  }),
  v.object({
    type: v.literal('bar'),
    data: v.number(),
  }),
  v.object({
    type: v.literal('baz'),
    data: v.null_(),
  }),
]);

To get a schema of specific fields without repeating yourself:

const TypeScheme = v.union([v.literal('foo'), v.literal('bar'), v.literal('baz')]);
const DataScheme = v.union([v.string(), v.number(), v.null_()]);

Unfortunately, pick and omit do not accept VariantSchema as an input.

If it is already possible, please add an example to the docs: https://valibot.dev/guides/unions/

@fabian-hiller
Copy link
Owner

This is not currently possible and I am not sure if I want to implement it. I will probably take a look at it after we release v1 in about one or two months.

@fabian-hiller fabian-hiller self-assigned this Feb 28, 2024
@fabian-hiller fabian-hiller added the enhancement New feature or request label Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants