diff --git a/types/yup/index.d.ts b/types/yup/index.d.ts index f29fe60d23..debbec3ada 100644 --- a/types/yup/index.d.ts +++ b/types/yup/index.d.ts @@ -369,8 +369,19 @@ export class Ref { // tslint:disable-next-line:no-empty-interface export interface Lazy extends Schema {} +export interface FormatErrorParams { + path: string; + type: string; + value?: any; + originalValue?: any; +} + +export type LocaleValue = + | string + | ((params: FormatErrorParams) => string); + export interface LocaleObject { - mixed?: { [key in keyof MixedSchema]?: string }; + mixed?: { [key in keyof MixedSchema]?: string; } & { notType?: LocaleValue }; string?: { [key in keyof StringSchema]?: string }; number?: { [key in keyof NumberSchema]?: string }; boolean?: { [key in keyof BooleanSchema]?: string }; diff --git a/types/yup/yup-tests.ts b/types/yup/yup-tests.ts index ff45c00a92..2e3d854ec0 100644 --- a/types/yup/yup-tests.ts +++ b/types/yup/yup-tests.ts @@ -13,7 +13,8 @@ import { TestOptions, ValidateOptions, NumberSchema, - TestContext + TestContext, + LocaleObject } from "yup"; // reach function @@ -452,6 +453,33 @@ const validateOptions: ValidateOptions = { } }; +const localeNotType1: LocaleObject = { + mixed: { + required: "message", + notType: "message" + } +}; +const localeNotType2: LocaleObject = { + mixed: { + required: "message", + notType: () => "message" + } +}; +const localeNotType3: LocaleObject = { + mixed: { + required: "message", + notType: (_ref) => { + const isCast: boolean = _ref.originalValue != null && _ref.originalValue !== _ref.value; + const finalPartOfTheMessage = isCast + ? ` (cast from the value '${_ref.originalValue}').` + : '.'; + + return `${_ref.path} must be a '${_ref.type}'` + + ` but the final value was: '${_ref.value}'${finalPartOfTheMessage}`; + } + } +}; + yup.setLocale({ number: { max: "Max message", min: "Min message" }, string: { email: "String message" }