Skip to content

Commit

Permalink
feat(project): add yupschema creator
Browse files Browse the repository at this point in the history
  • Loading branch information
RCVZ committed Jul 27, 2021
1 parent 2d101bd commit 807a981
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/utils/yupSchemaCreator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as yupRaw from 'yup';
import type { AnySchema } from 'yup';
import type Reference from 'yup/lib/Reference';
import type Lazy from 'yup/lib/Lazy';

const yup: any = yupRaw;

export function createYupSchema(
schema: Record<string, any>,
config: Record<string, any>,
): Record<string, AnySchema<any, any, any> | Reference<unknown> | Lazy<any, any>> {
const { name, validationType, validations = [] } = config;

if (!yup[validationType] || !validations) {
return schema;
}

let validator = yup[validationType]();
validations.forEach((validation: any) => {
const { params, type } = validation;
if (!validator[type]) {
return;
}
validator = validator[type](...params);
});
schema[name] = validator;
return schema;
}

0 comments on commit 807a981

Please sign in to comment.