Skip to content

Partial variant schema #461

@frenzzy

Description

@frenzzy

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/

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions