From 01301313642b69b24351c2a0bc26378586de02c3 Mon Sep 17 00:00:00 2001 From: Kirill Dyachkovskiy <81510334+KirillDyachkovskiy@users.noreply.github.com> Date: Thu, 14 Dec 2023 16:48:21 +0300 Subject: [PATCH] feat(validators): add custom error messages property (#150) --- src/lib/kit/validators/messages.ts | 6 ++- src/lib/kit/validators/types.ts | 16 ++++++ src/lib/kit/validators/validators.ts | 77 ++++++++++++++++------------ 3 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 src/lib/kit/validators/types.ts diff --git a/src/lib/kit/validators/messages.ts b/src/lib/kit/validators/messages.ts index 40d38c8f..a670feec 100644 --- a/src/lib/kit/validators/messages.ts +++ b/src/lib/kit/validators/messages.ts @@ -1,7 +1,9 @@ import i18n from '../i18n'; import {subscribeConfigure} from '../i18n/configure'; -const getErrorMessages = () => ({ +import type {ErrorMessagesType} from './types'; + +const getErrorMessages = (): ErrorMessagesType => ({ REQUIRED: i18n('label_error-required'), INVALID: i18n('label_error-invalid'), INT: i18n('label_error-int'), @@ -30,7 +32,7 @@ const getErrorMessages = () => ({ ZERO_START: i18n('label_error-zero-start'), }); -export let ErrorMessages = getErrorMessages(); +export let ErrorMessages: ErrorMessagesType = getErrorMessages(); subscribeConfigure(() => { ErrorMessages = getErrorMessages(); diff --git a/src/lib/kit/validators/types.ts b/src/lib/kit/validators/types.ts new file mode 100644 index 00000000..53da49a5 --- /dev/null +++ b/src/lib/kit/validators/types.ts @@ -0,0 +1,16 @@ +export interface ErrorMessagesType { + REQUIRED: string; + INVALID: string; + INT: string; + NUMBER: string; + minLength: (count: number | bigint) => string; + minLengthArr: (count: number | bigint) => string; + maxLength: (count: number | bigint) => string; + maxLengthArr: (count: number | bigint) => string; + minNumber: (count: number | bigint) => string; + maxNumber: (count: number | bigint) => string; + SPACE_START: string; + SPACE_END: string; + DOT_END: string; + ZERO_START: string; +} diff --git a/src/lib/kit/validators/validators.ts b/src/lib/kit/validators/validators.ts index c81e61d5..f83b6abe 100644 --- a/src/lib/kit/validators/validators.ts +++ b/src/lib/kit/validators/validators.ts @@ -12,21 +12,28 @@ import { import {ErrorMessages} from '../validators'; import {isFloat, isInt} from './helpers'; +import {ErrorMessagesType} from './types'; -export interface GetArrayValidatorParams { +interface CommonValidatorParams { ignoreRequiredCheck?: boolean; + customErrorMessages?: Partial; +} + +export interface GetArrayValidatorParams extends CommonValidatorParams { ignoreMaxLengthCheck?: boolean; ignoreMinLengthCheck?: boolean; } export const getArrayValidator = (params: GetArrayValidatorParams = {}) => { - const {ignoreRequiredCheck, ignoreMaxLengthCheck, ignoreMinLengthCheck} = params; + const {ignoreRequiredCheck, ignoreMaxLengthCheck, ignoreMinLengthCheck, customErrorMessages} = + params; + const errorMessages = {...ErrorMessages, ...customErrorMessages}; return (spec: ArraySpec, value?: ArrayValue) => { const valueLength = value?.length || 0; if (!ignoreRequiredCheck && spec.required && !_.isArray(value)) { - return ErrorMessages.REQUIRED; + return errorMessages.REQUIRED; } if ( @@ -34,7 +41,7 @@ export const getArrayValidator = (params: GetArrayValidatorParams = {}) => { typeof spec.maxLength === 'bigint' && valueLength > spec.maxLength ) { - return ErrorMessages.maxLengthArr(spec.maxLength); + return errorMessages.maxLengthArr(spec.maxLength); } if ( @@ -42,31 +49,29 @@ export const getArrayValidator = (params: GetArrayValidatorParams = {}) => { typeof spec.minLength === 'bigint' && valueLength < spec.minLength ) { - return ErrorMessages.minLengthArr(spec.minLength); + return errorMessages.minLengthArr(spec.minLength); } return false; }; }; -export interface GetBooleanValidatorParams { - ignoreRequiredCheck?: boolean; -} +export interface GetBooleanValidatorParams extends CommonValidatorParams {} export const getBooleanValidator = (params: GetBooleanValidatorParams = {}) => { - const {ignoreRequiredCheck} = params; + const {ignoreRequiredCheck, customErrorMessages} = params; + const errorMessages = {...ErrorMessages, ...customErrorMessages}; return (spec: BooleanSpec, value?: boolean) => { if (!ignoreRequiredCheck && spec.required && !value) { - return ErrorMessages.REQUIRED; + return errorMessages.REQUIRED; } return false; }; }; -export interface GetNumberValidatorParams { - ignoreRequiredCheck?: boolean; +export interface GetNumberValidatorParams extends CommonValidatorParams { ignoreSpaceStartCheck?: boolean; ignoreSpaceEndCheck?: boolean; ignoreNumberCheck?: boolean; @@ -88,30 +93,33 @@ export const getNumberValidator = (params: GetNumberValidatorParams = {}) => { ignoreIntCheck, ignoreDotEnd, ignoreZeroStart, + customErrorMessages, } = params; + const errorMessages = {...ErrorMessages, ...customErrorMessages}; + // eslint-disable-next-line complexity return (spec: NumberSpec, value: string | number = '') => { const stringValue = String(value); if (!ignoreRequiredCheck && spec.required && !stringValue.length) { - return ErrorMessages.REQUIRED; + return errorMessages.REQUIRED; } if (stringValue.length) { if (!ignoreSpaceStartCheck && !stringValue[0].trim()) { - return ErrorMessages.SPACE_START; + return errorMessages.SPACE_START; } if (!ignoreSpaceEndCheck && !stringValue[stringValue.length - 1].trim()) { - return ErrorMessages.SPACE_END; + return errorMessages.SPACE_END; } if (!ignoreDotEnd && stringValue[stringValue.length - 1] === '.') { - return ErrorMessages.DOT_END; + return errorMessages.DOT_END; } if (!ignoreNumberCheck && !isFloat(stringValue)) { - return ErrorMessages.NUMBER; + return errorMessages.NUMBER; } if ( @@ -121,7 +129,7 @@ export const getNumberValidator = (params: GetNumberValidatorParams = {}) => { stringValue.substring(0, 2) === '-0' && stringValue[2] !== '.')) ) { - return ErrorMessages.ZERO_START; + return errorMessages.ZERO_START; } } @@ -131,7 +139,7 @@ export const getNumberValidator = (params: GetNumberValidatorParams = {}) => { stringValue.length && Number(stringValue) > spec.maximum ) { - return ErrorMessages.maxNumber(spec.maximum); + return errorMessages.maxNumber(spec.maximum); } if ( @@ -140,12 +148,12 @@ export const getNumberValidator = (params: GetNumberValidatorParams = {}) => { stringValue.length && spec.minimum > Number(stringValue) ) { - return ErrorMessages.minNumber(spec.minimum); + return errorMessages.minNumber(spec.minimum); } if (_.isString(spec.format) && stringValue.length) { if (!ignoreIntCheck && spec.format === 'int64' && !isInt(stringValue)) { - return ErrorMessages.INT; + return errorMessages.INT; } } @@ -153,24 +161,22 @@ export const getNumberValidator = (params: GetNumberValidatorParams = {}) => { }; }; -export interface GetObjectValidatorParams { - ignoreRequiredCheck?: boolean; -} +export interface GetObjectValidatorParams extends CommonValidatorParams {} export const getObjectValidator = (params: GetObjectValidatorParams = {}) => { - const {ignoreRequiredCheck} = params; + const {ignoreRequiredCheck, customErrorMessages} = params; + const errorMessages = {...ErrorMessages, ...customErrorMessages}; return (spec: ObjectSpec, value?: ObjectValue) => { if (!ignoreRequiredCheck && spec.required && !value) { - return ErrorMessages.REQUIRED; + return errorMessages.REQUIRED; } return false; }; }; -export interface GetStringValidatorParams { - ignoreRequiredCheck?: boolean; +export interface GetStringValidatorParams extends CommonValidatorParams { ignoreSpaceStartCheck?: boolean; ignoreSpaceEndCheck?: boolean; ignoreMaxLengthCheck?: boolean; @@ -186,22 +192,25 @@ export const getStringValidator = (params: GetStringValidatorParams = {}) => { ignoreMaxLengthCheck, ignoreMinLengthCheck, ignoreRegExpCheck, + customErrorMessages, } = params; + const errorMessages = {...ErrorMessages, ...customErrorMessages}; + // eslint-disable-next-line complexity return (spec: StringSpec, value = '') => { const valueLength = value?.length; if (!ignoreRequiredCheck && spec.required && !valueLength) { - return ErrorMessages.REQUIRED; + return errorMessages.REQUIRED; } if (valueLength) { if (!ignoreSpaceStartCheck && !value[0].trim()) { - return ErrorMessages.SPACE_START; + return errorMessages.SPACE_START; } if (!ignoreSpaceEndCheck && !value[value.length - 1].trim()) { - return ErrorMessages.SPACE_END; + return errorMessages.SPACE_END; } } @@ -210,7 +219,7 @@ export const getStringValidator = (params: GetStringValidatorParams = {}) => { typeof spec.maxLength === 'bigint' && valueLength > spec.maxLength ) { - return ErrorMessages.maxLength(spec.maxLength); + return errorMessages.maxLength(spec.maxLength); } if ( @@ -218,7 +227,7 @@ export const getStringValidator = (params: GetStringValidatorParams = {}) => { typeof spec.minLength === 'bigint' && valueLength < spec.minLength ) { - return ErrorMessages.minLength(spec.minLength); + return errorMessages.minLength(spec.minLength); } if (_.isString(spec.pattern) && spec.pattern.length) { @@ -227,7 +236,7 @@ export const getStringValidator = (params: GetStringValidatorParams = {}) => { if (!ignoreRegExpCheck && !regex.test(value)) { return _.isString(spec.patternError) && spec.patternError.length ? spec.patternError - : ErrorMessages.INVALID; + : errorMessages.INVALID; } }