From 73095f3f18378bd49c311652cbc7af7d889fddba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dra=C5=BEen=20Ten=C5=BEera?= Date: Wed, 15 Aug 2018 14:16:09 +0200 Subject: [PATCH] [yup] Add ValidationError as a valid return type for the test function --- types/yup/index.d.ts | 4 ++-- types/yup/yup-tests.ts | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/types/yup/index.d.ts b/types/yup/index.d.ts index b4c8ea797e..2dcf110a9e 100644 --- a/types/yup/index.d.ts +++ b/types/yup/index.d.ts @@ -55,7 +55,7 @@ export interface Schema { oneOf(arrayOfValues: any[], message?: string): this; notOneOf(arrayOfValues: any[], message?: string): this; when(keys: string | any[], builder: WhenOptions): this; - test(name: string, message: string, test: (this: TestContext, value?: any) => boolean | Promise, callbackStyleAsync?: boolean): this; + test(name: string, message: string, test: (this: TestContext, value?: any) => boolean | ValidationError | Promise, callbackStyleAsync?: boolean): this; test(options: TestOptions): this; transform(fn: TransformFunction): this; } @@ -202,7 +202,7 @@ export interface TestOptions { /** * Test function, determines schema validity */ - test: (this: TestContext, value: any) => boolean | Promise; + test: (this: TestContext, value: any) => boolean | ValidationError | Promise; /** * The validation error message diff --git a/types/yup/yup-tests.ts b/types/yup/yup-tests.ts index 2d5b509b37..c6f367a043 100644 --- a/types/yup/yup-tests.ts +++ b/types/yup/yup-tests.ts @@ -141,6 +141,26 @@ mixed.test({ test: testContext }); +// Async ValidationError +const asyncValidationErrorTest = function(this: TestContext): Promise { + return new Promise(resolve => resolve(this.createError({ path: "testPath", message: "testMessage" }))); +}; + +mixed.test('async-validation-error', 'Returns async ValidationError', asyncValidationErrorTest); +mixed.test({ + test: asyncValidationErrorTest, +}); + +// Sync ValidationError +const syncValidationErrorTest = function(this: TestContext): ValidationError { + return this.createError({ path: "testPath", message: "testMessage" }); +}; + +mixed.test('sync-validation-error', 'Returns sync ValidationError', syncValidationErrorTest); +mixed.test({ + test: syncValidationErrorTest, +}); + yup.string().transform(function(this, value: any, originalvalue: any) { return this.isType(value) && value !== null ? value.toUpperCase() : value; });