mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-07-07 18:50:14 +00:00
Add optional params key to yup createError() argument (#40834)
* Add optional params key * Add more tests
This commit is contained in:
committed by
Daniel Rosenwasser
parent
24f1d0c82d
commit
0afd3fb719
2
types/yup/index.d.ts
vendored
2
types/yup/index.d.ts
vendored
@@ -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 {
|
||||
|
||||
@@ -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' },
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user