Skip to content

Commit

Permalink
added zod type - uniforms for props
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrpospiech committed Apr 20, 2024
1 parent 49715f4 commit 028672a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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._def.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
22 changes: 21 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 } from 'zod';

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

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

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

interface ZodTypeDef {
uniforms: UnknownObject | ConnectedField<any>;
}
}

z.ZodType.prototype.uniforms = function extend(uniforms) {
const This = (this as any).constructor;

return new This({
...this._def,
uniforms,
});
};

0 comments on commit 028672a

Please sign in to comment.