Skip to content

Commit

Permalink
updated schema type in the ZodBridge
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrpospiech committed Nov 18, 2024
1 parent 712f578 commit b36ee3b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
43 changes: 29 additions & 14 deletions packages/uniforms-bridge-zod/__tests__/ZodBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
enum as enum_,
function as function_,
instanceof as instanceof_,
infer as infer_,
intersection,
lazy,
literal,
Expand Down Expand Up @@ -43,7 +44,8 @@ describe('ZodBridge', () => {

it('works with simple types', () => {
const schema = object({ a: string(), b: number() });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({});
const issues = error?.issues;
expect(bridge.getError('a', error)).toBe(issues?.[0]);
Expand All @@ -52,7 +54,8 @@ describe('ZodBridge', () => {

it('works with arrays', () => {
const schema = object({ a: array(array(string())) });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: [['x', 'y', 0], [1]] });
const issues = error?.issues;
expect(bridge.getError('a', error)).toBe(null);
Expand All @@ -65,7 +68,8 @@ describe('ZodBridge', () => {

it('works with nested objects', () => {
const schema = object({ a: object({ b: object({ c: string() }) }) });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: { b: { c: 1 } } });
const issues = error?.issues;
expect(bridge.getError('a', error)).toBe(null);
Expand All @@ -84,7 +88,8 @@ describe('ZodBridge', () => {
path: ['b'],
});

const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: 'a', b: 'b' });
expect(error?.issues?.[0]?.message).toBe(errorMessage);
});
Expand All @@ -106,7 +111,8 @@ describe('ZodBridge', () => {
path: ['b'],
});

const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: 1, b: 2 });
expect(error?.issues?.[0]?.message).toBe(firstErrorMessage);
expect(error?.issues?.[1]?.message).toBe(secondErrorMessage);
Expand All @@ -127,7 +133,8 @@ describe('ZodBridge', () => {
}
});

const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: 'a', b: 'b' });
expect(error?.issues?.[0]?.message).toBe(errorMessage);
});
Expand Down Expand Up @@ -157,7 +164,8 @@ describe('ZodBridge', () => {
}
});

const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: 1, b: 2 });
expect(error?.issues?.[0]?.message).toBe(firstErrorMessage);
expect(error?.issues?.[1]?.message).toBe(secondErrorMessage);
Expand All @@ -174,7 +182,8 @@ describe('ZodBridge', () => {

it('works with simple types', () => {
const schema = object({ a: string(), b: number() });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({});
const issues = error?.issues;
expect(bridge.getErrorMessage('a', error)).toBe(issues?.[0].message);
Expand All @@ -183,7 +192,8 @@ describe('ZodBridge', () => {

it('works with arrays', () => {
const schema = object({ a: array(array(string())) });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: [['x', 'y', 0], [1]] });
const issues = error?.issues;
expect(bridge.getErrorMessage('a', error)).toBe('');
Expand All @@ -196,7 +206,8 @@ describe('ZodBridge', () => {

it('works with nested objects', () => {
const schema = object({ a: object({ b: object({ c: string() }) }) });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: { b: { c: 1 } } });
const issues = error?.issues;
expect(bridge.getErrorMessage('a', error)).toBe('');
Expand All @@ -221,15 +232,17 @@ describe('ZodBridge', () => {

it('works with simple types', () => {
const schema = object({ a: string(), b: number() });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({});
const messages = ['A: Required', 'B: Required'];
expect(bridge.getErrorMessages(error)).toEqual(messages);
});

it('works with arrays', () => {
const schema = object({ a: array(array(string())) });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: [['x', 'y', 0], [1]] });
const messages = [
'A (0, 2): Expected string, received number',
Expand All @@ -240,7 +253,8 @@ describe('ZodBridge', () => {

it('works with nested objects', () => {
const schema = object({ a: object({ b: object({ c: string() }) }) });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: { b: { c: 1 } } });
const messages = ['C: Expected string, received number'];
expect(bridge.getErrorMessages(error)).toEqual(messages);
Expand Down Expand Up @@ -751,7 +765,8 @@ describe('ZodBridge', () => {
describe('#getValidator', () => {
it('return a function', () => {
const schema = object({});
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const validator = bridge.getValidator();
expect(validator).toEqual(expect.any(Function));
expect(validator({})).toEqual(null);
Expand Down
19 changes: 3 additions & 16 deletions packages/uniforms-bridge-zod/src/ZodBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
ZodNumberDef,
ZodObject,
ZodOptional,
ZodRawShape,
ZodString,
ZodType,
} from 'zod';
Expand Down Expand Up @@ -55,27 +54,15 @@ type Option<Value> = {
value: Value;
};

type BuildTuple<L extends number, T extends any[] = []> = T['length'] extends L
? T
: BuildTuple<L, [...T, any]>;

type Decrement<N extends number> =
BuildTuple<N> extends [any, ...infer R] ? R['length'] : never;

type NestedZodEffect<
T extends ZodObject<any>,
Depth extends number = 20,
> = Depth extends 0 ? T : ZodEffects<T | NestedZodEffect<T, Decrement<Depth>>>;

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

constructor({
schema,
provideDefaultLabelFromFieldName = true,
}: {
schema: ZodObject<T> | NestedZodEffect<ZodObject<T>>;
schema: ZodType<T>;
provideDefaultLabelFromFieldName?: boolean;
}) {
super();
Expand Down

0 comments on commit b36ee3b

Please sign in to comment.