Skip to content

Commit

Permalink
Merge pull request DefinitelyTyped#32067 from YashdalfTheGray/master
Browse files Browse the repository at this point in the history
@types/yup: Add a ValidationError class to match the source code
  • Loading branch information
rbuckton authored Jan 19, 2019
2 parents 1e41be9 + 4bc1ead commit 35c6461
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
45 changes: 33 additions & 12 deletions types/yup/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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;
Expand All @@ -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> {
Expand Down Expand Up @@ -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>;
Expand All @@ -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>;
Expand Down Expand Up @@ -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;
Expand All @@ -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:
Expand Down
35 changes: 21 additions & 14 deletions types/yup/yup-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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" }
};
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 35c6461

Please sign in to comment.