diff --git a/src/utils/yupSchemaCreator.ts b/src/utils/yupSchemaCreator.ts new file mode 100644 index 000000000..3430273d0 --- /dev/null +++ b/src/utils/yupSchemaCreator.ts @@ -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, + config: Record, +): Record | Reference | Lazy> { + 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; +}