Add optional params key to yup createError() argument (#40834)

* Add optional params key

* Add more tests
This commit is contained in:
ericbiewener
2019-12-19 22:35:56 -08:00
committed by Daniel Rosenwasser
parent 24f1d0c82d
commit 0afd3fb719
2 changed files with 34 additions and 17 deletions

View File

@@ -291,7 +291,7 @@ export interface TestContext {
parent: any;
schema: Schema<any>;
resolve: (value: any) => any;
createError: (params?: { path?: string; message?: string }) => ValidationError;
createError: (params?: { path?: string; message?: string, params?: object }) => ValidationError;
}
export interface ValidateOptions {

View File

@@ -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<string>().concat(yup.date()); // $ExpectType MixedSchema<string | Date>
// Async ValidationError
const asyncValidationErrorTest = function(this: TestContext): Promise<ValidationError> {
return new Promise(resolve => resolve(this.createError({ path: 'testPath', message: 'testMessage' })));
};
const asyncValidationErrorTest = (includeParams: boolean) =>
function(this: TestContext): Promise<ValidationError> {
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' },
};