From 028672a9b60cc53fe4b532bb0483b5e5aeb97bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Po=C5=9Bpiech?= Date: Sat, 20 Apr 2024 16:35:16 +0200 Subject: [PATCH 1/4] added zod type - uniforms for props --- packages/uniforms-bridge-zod/src/ZodBridge.ts | 8 +++++++ packages/uniforms-bridge-zod/src/register.ts | 22 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/uniforms-bridge-zod/src/ZodBridge.ts b/packages/uniforms-bridge-zod/src/ZodBridge.ts index 6c7a241c2..a65a60549 100644 --- a/packages/uniforms-bridge-zod/src/ZodBridge.ts +++ b/packages/uniforms-bridge-zod/src/ZodBridge.ts @@ -155,6 +155,14 @@ export default class ZodBridge 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; diff --git a/packages/uniforms-bridge-zod/src/register.ts b/packages/uniforms-bridge-zod/src/register.ts index fe2af507a..878e39382 100644 --- a/packages/uniforms-bridge-zod/src/register.ts +++ b/packages/uniforms-bridge-zod/src/register.ts @@ -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' { @@ -9,3 +10,22 @@ declare module 'uniforms' { } filterDOMProps.register('minCount', 'maxCount'); + +declare module 'zod' { + interface ZodType { + uniforms(uniforms: UnknownObject | ConnectedField): this; + } + + interface ZodTypeDef { + uniforms: UnknownObject | ConnectedField; + } +} + +z.ZodType.prototype.uniforms = function extend(uniforms) { + const This = (this as any).constructor; + + return new This({ + ...this._def, + uniforms, + }); +}; From 6e04e35445dc1bd97908dbbe904a826e381fb87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Po=C5=9Bpiech?= Date: Mon, 22 Apr 2024 19:02:13 +0200 Subject: [PATCH 2/4] added tests --- .../__tests__/ZodBridge.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/uniforms-bridge-zod/__tests__/ZodBridge.ts b/packages/uniforms-bridge-zod/__tests__/ZodBridge.ts index 9dd1dc797..561525e0d 100644 --- a/packages/uniforms-bridge-zod/__tests__/ZodBridge.ts +++ b/packages/uniforms-bridge-zod/__tests__/ZodBridge.ts @@ -1,3 +1,4 @@ +import { connectField } from 'uniforms'; import { ZodBridge } from 'uniforms-bridge-zod'; import { any, @@ -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', () => { From 99dd27052b908b1e1aaafa50b5b4e2320c72df49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Po=C5=9Bpiech?= Date: Fri, 3 May 2024 07:39:11 +0200 Subject: [PATCH 3/4] fixed ts errors --- packages/uniforms-bridge-zod/src/ZodBridge.ts | 2 +- packages/uniforms-bridge-zod/src/register.ts | 13 +++---------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/packages/uniforms-bridge-zod/src/ZodBridge.ts b/packages/uniforms-bridge-zod/src/ZodBridge.ts index a65a60549..c6a36667a 100644 --- a/packages/uniforms-bridge-zod/src/ZodBridge.ts +++ b/packages/uniforms-bridge-zod/src/ZodBridge.ts @@ -156,7 +156,7 @@ export default class ZodBridge extends Bridge { let field = this.getField(name); - const uniforms = field._def.uniforms; + const uniforms = field._uniforms; if (typeof uniforms === 'function') { props.component = uniforms; } else { diff --git a/packages/uniforms-bridge-zod/src/register.ts b/packages/uniforms-bridge-zod/src/register.ts index 878e39382..0c35ea7fc 100644 --- a/packages/uniforms-bridge-zod/src/register.ts +++ b/packages/uniforms-bridge-zod/src/register.ts @@ -14,18 +14,11 @@ filterDOMProps.register('minCount', 'maxCount'); declare module 'zod' { interface ZodType { uniforms(uniforms: UnknownObject | ConnectedField): this; - } - - interface ZodTypeDef { - uniforms: UnknownObject | ConnectedField; + _uniforms: UnknownObject | ConnectedField; } } z.ZodType.prototype.uniforms = function extend(uniforms) { - const This = (this as any).constructor; - - return new This({ - ...this._def, - uniforms, - }); + this._uniforms = uniforms; + return this; }; From da8fd1db061087da3ffb617f250988deac203b36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Po=C5=9Bpiech?= Date: Fri, 3 May 2024 07:55:04 +0200 Subject: [PATCH 4/4] fixed ts errors --- packages/uniforms-bridge-zod/src/register.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/uniforms-bridge-zod/src/register.ts b/packages/uniforms-bridge-zod/src/register.ts index 0c35ea7fc..fae893e9c 100644 --- a/packages/uniforms-bridge-zod/src/register.ts +++ b/packages/uniforms-bridge-zod/src/register.ts @@ -1,5 +1,5 @@ import { ConnectedField, filterDOMProps, UnknownObject } from 'uniforms'; -import { z } from 'zod'; +import { z, ZodTypeAny } from 'zod'; // There's no possibility to retrieve them at runtime. declare module 'uniforms' { @@ -13,7 +13,7 @@ filterDOMProps.register('minCount', 'maxCount'); declare module 'zod' { interface ZodType { - uniforms(uniforms: UnknownObject | ConnectedField): this; + uniforms(uniforms: UnknownObject | ConnectedField): ZodTypeAny; _uniforms: UnknownObject | ConnectedField; } }