mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-07-04 09:10:09 +00:00
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:
committed by
Wesley Wigham
parent
375444d5bf
commit
ca20ec7cf9
13
types/yup/index.d.ts
vendored
13
types/yup/index.d.ts
vendored
@@ -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 };
|
||||
|
||||
@@ -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" }
|
||||
|
||||
Reference in New Issue
Block a user