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/CHANGELOG.md b/CHANGELOG.md index 0371b7e..2e22d27 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # @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 + +- [`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/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/package.json b/package.json index e1e00dd..3032e9e 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@brainylab/resolver-validators", - "version": "0.8.1", + "version": "0.8.3", "description": "", "keywords": [], "bugs": { 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.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..d800849 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) { @@ -157,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); }