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

Add support for Zod .refine and .superRefine #1319

Merged
merged 4 commits into from
May 24, 2024
Merged
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
43 changes: 43 additions & 0 deletions packages/uniforms-bridge-zod/__tests__/ZodBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
undefined as undefined_,
union,
unknown,
ZodIssueCode,
} from 'zod';

describe('ZodBridge', () => {
Expand Down Expand Up @@ -70,6 +71,42 @@ describe('ZodBridge', () => {
expect(bridge.getError('a.b', error)).toBe(null);
expect(bridge.getError('a.b.c', error)).toBe(issues?.[0]);
});

it('works with refined schema', () => {
const errorMessage = 'Different values';

const schema = object({
a: string(),
b: string(),
}).refine(({ a, b }) => a === b, {
message: errorMessage,
path: ['b'],
});

const bridge = new ZodBridge({ schema });
const error = bridge.getValidator()({ a: 'a', b: 'b' });
expect(error?.issues?.[0]?.message).toBe(errorMessage);
});

it('works with super refined schema', () => {
const errorMessage = 'Different values';

const schema = object({
a: string(),
b: string(),
}).superRefine((val, ctx) => {
if (val.a !== val.b) {
ctx.addIssue({
code: ZodIssueCode.custom,
message: errorMessage,
});
}
});

const bridge = new ZodBridge({ schema });
const error = bridge.getValidator()({ a: 'a', b: 'b' });
expect(error?.issues?.[0]?.message).toBe(errorMessage);
});
});

describe('#getErrorMessage', () => {
Expand Down Expand Up @@ -197,6 +234,12 @@ describe('ZodBridge', () => {
expect(bridge.getField('a')).toBe(schema.shape.a);
expect(bridge.getField('a.b')).toBe(schema.shape.a.unwrap().shape.b);
});

it('works with ZodEffects', () => {
const schema = object({}).refine(data => data);
const bridge = new ZodBridge({ schema });
expect(bridge.getField('')).toBe(schema._def.schema);
});
});

describe('#getInitialValue', () => {
Expand Down
10 changes: 8 additions & 2 deletions packages/uniforms-bridge-zod/src/ZodBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ZodBoolean,
ZodDate,
ZodDefault,
ZodEffects,
ZodEnum,
ZodError,
ZodNativeEnum,
Expand Down Expand Up @@ -37,14 +38,14 @@ type Option<Value> = {
};

export default class ZodBridge<T extends ZodRawShape> extends Bridge {
schema: ZodObject<T>;
schema: ZodObject<T> | ZodEffects<ZodObject<T>>;
provideDefaultLabelFromFieldName: boolean;

constructor({
schema,
provideDefaultLabelFromFieldName = true,
}: {
schema: ZodObject<T>;
schema: ZodObject<T> | ZodEffects<ZodObject<T>>;
provideDefaultLabelFromFieldName?: boolean;
}) {
super();
Expand Down Expand Up @@ -87,6 +88,11 @@ export default class ZodBridge<T extends ZodRawShape> extends Bridge {

getField(name: string) {
let field: ZodType = this.schema;

if (this.schema instanceof ZodEffects) {
field = this.schema._def.schema;
}

for (const key of joinName(null, name)) {
if (field instanceof ZodDefault) {
field = field.removeDefault();
Expand Down
Loading