From ca20ec7cf9fa9cca49ea437230e0f4fd2924fac2 Mon Sep 17 00:00:00 2001 From: Andres Rosero Velasquez <9358393+drebits@users.noreply.github.com> Date: Thu, 2 May 2019 12:33:58 -0500 Subject: [PATCH] Types for the 'mixed.notType' property from the Yup library. (#35004) * Types for the mixed.notType function. * move notType only to 'LocaleObject' * fix delete 'label' property * fix to 'strict-export-declare-modifiers' rule * added test from MixedSchema * export interfaces FormatErrorParams and LocaleValue --- types/yup/index.d.ts | 13 ++++++++++++- types/yup/yup-tests.ts | 30 +++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) 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" }