Skip to content
10 changes: 5 additions & 5 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }}
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ type SchemaType = InferTypes<typeof newSchema>;
//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);
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@brainylab/resolver-validators",
"version": "0.8.1",
"version": "0.8.3",
"description": "",
"keywords": [],
"bugs": {
Expand Down
4 changes: 3 additions & 1 deletion src/resolvers/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions src/resolvers/zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
25 changes: 24 additions & 1 deletion src/resolvers/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down