Skip to content

Commit

Permalink
feat(required): create required and description param on string,numbe…
Browse files Browse the repository at this point in the history
…r and required
  • Loading branch information
andrefelipeschulle committed Oct 11, 2024
1 parent 3be0cc0 commit e8275c5
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-seals-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@brainylab/resolver-validators": minor
---

create required and add param description on required,string,number
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ dist
coverage

.DS_Store
typebox/*
./typebox/*
index.cjs
index.d.ts
index.d.cts
Expand Down
14 changes: 14 additions & 0 deletions src/core/required.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { RVRequired, RVRequiredParams } from '@/types/required';
import type { RVSchema } from '@/types/schema';

export function required<T extends RVSchema>(
schema: T,
params?: RVRequiredParams,
): RVRequired<T> {
return {
type: 'required',
schema,
params,
required: true,
} as never;
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export type * from './types/object';
import { optional } from './core/optional';
export type * from './types/optional';

import { required } from './core/required';
// export type * from './types/required';

import { string } from './core/string';
export type * from './types/string';

Expand All @@ -39,6 +42,7 @@ export const rv = {
number,
object,
optional,
required,
string,
tuple,
isArray,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { resolver } from './resolver';
describe('TypeBox Resolver', () => {
it('resolver core schema to typebox validator', () => {
const coreSchema = rv.object({
name: rv.string(),
name: rv.required(rv.string(), { description: 'description test' }),
age: rv.optional(rv.number()),
isActive: rv.boolean(),
other: rv.object({
Expand All @@ -21,7 +21,7 @@ describe('TypeBox Resolver', () => {
});

const typeBoxSchema = Type.Object({
name: Type.String(),
name: Type.Required(Type.String(), { description: 'description test' }),
age: Type.Optional(Type.Number()),
isActive: Type.Boolean(),
other: Type.Object({
Expand Down
17 changes: 12 additions & 5 deletions src/resolvers/typebox/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ import type { RVNumberParams } from '@/types/number';
export function TBNumber(params?: RVNumberParams): TNumber {
const typeBoxParams: NumberOptions = {};

if (params?.min) {
typeBoxParams.minLength = params.min;
}
const keys: { [key in keyof RVNumberParams]: string } = {
min: 'minLength',
max: 'maxLength',
description: 'description',
};

if (params?.max) {
typeBoxParams.maxLength = params.max;
if (params) {
for (const key in keys) {
const mappedKey = keys[key as keyof typeof keys] as keyof RVNumberParams;
if (params[key as keyof RVNumberParams] !== undefined) {
typeBoxParams[mappedKey] = params[key as keyof RVNumberParams];
}
}
}

return Type.Number(typeBoxParams);
Expand Down
28 changes: 28 additions & 0 deletions src/resolvers/typebox/required.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Type } from '@sinclair/typebox';

import type { RVRequiredParams } from '@/types/required';
import type { SchemaOptions, TSchema } from '@sinclair/typebox';

export function TBRequired(
schema: TSchema,
params?: RVRequiredParams,
): TSchema {
const typeBoxParams: SchemaOptions = {};

const keys: { [key in keyof RVRequiredParams]: string } = {
description: 'description',
};

if (params) {
for (const key in keys) {
const mappedKey = keys[
key as keyof typeof keys
] as keyof RVRequiredParams;
if (params[key as keyof RVRequiredParams] !== undefined) {
typeBoxParams[mappedKey] = params[key as keyof RVRequiredParams];
}
}
}

return Type.Required(schema as unknown as TSchema, typeBoxParams);
}
8 changes: 8 additions & 0 deletions src/resolvers/typebox/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TBBoolean } from './boolean';
import { TBDate } from './date';
import { TBArray } from './array';
import { TBTuple } from './tuple';
import { TBRequired } from './required';

import type { TArray, TObject, TSchema } from '@sinclair/typebox';
import type { RVParams } from '@/types/params';
Expand Down Expand Up @@ -69,6 +70,13 @@ function resolverPrimitiveSchema(
resolverPrimitiveSchema(options.schema as PrimitiveSchema) as TSchema,
);
}

if (options.type === 'required') {
return TBRequired(
resolverPrimitiveSchema(options.schema as PrimitiveSchema) as TSchema,
options.params,
);
}
}

function resolverObjectSchema(schema: ResolverObjectSchema) {
Expand Down
28 changes: 14 additions & 14 deletions src/resolvers/typebox/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import type { RVStringParams } from '@/types/string';
export function TBString(params?: RVStringParams): TString {
const typeBoxParams: StringOptions = {};

if (params?.min) {
typeBoxParams.minLength = params.min;
}

if (params?.max) {
typeBoxParams.maxLength = params.max;
}

if (params?.format) {
typeBoxParams.format = params.format;
}
const keys: { [key in keyof RVStringParams]: string } = {
min: 'minLength',
max: 'maxLength',
format: 'format',
pattern: 'pattern',
description: 'description',
};

if (params?.regex) {
typeBoxParams.format = 'regex';
typeBoxParams.pattern = params.regex;
if (params) {
for (const key in keys) {
const mappedKey = keys[key as keyof typeof keys] as keyof RVStringParams;
if (params[key as keyof RVStringParams] !== undefined) {
typeBoxParams[mappedKey] = params[key as keyof RVStringParams];
}
}
}

return Type.String(typeBoxParams);
Expand Down
1 change: 1 addition & 0 deletions src/types/helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export type RVTypes =
| 'number'
| 'object'
| 'optional'
| 'required'
| 'string'
| 'tuple';
1 change: 1 addition & 0 deletions src/types/params.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type RVParams = {
min?: number;
max?: number;
description?: string;
};
7 changes: 7 additions & 0 deletions src/types/required.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { RVObject } from './object';
import type { RVSchema } from './schema';

export type RVRequiredParams = { description?: string };

export type RVRequired<T extends RVSchema> =
T extends RVObject<infer U> ? RVObject<RVRequired<U>> : T;
2 changes: 1 addition & 1 deletion src/types/string.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { RVParams } from './params';

export type RVStringParams = RVParams & {
format?: 'email';
regex?: string;
pattern?: string;
};

export interface RVString extends RVSchema {
Expand Down

0 comments on commit e8275c5

Please sign in to comment.