From e50d54750b1c59ef5270ad292567873eacf164dc Mon Sep 17 00:00:00 2001 From: gkhanduman Date: Sat, 2 Dec 2023 13:25:26 +0300 Subject: [PATCH] feat: added "en" default language --- README.md | 1 + src/index.ts | 2 ++ src/locales/en.ts | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/locales/en.ts diff --git a/README.md b/README.md index 6901c3a..519abdd 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Locales implemented: - `bs` - `da` - `de` +- `en` - `es` - `fr` - `he` diff --git a/src/index.ts b/src/index.ts index 20b2b4f..01781da 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import * as ar from './locales/ar'; import * as bs from './locales/bs'; import * as da from './locales/da'; import * as de from './locales/de'; +import * as en from './locales/en'; import * as es from './locales/es'; import * as fr from './locales/fr'; import * as he from './locales/he'; @@ -29,6 +30,7 @@ export { bs, da, de, + en, es, fr, he, diff --git a/src/locales/en.ts b/src/locales/en.ts new file mode 100644 index 0000000..2b44847 --- /dev/null +++ b/src/locales/en.ts @@ -0,0 +1,64 @@ +/*eslint-disable no-template-curly-in-string*/ +import { printValue, LocaleObject } from 'yup'; + +export const mixed: LocaleObject['mixed'] = { + default: '${path} is invalid', + required: '${path} is a required field', + defined: '${path} must be defined', + notNull: '${path} cannot be null', + oneOf: '${path} must be one of the following values: ${values}', + notOneOf: '${path} must not be one of the following values: ${values}', + notType: ({ path, type, value, originalValue }) => { + const isCast = originalValue != null && originalValue !== value; + let msg = + `${path} must be a \`${type}\` type, ` + + `but the final value was: \`${printValue(value, true)}\`` + + (isCast + ? ` (cast from the value \`${printValue(originalValue, true)}\`).` + : '.'); + + if (value === null) { + msg += + `\n If "null" is intended as an empty value be sure to mark the schema as` + + ' `.nullable()`'; + } + + return msg; + }, +}; +export const string: LocaleObject['string'] = { + length: '${path} must be exactly ${length} characters', + min: '${path} must be at least ${min} characters', + max: '${path} must be at most ${max} characters', + matches: '${path} must match the following: "${regex}"', + email: '${path} must be a valid email', + url: '${path} must be a valid URL', + uuid: '${path} must be a valid UUID', + trim: '${path} must be a trimmed string', + lowercase: '${path} must be a lowercase string', + uppercase: '${path} must be a upper case string', +}; +export const number: LocaleObject['number'] = { + min: '${path} must be greater than or equal to ${min}', + max: '${path} must be less than or equal to ${max}', + lessThan: '${path} must be less than ${less}', + moreThan: '${path} must be greater than ${more}', + positive: '${path} must be a positive number', + negative: '${path} must be a negative number', + integer: '${path} must be an integer', +}; +export const date: LocaleObject['date'] = { + min: '${path} field must be later than ${min}', + max: '${path} field must be at earlier than ${max}', +}; +export const object: LocaleObject['object'] = { + noUnknown: '${path} field has unspecified keys: ${unknown}', +}; +export const array: LocaleObject['array'] = { + min: '${path} field must have at least ${min} items', + max: '${path} field must have less than or equal to ${max} items', + length: '${path} must have ${length} items', +}; +export const boolean: LocaleObject['boolean'] = { + isValue: '${path} field must be ${value}', +};