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

Improve Zod bridge - passing props #1321

Merged
merged 5 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
24 changes: 24 additions & 0 deletions packages/uniforms-bridge-zod/__tests__/ZodBridge.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { connectField } from 'uniforms';
import { ZodBridge } from 'uniforms-bridge-zod';
import {
any,
Expand Down Expand Up @@ -447,6 +448,29 @@ describe('ZodBridge', () => {
const bridge = new ZodBridge({ schema });
expect(bridge.getProps('a')).toEqual({ label: 'A', required: true });
});

it('works with uniforms props', () => {
const schema = object({ a: string().uniforms({ type: 'password' }) });
const bridge = new ZodBridge({ schema });
expect(bridge.getProps('a')).toEqual({
label: 'A',
required: true,
type: 'password',
});
});

it('works with uniforms props (component)', () => {
const field = jest.fn(() => null);
const Field = connectField(field);

const schema = object({ a: string().uniforms(Field) });
const bridge = new ZodBridge({ schema });
expect(bridge.getProps('a')).toEqual({
component: Field,
label: 'A',
required: true,
});
});
});

describe('#getSubfields', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/uniforms-bridge-zod/src/ZodBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ export default class ZodBridge<T extends ZodRawShape> extends Bridge {
};

let field = this.getField(name);

const uniforms = field._uniforms;
if (typeof uniforms === 'function') {
props.component = uniforms;
} else {
Object.assign(props, uniforms);
}

if (field instanceof ZodDefault) {
field = field.removeDefault();
props.required = false;
Expand Down
15 changes: 14 additions & 1 deletion packages/uniforms-bridge-zod/src/register.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { filterDOMProps } from 'uniforms';
import { ConnectedField, filterDOMProps, UnknownObject } from 'uniforms';
import { z, ZodTypeAny } from 'zod';

// There's no possibility to retrieve them at runtime.
declare module 'uniforms' {
Expand All @@ -9,3 +10,15 @@ declare module 'uniforms' {
}

filterDOMProps.register('minCount', 'maxCount');

declare module 'zod' {
interface ZodType {
uniforms(uniforms: UnknownObject | ConnectedField<any>): ZodTypeAny;
_uniforms: UnknownObject | ConnectedField<any>;
}
}

z.ZodType.prototype.uniforms = function extend(uniforms) {
this._uniforms = uniforms;
return this;
};
Loading