mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-03 22:37:03 +00:00
Merge pull request #32067 from YashdalfTheGray/master
@types/yup: Add a ValidationError class to match the source code
This commit is contained in:
Vendored
+33
-12
@@ -8,7 +8,7 @@
|
||||
// Vincent Pizzo <https://github.com/vincentjames501>
|
||||
// Robert Bullen <https://github.com/robertbullen>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.2
|
||||
// TypeScript Version: 2.8
|
||||
|
||||
export function reach<T>(
|
||||
schema: Schema<T>,
|
||||
@@ -23,12 +23,6 @@ export function addMethod<T extends Schema<any>>(
|
||||
): void;
|
||||
export function ref(path: string, options?: { contextPrefix: string }): Ref;
|
||||
export function lazy<T>(fn: (value: T) => Schema<T>): Lazy;
|
||||
export function ValidationError(
|
||||
errors: string | string[],
|
||||
value: any,
|
||||
path: string,
|
||||
type?: any
|
||||
): ValidationError;
|
||||
export function setLocale(customLocale: LocaleObject): void;
|
||||
|
||||
export const mixed: MixedSchemaConstructor;
|
||||
@@ -49,7 +43,8 @@ export type AnySchemaConstructor =
|
||||
| ArraySchemaConstructor
|
||||
| ObjectSchemaConstructor;
|
||||
|
||||
export type TestOptionsMessage = string
|
||||
export type TestOptionsMessage =
|
||||
| string
|
||||
| ((params: object & Partial<TestMessageParams>) => string);
|
||||
|
||||
export interface Schema<T> {
|
||||
@@ -171,14 +166,19 @@ export interface ArraySchema<T> extends Schema<T[]> {
|
||||
compact(rejector?: (value: any) => boolean): ArraySchema<T>;
|
||||
}
|
||||
|
||||
export type ObjectSchemaDefinition<T extends object> = { [field in keyof T]: Schema<T[field]> | Ref };
|
||||
export type ObjectSchemaDefinition<T extends object> = {
|
||||
[field in keyof T]: Schema<T[field]> | Ref
|
||||
};
|
||||
|
||||
/**
|
||||
* Merges two interfaces. For properties in common, property types from `U` trump those of `T`.
|
||||
* This is conducive to the functionality of
|
||||
* [yup's `object.shape()` method](https://www.npmjs.com/package/yup#objectshapefields-object-nosortedges-arraystring-string-schema).
|
||||
*/
|
||||
export type Shape<T extends object, U extends object> = { [P in keyof T]: P extends keyof U ? U[P] : T[P] } & U;
|
||||
export type Shape<T extends object, U extends object> = {
|
||||
[P in keyof T]: P extends keyof U ? U[P] : T[P]
|
||||
} &
|
||||
U;
|
||||
|
||||
export interface ObjectSchemaConstructor {
|
||||
<T extends object>(fields?: ObjectSchemaDefinition<T>): ObjectSchema<T>;
|
||||
@@ -191,7 +191,10 @@ export interface ObjectSchema<T extends object> extends Schema<T> {
|
||||
noSortEdges?: Array<[string, string]>
|
||||
): ObjectSchema<Shape<T, U>>;
|
||||
from(fromKey: string, toKey: string, alias?: boolean): ObjectSchema<T>;
|
||||
noUnknown(onlyKnownKeys?: boolean, message?: TestOptionsMessage): ObjectSchema<T>;
|
||||
noUnknown(
|
||||
onlyKnownKeys?: boolean,
|
||||
message?: TestOptionsMessage
|
||||
): ObjectSchema<T>;
|
||||
transformKeys(callback: (key: any) => any): void;
|
||||
camelCase(): ObjectSchema<T>;
|
||||
constantCase(): ObjectSchema<T>;
|
||||
@@ -292,7 +295,12 @@ export interface SchemaDescription {
|
||||
fields: object;
|
||||
}
|
||||
|
||||
export interface ValidationError {
|
||||
// ValidationError works a lot more like a class vs. a constructor
|
||||
// function that returns an interface. It's also got a couple of
|
||||
// static methods and it inherits from the generic Error class in
|
||||
// the [yup codebase][1].
|
||||
// [1]: (https://github.com/jquense/yup/blob/master/src/ValidationError.js)
|
||||
export class ValidationError extends Error {
|
||||
name: string;
|
||||
message: string;
|
||||
value: any;
|
||||
@@ -311,6 +319,19 @@ export interface ValidationError {
|
||||
*/
|
||||
inner: ValidationError[];
|
||||
params?: object;
|
||||
|
||||
static isError(err: any): err is ValidationError;
|
||||
static formatError(
|
||||
message: string | ((params?: any) => string),
|
||||
params?: any
|
||||
): string | ((params?: any) => string);
|
||||
|
||||
constructor(
|
||||
errors: string | string[],
|
||||
value: any,
|
||||
path: string,
|
||||
type?: any
|
||||
);
|
||||
}
|
||||
|
||||
// It is tempting to declare `Ref` very simply, but there are problems with these approaches:
|
||||
|
||||
+21
-14
@@ -77,12 +77,21 @@ const renderable = yup.lazy(value => {
|
||||
});
|
||||
const renderables = yup.array().of(renderable);
|
||||
|
||||
// ValidationError static methods
|
||||
// $ExpectType boolean
|
||||
ValidationError.isError(new ValidationError("error", "value", "path"));
|
||||
// $ExpectType string | ((params?: any) => string)
|
||||
ValidationError.formatError("error", { path: "path" });
|
||||
ValidationError.formatError("error");
|
||||
ValidationError.formatError(() => "error");
|
||||
ValidationError.formatError(() => "error", { path: "path" });
|
||||
|
||||
// ValidationError
|
||||
let error: ValidationError = yup.ValidationError("error", "value", "path");
|
||||
error = yup.ValidationError(["error", "error2"], true, "path");
|
||||
error = yup.ValidationError(["error", "error2"], 5, "path");
|
||||
error = yup.ValidationError(["error", "error2"], { name: "value" }, "path");
|
||||
error = yup.ValidationError(
|
||||
let error: ValidationError = new yup.ValidationError("error", "value", "path");
|
||||
error = new yup.ValidationError(["error", "error2"], true, "path");
|
||||
error = new yup.ValidationError(["error", "error2"], 5, "path");
|
||||
error = new yup.ValidationError(["error", "error2"], { name: "value" }, "path");
|
||||
error = new yup.ValidationError(
|
||||
["error", "error2"],
|
||||
{ name: "value" },
|
||||
"path",
|
||||
@@ -93,7 +102,7 @@ error = {
|
||||
message: "error",
|
||||
path: "path",
|
||||
errors: ["error"],
|
||||
inner: [yup.ValidationError("error", true, "path")],
|
||||
inner: [new yup.ValidationError("error", true, "path")],
|
||||
type: "date",
|
||||
value: { start: "2017-11-10" }
|
||||
};
|
||||
@@ -135,8 +144,8 @@ mixed.default(() => ({ number: 5 }));
|
||||
mixed.default();
|
||||
mixed.nullable(true);
|
||||
mixed.required();
|
||||
mixed.required('Foo');
|
||||
mixed.required(() => 'Foo');
|
||||
mixed.required("Foo");
|
||||
mixed.required(() => "Foo");
|
||||
mixed.notRequired(); // $ExpectType MixedSchema
|
||||
mixed.typeError("type error");
|
||||
mixed.typeError(() => "type error");
|
||||
@@ -155,10 +164,8 @@ mixed
|
||||
then: yup.number().min(5),
|
||||
otherwise: yup.number().min(0)
|
||||
})
|
||||
.when(
|
||||
"$other",
|
||||
(value: any, schema: MixedSchema) =>
|
||||
value === 4 ? schema.required() : schema
|
||||
.when("$other", (value: any, schema: MixedSchema) =>
|
||||
value === 4 ? schema.required() : schema
|
||||
);
|
||||
// tslint:disable-next-line:no-invalid-template-strings
|
||||
mixed.test("is-jimmy", "${path} is not Jimmy", value => value === "jimmy");
|
||||
@@ -486,8 +493,8 @@ interface ExpectedABC {
|
||||
}
|
||||
|
||||
const expectedAbc: ExpectedABC = {
|
||||
a: 'qwerty',
|
||||
b: 'asdfg',
|
||||
a: "qwerty",
|
||||
b: "asdfg",
|
||||
c: 123
|
||||
};
|
||||
const actualAbc: yup.Shape<AB, BC> = expectedAbc;
|
||||
|
||||
Reference in New Issue
Block a user