From 514f4eea3cdd2f790a7151cf60edf142095a68af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Felipe=20Schulle?= Date: Fri, 28 Nov 2025 15:15:11 -0300 Subject: [PATCH 1/5] Add optional/nullable/or support to Zod resolver Add unit test covering optional and nullable handling and update README with a Zod resolver example --- .changeset/late-donuts-count.md | 5 +++++ README.md | 7 +++++-- src/resolvers/zod.spec.ts | 31 +++++++++++++++++++++++++++++++ src/resolvers/zod.ts | 21 +++++++++++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 .changeset/late-donuts-count.md diff --git a/.changeset/late-donuts-count.md b/.changeset/late-donuts-count.md new file mode 100644 index 0000000..d44feb7 --- /dev/null +++ b/.changeset/late-donuts-count.md @@ -0,0 +1,5 @@ +--- +"@brainylab/resolver-validators": patch +--- + +Add optional/nullable/or support to Zod resolver diff --git a/README.md b/README.md index b03d163..36f2944 100755 --- a/README.md +++ b/README.md @@ -41,9 +41,12 @@ type SchemaType = InferTypes; //resolve to typebox import { resolver } from '@brainylab/resolver-validators/typebox'; -const resolveToTypeBox = resolver(newSchema); // `resolveToTypeBox` is instance resolved from TypeBox +const resolveToTypeBox = resolver(newSchema); + +import { resolver } from '@brainylab/resolver-validators/zod'; -// resolve to zod (in development) +// `resolveToZod` is instance resolved from Zod +const resolveToZod = resolver(newSchema); ``` diff --git a/src/resolvers/zod.spec.ts b/src/resolvers/zod.spec.ts index 657fe34..438cc64 100755 --- a/src/resolvers/zod.spec.ts +++ b/src/resolvers/zod.spec.ts @@ -52,4 +52,35 @@ describe("Zod Resolver", () => { expect(resolvedZod.parse(schema)).toEqual(zodSchema.parse(schema)); }); + + it("resolve zod schema with optinal and nullable", () => { + const schema = { + name: { + reference: "5087", + }, + }; + + const coreSchema = rv.object({ + name: rv.nullable( + rv.optional( + rv.object({ + reference: rv.string(), + }), + ), + ), + }); + + const zodSchema = z.object({ + name: z + .object({ + reference: z.string(), + }) + .optional() + .nullable(), + }); + + const resolvedZod = resolver(coreSchema); + + expect(resolvedZod.parse(schema)).toEqual(zodSchema.parse(schema)); + }); }); diff --git a/src/resolvers/zod.ts b/src/resolvers/zod.ts index 775084f..1878a8d 100644 --- a/src/resolvers/zod.ts +++ b/src/resolvers/zod.ts @@ -117,6 +117,27 @@ function resolverPrimitiveSchema( options as unknown as ResolverObjectSchema, ) as ZodObject; } + + if (options.type === "optional") { + const inner = resolverPrimitiveSchema(options.schema as PrimitiveSchema); + if (!inner) return undefined; + return (inner as ZodType).optional(); + } + + if (options.type === "nullable") { + const inner = resolverPrimitiveSchema(options.schema as PrimitiveSchema); + if (!inner) return undefined; + return (inner as ZodType).nullable(); + } + + if (options.type === "or" && options.schemas) { + const resolved = options.schemas + .map((s) => resolverPrimitiveSchema(s as PrimitiveSchema)) + .filter(Boolean) as ZodType[]; + if (resolved.length === 0) return undefined; + + return z.union(resolved as ZodType[]); + } } function resolverObjectSchema(opts: ResolverObjectSchema) { From a21dcb9baa6e41886b5b0444d2bf5e3462657289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Felipe=20Schulle?= Date: Fri, 28 Nov 2025 18:15:51 +0000 Subject: [PATCH 2/5] ci(changesets): version packages --- .changeset/late-donuts-count.md | 5 ----- CHANGELOG.md | 6 ++++++ package.json | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 .changeset/late-donuts-count.md diff --git a/.changeset/late-donuts-count.md b/.changeset/late-donuts-count.md deleted file mode 100644 index d44feb7..0000000 --- a/.changeset/late-donuts-count.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@brainylab/resolver-validators": patch ---- - -Add optional/nullable/or support to Zod resolver diff --git a/CHANGELOG.md b/CHANGELOG.md index 0371b7e..cea7234 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # @brainylab/resolver-validators +## 0.8.2 + +### Patch Changes + +- [`514f4ee`](https://github.com/brainylab/resolver-validators/commit/514f4eea3cdd2f790a7151cf60edf142095a68af) Thanks [@andrefelipeschulle](https://github.com/andrefelipeschulle)! - Add optional/nullable/or support to Zod resolver + ## 0.8.1 ### Patch Changes diff --git a/package.json b/package.json index e1e00dd..545165d 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@brainylab/resolver-validators", - "version": "0.8.1", + "version": "0.8.2", "description": "", "keywords": [], "bugs": { From 7ffcca8057a23710e1b6eeb4da33c327914808d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Felipe=20Schulle?= Date: Fri, 5 Dec 2025 09:15:24 -0300 Subject: [PATCH 3/5] fix received undefined properties --- .github/workflows/release.yaml | 10 +++++----- src/resolvers/typebox.ts | 4 +++- src/resolvers/zod.ts | 4 +++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 0b780f4..d11bac6 100755 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -24,12 +24,12 @@ jobs: - name: install and configure git uses: ./.github/common-actions/install - - name: build packages - run: pnpm build - - name: test run: pnpm test + - name: build packages + run: pnpm build + - name: npm package registry authentication run: npm set //registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }} env: @@ -40,8 +40,8 @@ jobs: uses: changesets/action@v1 with: publish: pnpm release - title: 'ci(changesets): :package: version packages' - commit: 'ci(changesets): version packages' + title: "ci(changesets): :package: version packages" + commit: "ci(changesets): version packages" setupGitUser: false env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/src/resolvers/typebox.ts b/src/resolvers/typebox.ts index 8028c82..38a9cc6 100644 --- a/src/resolvers/typebox.ts +++ b/src/resolvers/typebox.ts @@ -131,7 +131,9 @@ function resolverObjectSchema(schema: ResolverObjectSchema) { } } -export function resolver(schema: RVSchema): TSchema { +export function resolver(schema?: RVSchema): TSchema { + if (!schema) return Type.Any(); + if (isObject(schema)) { console.log("aqui 3"); return resolverObjectSchema(schema as ResolverObjectSchema) as TSchema; diff --git a/src/resolvers/zod.ts b/src/resolvers/zod.ts index 1878a8d..d800849 100644 --- a/src/resolvers/zod.ts +++ b/src/resolvers/zod.ts @@ -178,7 +178,9 @@ function resolverObjectSchema(opts: ResolverObjectSchema) { return z.object(resolvedZod) as ZodObject; } -export function resolver(schema: RVSchema): ZodType | ZodObject { +export function resolver(schema?: RVSchema): ZodType | ZodObject { + if (!schema) return z.any(); + if (isObject(schema)) { return resolverObjectSchema(schema as ResolverObjectSchema); } From 245fc31c0f53b3d1521c39d6b5447cd699e87fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Felipe=20Schulle?= Date: Fri, 5 Dec 2025 09:15:50 -0300 Subject: [PATCH 4/5] version --- .changeset/humble-suns-dig.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/humble-suns-dig.md diff --git a/.changeset/humble-suns-dig.md b/.changeset/humble-suns-dig.md new file mode 100644 index 0000000..3a4ac9c --- /dev/null +++ b/.changeset/humble-suns-dig.md @@ -0,0 +1,5 @@ +--- +"@brainylab/resolver-validators": patch +--- + +fix received undefined properties From e5b6590fa7b665def74ded73196f93752edf6b7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Felipe=20Schulle?= Date: Fri, 5 Dec 2025 12:16:29 +0000 Subject: [PATCH 5/5] ci(changesets): version packages --- .changeset/humble-suns-dig.md | 5 ----- CHANGELOG.md | 6 ++++++ package.json | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 .changeset/humble-suns-dig.md diff --git a/.changeset/humble-suns-dig.md b/.changeset/humble-suns-dig.md deleted file mode 100644 index 3a4ac9c..0000000 --- a/.changeset/humble-suns-dig.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@brainylab/resolver-validators": patch ---- - -fix received undefined properties diff --git a/CHANGELOG.md b/CHANGELOG.md index cea7234..2e22d27 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # @brainylab/resolver-validators +## 0.8.3 + +### Patch Changes + +- [`245fc31`](https://github.com/brainylab/resolver-validators/commit/245fc31c0f53b3d1521c39d6b5447cd699e87fa8) Thanks [@andrefelipeschulle](https://github.com/andrefelipeschulle)! - fix received undefined properties + ## 0.8.2 ### Patch Changes diff --git a/package.json b/package.json index 545165d..3032e9e 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@brainylab/resolver-validators", - "version": "0.8.2", + "version": "0.8.3", "description": "", "keywords": [], "bugs": {