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
This commit is contained in:
Andres Rosero Velasquez
2019-05-02 12:33:58 -05:00
committed by Wesley Wigham
parent 375444d5bf
commit ca20ec7cf9
2 changed files with 41 additions and 2 deletions

13
types/yup/index.d.ts vendored
View File

@@ -369,8 +369,19 @@ export class Ref {
// tslint:disable-next-line:no-empty-interface
export interface Lazy extends Schema<any> {}
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 };

View File

@@ -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" }