diff --git a/types/yup/index.d.ts b/types/yup/index.d.ts index fd572f1334..aabeb3443e 100644 --- a/types/yup/index.d.ts +++ b/types/yup/index.d.ts @@ -291,7 +291,7 @@ export interface TestContext { parent: any; schema: Schema; resolve: (value: any) => any; - createError: (params?: { path?: string; message?: string }) => ValidationError; + createError: (params?: { path?: string; message?: string, params?: object }) => ValidationError; } export interface ValidateOptions { diff --git a/types/yup/yup-tests.ts b/types/yup/yup-tests.ts index a8e2c99cc7..e633acc0cf 100644 --- a/types/yup/yup-tests.ts +++ b/types/yup/yup-tests.ts @@ -167,7 +167,11 @@ mixed .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'); -mixed.test('is-jimmy', ({ path, value }) => `${path} has an error, it is ${value}`, value => value === 'jimmy'); +mixed.test( + 'is-jimmy', + ({ path, value }) => `${path} has an error, it is ${value}`, + value => value === 'jimmy', +); mixed.test({ name: 'lessThan5', exclusive: true, @@ -213,24 +217,34 @@ yup.object({ name: yup.string() }).concat(yup.object({ when: yup.date() })); // yup.mixed().concat(yup.date()); // $ExpectType MixedSchema // Async ValidationError -const asyncValidationErrorTest = function(this: TestContext): Promise { - return new Promise(resolve => resolve(this.createError({ path: 'testPath', message: 'testMessage' }))); -}; +const asyncValidationErrorTest = (includeParams: boolean) => + function(this: TestContext): Promise { + return new Promise(resolve => + resolve( + includeParams + ? this.createError({ path: 'testPath', message: 'testMessage', params: { foo: 'bar' } }) + : this.createError(), + ), + ); + }; -mixed.test('async-validation-error', 'Returns async ValidationError', asyncValidationErrorTest); -mixed.test({ - test: asyncValidationErrorTest, -}); +mixed.test('async-validation-error', 'Returns async ValidationError', asyncValidationErrorTest(true)); +mixed.test('async-validation-error', 'Returns async ValidationError', asyncValidationErrorTest(false)); +mixed.test({ test: asyncValidationErrorTest(true) }); +mixed.test({ test: asyncValidationErrorTest(false) }); // Sync ValidationError -const syncValidationErrorTest = function(this: TestContext): ValidationError { - return this.createError({ path: 'testPath', message: 'testMessage' }); -}; +const syncValidationErrorTest = (includeParams: boolean) => + function(this: TestContext): ValidationError { + return includeParams + ? this.createError({ path: 'testPath', message: 'testMessage', params: { foo: 'bar' } }) + : this.createError(); + }; -mixed.test('sync-validation-error', 'Returns sync ValidationError', syncValidationErrorTest); -mixed.test({ - test: syncValidationErrorTest, -}); +mixed.test('sync-validation-error', 'Returns sync ValidationError', syncValidationErrorTest(true)); +mixed.test('sync-validation-error', 'Returns sync ValidationError', syncValidationErrorTest(false)); +mixed.test({ test: syncValidationErrorTest(true) }); +mixed.test({ test: syncValidationErrorTest(false) }); yup.string().transform(function(this, value: any, originalvalue: any) { return this.isType(value) && value !== null ? value.toUpperCase() : value; @@ -431,7 +445,10 @@ const description: SchemaDescription = { type: 'type', label: 'label', meta: { key: 'value' }, - tests: [{ name: 'test1', params: {} }, { name: 'test2', params: {} }], + tests: [ + { name: 'test1', params: {} }, + { name: 'test2', params: {} }, + ], fields: { key: 'value' }, };