From 33a50e330e8db79316bdb9ff0d00493495447579 Mon Sep 17 00:00:00 2001 From: Yash Kulshrestha Date: Fri, 11 Jan 2019 07:15:49 -0800 Subject: [PATCH 1/3] changed ValidationError class to work more like the yup source code --- types/yup/index.d.ts | 45 ++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/types/yup/index.d.ts b/types/yup/index.d.ts index 8632968433..cfd73389b6 100644 --- a/types/yup/index.d.ts +++ b/types/yup/index.d.ts @@ -8,7 +8,7 @@ // Vincent Pizzo // Robert Bullen // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.2 +// TypeScript Version: 2.8 export function reach( schema: Schema, @@ -23,12 +23,6 @@ export function addMethod>( ): void; export function ref(path: string, options?: { contextPrefix: string }): Ref; export function lazy(fn: (value: T) => Schema): 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) => string); export interface Schema { @@ -171,14 +166,19 @@ export interface ArraySchema extends Schema { compact(rejector?: (value: any) => boolean): ArraySchema; } -export type ObjectSchemaDefinition = { [field in keyof T]: Schema | Ref }; +export type ObjectSchemaDefinition = { + [field in keyof T]: Schema | 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 = { [P in keyof T]: P extends keyof U ? U[P] : T[P] } & U; +export type Shape = { + [P in keyof T]: P extends keyof U ? U[P] : T[P] +} & + U; export interface ObjectSchemaConstructor { (fields?: ObjectSchemaDefinition): ObjectSchema; @@ -191,7 +191,10 @@ export interface ObjectSchema extends Schema { noSortEdges?: Array<[string, string]> ): ObjectSchema>; from(fromKey: string, toKey: string, alias?: boolean): ObjectSchema; - noUnknown(onlyKnownKeys?: boolean, message?: TestOptionsMessage): ObjectSchema; + noUnknown( + onlyKnownKeys?: boolean, + message?: TestOptionsMessage + ): ObjectSchema; transformKeys(callback: (key: any) => any): void; camelCase(): ObjectSchema; constantCase(): ObjectSchema; @@ -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 for 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: From b25da8c8bef73dbb47312de3cfad105241c586d7 Mon Sep 17 00:00:00 2001 From: Yash Kulshrestha Date: Fri, 11 Jan 2019 07:16:25 -0800 Subject: [PATCH 2/3] added tests for ValidationError instance and static methods and properties --- types/yup/yup-tests.ts | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/types/yup/yup-tests.ts b/types/yup/yup-tests.ts index cd661a2521..361cc62084 100644 --- a/types/yup/yup-tests.ts +++ b/types/yup/yup-tests.ts @@ -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 = expectedAbc; From 4bc1eada2bbf1548e2e6c4c92c9a5b0ee679c2b2 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 18 Jan 2019 16:44:23 -0800 Subject: [PATCH 3/3] Update index.d.ts --- types/yup/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/yup/index.d.ts b/types/yup/index.d.ts index cfd73389b6..0afaf1fd9d 100644 --- a/types/yup/index.d.ts +++ b/types/yup/index.d.ts @@ -297,7 +297,7 @@ export interface SchemaDescription { // 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 for the generic Error class in +// 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 {