Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# @brainylab/resolver-validators

## 0.8.9

### Patch Changes

- [`9a02ee1`](https://github.com/brainylab/resolver-validators/commit/9a02ee14bf642ff14ffe3e61332fb47a4de1fd5a) Thanks [@andrefelipeschulle](https://github.com/andrefelipeschulle)! - "fix when adding the parameters in the zod parse

## 0.8.8

### Patch Changes

- [`cb6e959`](https://github.com/brainylab/resolver-validators/commit/cb6e959d146f282e9ad066081067f5e8003e7cb2) Thanks [@andrefelipeschulle](https://github.com/andrefelipeschulle)! - fix import utils on typebox-deprecated resolver

## 0.8.7

### Patch Changes

- [`5e64d69`](https://github.com/brainylab/resolver-validators/commit/5e64d69e4ddf8b353d4a9fc503c7cc48e8cdf6bd) Thanks [@andrefelipeschulle](https://github.com/andrefelipeschulle)! - Make resolver schema parameter optional

## 0.8.6

### Patch Changes

- [`5b03aba`](https://github.com/brainylab/resolver-validators/commit/5b03aba347737e7aa3d68540cd9ba0be2125b623) Thanks [@andrefelipeschulle](https://github.com/andrefelipeschulle)! - fix: fix received undefined in resolver function e resolve or undefined

## 0.8.5

### Patch Changes
Expand Down
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.5",
"version": "0.8.9",
"description": "",
"keywords": [],
"bugs": {
Expand Down
49 changes: 19 additions & 30 deletions src/resolvers/typebox-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import {
Type,
} from "@sinclair/typebox";

import { isObject } from "@/utils";

import type {
RVNumberParams,
RVParams,
Expand All @@ -24,8 +22,7 @@ import type {
RVTypes,
} from "@/types";

// import { TBOptional } from "./optional";
// import { TBRequired } from "./required";
import { isObject } from "../utils";

function TBString(params?: RVStringParams): TString {
const typeBoxParams: StringOptions = {};
Expand Down Expand Up @@ -99,10 +96,6 @@ function TBNumber(params?: RVNumberParams): TNumber {
return Type.Number(typeBoxParams);
}

function TBObject(schema: TObject): TObject {
return Type.Object(schema);
}

type PrimitiveSchema = {
type: RVTypes;
schema?: RVSchema;
Expand All @@ -116,9 +109,7 @@ type ResolverObjectSchema = {
schema: { [key: string]: PrimitiveSchema };
};

function resolverPrimitiveSchema(
options: PrimitiveSchema,
): TSchema | undefined {
function resolverPrimitiveSchema(options: PrimitiveSchema): TSchema {
if (options.type === "string") {
return TBString(options.params);
}
Expand Down Expand Up @@ -148,12 +139,6 @@ function resolverPrimitiveSchema(
return TBTuple(tuplePrimitiveResolved as TSchema[]);
}

if (options.type === "object") {
return resolverObjectSchema(
options as unknown as ResolverObjectSchema,
) as TObject;
}

if (options.type === "optional") {
return TBOptional(
resolverPrimitiveSchema(options.schema as PrimitiveSchema) as TSchema,
Expand All @@ -173,30 +158,34 @@ function resolverPrimitiveSchema(
) as TSchema[],
);
}
}

function resolverObjectSchema(schema: ResolverObjectSchema) {
if (schema.type === "object") {
const resolvedTypeBox = {} as TObject;
return resolverObjectSchema(
options as unknown as ResolverObjectSchema,
) as TObject;
}

const primitiveSchema = schema.schema;
function resolverObjectSchema(schema: ResolverObjectSchema): TSchema {
const resolvedTypeBox = {} as TObject;

for (const key in primitiveSchema) {
const value = primitiveSchema[key];
const primitiveSchema = schema.schema;

if (value.type === "optional") {
resolvedTypeBox[key] = resolverPrimitiveSchema(value);
continue;
}
for (const key in primitiveSchema) {
const value = primitiveSchema[key];

if (value.type === "optional") {
resolvedTypeBox[key] = resolverPrimitiveSchema(value);
continue;
}

return TBObject(resolvedTypeBox);
resolvedTypeBox[key] = resolverPrimitiveSchema(value);
}

return Type.Object(resolvedTypeBox);
}

export function resolver(schema: RVSchema) {
export function resolver(schema?: RVSchema): TSchema {
if (!schema) return Type.Any();

if (isObject(schema)) {
return resolverObjectSchema(schema as ResolverObjectSchema);
}
Expand Down
27 changes: 12 additions & 15 deletions src/resolvers/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,33 +109,30 @@ function resolverPrimitiveSchema(options: PrimitiveSchema): TSchema {
}

function resolverObjectSchema(schema: ResolverObjectSchema) {
if (schema.type === "object") {
const resolvedTypebox: TProperties = {} as TProperties;
const resolvedTypebox: TProperties = {} as TProperties;

const primitiveSchema = schema.schema;
const primitiveSchema = schema.schema;

for (const key in primitiveSchema) {
const value = primitiveSchema[key];
for (const key in primitiveSchema) {
const value = primitiveSchema[key];

if (value.type === "optional") {
resolvedTypebox[key] = resolverPrimitiveSchema(value);
continue;
}

resolvedTypebox[key] = resolverPrimitiveSchema(
value as PrimitiveSchema,
) as TSchema;
if (value.type === "optional") {
resolvedTypebox[key] = resolverPrimitiveSchema(value);
continue;
}

return Type.Object(resolvedTypebox);
resolvedTypebox[key] = resolverPrimitiveSchema(
value as PrimitiveSchema,
) as TSchema;
}

return Type.Object(resolvedTypebox);
}

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
16 changes: 16 additions & 0 deletions src/resolvers/zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,20 @@ describe("Zod Resolver", () => {

expect(resolvedZod.parse(schema)).toEqual(zodSchema.parse(schema));
});

it("resolve zod number schema number type", async () => {
expect(resolver(rv.number()).parse(20)).toEqual(20);
expect(resolver(rv.number({ coerce: true })).parse("20")).toEqual(20);
expect(resolver(rv.number({ min: 3, max: 5 })).parse(4)).toEqual(4);
expect(
resolver(rv.object({ age: rv.number({ default: 5 }) })).safeParse({})
.data,
).toEqual({ age: 5 });
expect(() => resolver(rv.number({ min: 5 })).parse(4)).toThrowError(
"Too small: expected number to be >=5",
);
expect(() => resolver(rv.number({ max: 5 })).parse(6)).toThrowError(
"Too big: expected number to be <=5",
);
});
});
19 changes: 13 additions & 6 deletions src/resolvers/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ function resolverPrimitiveSchema(
sSchema = z.string();
}

if (params?.min) sSchema.min(params.min);
if (params?.max) sSchema.max(params.max);
if (params?.min) sSchema = sSchema.min(params.min);
if (params?.max) sSchema = sSchema.max(params.max);
if (params?.default) sSchema = sSchema.default(params.default);

if (params?.description) sSchema.describe(params.description);
if (params?.description) sSchema = sSchema.describe(params.description);

return sSchema;
}
Expand All @@ -61,9 +62,11 @@ function resolverPrimitiveSchema(
nSchema = z.number();
}

if (params?.min) nSchema.min(params.min);
if (params?.max) nSchema.max(params.max);
if (params?.description) nSchema.describe(params.description);
if (params?.min) nSchema = nSchema.min(params.min);
if (params?.max) nSchema = nSchema.max(params.max);
if (params?.default) nSchema = nSchema.default(params.default);

if (params?.description) nSchema = nSchema.describe(params.description);

return nSchema;
}
Expand All @@ -79,6 +82,8 @@ function resolverPrimitiveSchema(
bSchema = z.boolean();
}

if (params?.default) bSchema = bSchema.default(params.default);

return bSchema;
}

Expand All @@ -93,6 +98,8 @@ function resolverPrimitiveSchema(
dSchema = z.date();
}

if (params?.default) dSchema = dSchema.default(params.default);

return dSchema;
}

Expand Down
8 changes: 6 additions & 2 deletions src/rv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type {
RVArray,
RVBoolean,
RVBooleanParams,
RVDate,
RVDateParams,
RVNullable,
RVNumber,
RVNumberParams,
Expand All @@ -22,9 +24,10 @@ export function array<T extends RVSchema>(schema: T): RVArray<T> {
} as never;
}

export function boolean(): RVBoolean {
export function boolean(params?: RVBooleanParams): RVBoolean {
return {
type: "boolean",
params,
} as never;
}

Expand Down Expand Up @@ -81,8 +84,9 @@ export function nullable<T extends RVSchema>(schema: T): RVNullable<T> {
} as never;
}

export function date(): RVDate {
export function date(params?: RVDateParams): RVDate {
return {
type: "date",
params,
} as never;
}
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface RVArray<T extends RVSchema> extends RVArraySchema {
infer: ArrayStatic<T, this["params"]>;
}

export type RVDateParams = { coerce?: boolean };
export type RVDateParams = { coerce?: boolean; default?: Date };

export interface RVDate extends RVSchema {
type: "date";
Expand All @@ -58,7 +58,7 @@ export interface RVNullable<T extends RVSchema> extends RVSchema {
infer: T extends { infer: infer U } ? U | null : null;
}

export type RVBooleanParams = { coerce?: boolean };
export type RVBooleanParams = { coerce?: boolean; default?: boolean };

export interface RVBoolean extends RVSchema {
type: "boolean";
Expand All @@ -70,6 +70,7 @@ export type RVStringParams = RVParams & {
format?: "email";
pattern?: string | RegExp;
coerce?: boolean;
default?: string;
};

export interface RVString extends RVSchema {
Expand All @@ -86,6 +87,7 @@ export interface RVOptional<T> extends RVSchema {

export type RVNumberParams = RVParams & {
coerce?: boolean;
default?: number;
};

export interface RVNumber extends RVSchema {
Expand Down
35 changes: 18 additions & 17 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["dom", "esnext"],
"moduleResolution": "node",
"rootDir": "./",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"paths": {
"@/*": ["./src/*"]
},
"types": ["vitest"]
},
"include": ["src/**/*", "./tsup.config.ts"],
"exclude": ["node_modules"]
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["dom", "esnext"],
"moduleResolution": "node",
"rootDir": "./",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
},
"types": ["vitest"]
},
"include": ["src/**/*", "./tsup.config.ts"],
"exclude": ["node_modules"]
}