Skip to content

fix: valibot variants not inferring required correctly #5031

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-eels-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vee-validate/valibot": patch
---

fix: valibot variants not inferring required correctly
17 changes: 17 additions & 0 deletions packages/valibot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
Config,
IntersectSchema,
IntersectIssue,
VariantSchema,
VariantOptions,
VariantIssue,
} from 'valibot';
import { isIndex, isObject, merge, normalizeFormPath } from '../../shared';

Expand Down Expand Up @@ -140,6 +143,10 @@
return schema.options.map(o => getSchemaForPath(path, o)).find(Boolean) ?? null;
}

if (isVariantSchema(schema)) {
return schema.options.map(o => getSchemaForPath(path, o)).find(Boolean) ?? null;
}

if (!isObjectSchema(schema)) {
return null;
}
Expand All @@ -161,6 +168,10 @@
currentSchema = currentSchema.options.find(o => isObjectSchema(o) && o.entries[p]) ?? currentSchema;
}

if (isVariantSchema(currentSchema)) {
currentSchema = currentSchema.options.find(o => isObjectSchema(o) && o.entries[p]) ?? currentSchema;

Check warning on line 172 in packages/valibot/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/valibot/src/index.ts#L172

Added line #L172 was not covered by tests
}

if (isObjectSchema(currentSchema)) {
currentSchema = currentSchema.entries[p] || null;
continue;
Expand Down Expand Up @@ -208,3 +219,9 @@
> {
return schema.type === 'intersect';
}

function isVariantSchema(
schema: BaseSchema<unknown, unknown, BaseIssue<unknown>> | BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
): schema is VariantSchema<string, VariantOptions<string>, ErrorMessage<VariantIssue> | undefined> {
return schema.type === 'variant';
}
178 changes: 178 additions & 0 deletions packages/valibot/tests/valibot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,184 @@ test('reports required state for schema intersections with nested fields', async
);
});

test('reports required state for variant schemas', async () => {
const metaSpy = vi.fn();
mountWithHoc({
setup() {
const ScheduledDateSchema = v.object({
dateType: v.literal('schedule'),
date: v.pipe(v.string(), v.isoDate()),
});

const ImmediateDateSchema = v.object({
dateType: v.literal('immediate'),
});

const PaymentDetailsFormSchema = v.variant('dateType', [ImmediateDateSchema, ScheduledDateSchema]);

const schema = toTypedSchema(PaymentDetailsFormSchema);

useForm({
validationSchema: schema,
});

const { meta: dateType } = useField('dateType');
const { meta: date } = useField('date');

metaSpy({
dateType: dateType.required,
date: date.required,
});

return {
schema,
};
},
template: `<div></div>`,
});

await flushPromises();
await expect(metaSpy).toHaveBeenLastCalledWith(
expect.objectContaining({
dateType: true,
date: true,
}),
);
});

test('reports required state for variant schemas with nested fields', async () => {
const metaSpy = vi.fn();
mountWithHoc({
setup() {
const ComplexVariantSchema = v.variant('kind', [
v.variant('type', [
v.object({
kind: v.literal('fruit'),
type: v.literal('apple'),
item: v.object({
name: v.string(),
price: v.number(),
}),
}),
v.object({
kind: v.literal('fruit'),
type: v.literal('banana'),
item: v.object({
name: v.string(),
price: v.number(),
}),
}),
]),
v.variant('type', [
v.object({
kind: v.literal('vegetable'),
type: v.literal('carrot'),
item: v.object({
name: v.string(),
price: v.number(),
}),
}),
v.object({
kind: v.literal('vegetable'),
type: v.literal('tomato'),
item: v.object({
name: v.string(),
price: v.number(),
}),
}),
]),
]);

const schema = toTypedSchema(ComplexVariantSchema);

useForm({
validationSchema: schema,
});

const { meta: kind } = useField('kind');
const { meta: type } = useField('type');
const { meta: item } = useField('item');

metaSpy({
kind: kind.required,
type: type.required,
item: item.required,
});

return {
schema,
};
},
template: `<div></div>`,
});

await flushPromises();
await expect(metaSpy).toHaveBeenLastCalledWith(
expect.objectContaining({
kind: true,
type: true,
item: true,
}),
);
});

test('reports required state for variant schemas when combined with intersections', async () => {
const metaSpy = vi.fn();
mountWithHoc({
setup() {
const ScheduledDateSchema = v.object({
dateType: v.literal('schedule'),
date: v.pipe(v.string(), v.isoDate()),
});

const ImmediateDateSchema = v.object({
dateType: v.literal('immediate'),
});

const PaymentDetailsFormSchema = v.intersect([
v.variant('dateType', [ImmediateDateSchema, ScheduledDateSchema]),
v.object({
amount: v.pipe(v.number(), v.minValue(1)),
note: v.optional(v.string()),
}),
]);

const schema = toTypedSchema(PaymentDetailsFormSchema);

useForm({
validationSchema: schema,
});

const { meta: dateType } = useField('dateType');
const { meta: date } = useField('date');
const { meta: amount } = useField('amount');
const { meta: note } = useField('note');

metaSpy({
dateType: dateType.required,
date: date.required,
amount: amount.required,
note: note.required,
});

return {
schema,
};
},
template: `<div></div>`,
});

await flushPromises();
await expect(metaSpy).toHaveBeenLastCalledWith(
expect.objectContaining({
dateType: true,
date: true,
amount: true,
note: false,
}),
);
});

test('allows passing valibot config', async () => {
let errors!: Ref<string[]>;
const wrapper = mountWithHoc({
Expand Down