-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Open
Labels
p2-nice-to-haveNot breaking anything but nice to have (priority)Not breaking anything but nice to have (priority)
Description
Clear and concise description of the problem
I created the custom matcher expect-match-schema for schema validation based on a Standard Schema like Zod, Valibot, ArkType, etc.
test('should pass for valid data', () => {
const schema = z.object({
name: z.string(),
email: z.email(),
});
const validData = { name: 'John', email: '[email protected]' };
expect(validData).toMatchSchema(schema);
});Since Standard Schema just specifies an interface without depending on the actual schema libraries like Zod, the implementation is quite lightweight.
It has received good feedback on Twitter and I thought this might be a good feature to be natively supported by Vitest: https://x.com/zirkelc_/status/1958534349319553418
Suggested solution
See PR #8527
Three options have been implemented for further discussion:
- custom equality tester for expect.toEqual()
- custom matcher expect.toEqualSchema()
- asymmetric matcher expect.schemaMatching()
const schema = z.object({
name: z.string(),
email: z.email(),
});
const validData = { name: 'John', email: '[email protected]' };
// custom equality test using toEqual
expect(validData).toEqual(schema);
expect({ email: "[email protected]" }).toEqual({ email: z.email() });
// custom matcher using toEqualSchema
expect(validData).toEqualSchema(schema);
expect({ email: "[email protected]" }).toEqualSchema({ email: z.email() });
// asymmetric matcher
expect(validData).toEqual(expect.schemaMatching(schema));
expect({ email: "[email protected]" }).toEqual(expect.schemaMatching({ email: z.email() }));Alternative
No response
Additional context
If approved, I'd like to submit a PR for this feature.
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
samhh, dubzzz, philwolstenholme, louiss0, xcfox and 4 more
Metadata
Metadata
Assignees
Labels
p2-nice-to-haveNot breaking anything but nice to have (priority)Not breaking anything but nice to have (priority)
Type
Projects
Status
Discussing