/** * @Author: Rostislav Simonik * @Date: 2019-06-26T13:06:63+02:00 * @Copyright: Technology Studio **/ /* eslint-disable @typescript-eslint/ban-types */ import type { StringSchema, ObjectSchema, DateSchema, ArraySchema, AnySchema, AnyObject, } from 'yup' import { string, object, date, array, } from 'yup' export const SUPPRESS_ERROR_MESSAGE = 'suppress-error-message' export const stringSchema = (): StringSchema => string().trim().nullable() export const requiredStringSchema = (): StringSchema => string().trim().required().nullable() export const relationSchema = ( options: { idKey?: string } = {}, // @ts-expect-error -- NOTE: yup does not return schema, instead it returns the value of given schema ): ObjectSchema>> => ( ) => ( object().shape({ [options.idKey ?? 'id']: stringSchema(), }) ) export const requiredRelationSchema = ( options: { idKey?: string, default?: string } = {}, // @ts-expect-error -- NOTE: yup does not return schema, instead it returns the value of given schema ): ObjectSchema>> => ( object().requiredRelation(options.idKey).shape({ [options.idKey ?? 'id']: stringSchema().meta({ default: options.default }), }) ) export const dateSchema = (): DateSchema => date().nullable() export const requiredDateSchema = (): DateSchema => date().nullable().required() export const numberSchema = (): StringSchema => string().numbersOnly().nullable() export const requiredNumberSchema = (): StringSchema => string().numbersOnly().nullable().required() export const phoneNumberSchema = (): StringSchema => string().trim().phoneNumber() export const requiredPhoneNumberSchema = (): StringSchema => string().trim().phoneNumber().required() // @ts-expect-error -- NOTE: yup does not return schema, instead it returns the value of given schema export const requiredArraySchema = (): ArraySchema => array().required().nullable()