mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-30 01:47:21 +08:00
yup: added test validator context
This commit is contained in:
12
types/yup/index.d.ts
vendored
12
types/yup/index.d.ts
vendored
@@ -54,7 +54,7 @@ export interface Schema<T> {
|
||||
oneOf(arrayOfValues: any[], message?: string): this;
|
||||
notOneOf(arrayOfValues: any[], message?: string): this;
|
||||
when(keys: string | any[], builder: WhenOptions<this>): this;
|
||||
test(name: string, message: string, test: (value?: any) => boolean | Promise<boolean>, callbackStyleAsync?: boolean): this;
|
||||
test(name: string, message: string, test: (this: TestContext, value?: any) => boolean | Promise<boolean>, callbackStyleAsync?: boolean): this;
|
||||
test(options: TestOptions): this;
|
||||
transform(fn: TransformFunction<this>): this;
|
||||
}
|
||||
@@ -161,6 +161,14 @@ export type WhenOptions<T> = WhenOptionsBuilder<T>
|
||||
| { is: boolean | ((value: any) => boolean), then: any, otherwise: any }
|
||||
| object;
|
||||
|
||||
export interface TestContext {
|
||||
path: string;
|
||||
options: ValidateOptions;
|
||||
parent: any;
|
||||
schema: Schema<any>;
|
||||
createError: (params: { path: string, message: string }) => ValidationError;
|
||||
}
|
||||
|
||||
export interface ValidateOptions {
|
||||
/**
|
||||
* Only validate the input, and skip and coercion or transformation. Default - false
|
||||
@@ -193,7 +201,7 @@ export interface TestOptions {
|
||||
/**
|
||||
* Test function, determines schema validity
|
||||
*/
|
||||
test: (value: any) => boolean | Promise<boolean>;
|
||||
test: (this: TestContext, value: any) => boolean | Promise<boolean>;
|
||||
|
||||
/**
|
||||
* The validation error message
|
||||
|
||||
@@ -124,6 +124,22 @@ mixed.test({
|
||||
test: value => value == null || value.length <= 5
|
||||
});
|
||||
mixed.test('with-promise', 'It contains invalid value', value => new Promise(resolve => true));
|
||||
mixed.test('with-context', 'it uses function context', function() {
|
||||
this.options.context;
|
||||
this.path;
|
||||
this.createError({ path: '1', message: '1' });
|
||||
this.schema.meta();
|
||||
return true;
|
||||
});
|
||||
mixed.test({
|
||||
test() {
|
||||
this.options.context;
|
||||
this.path;
|
||||
this.createError({ path: '1', message: '1' });
|
||||
this.schema.meta();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
yup.string().transform(function(this, value: any, originalvalue: any) {
|
||||
return this.isType(value) && value !== null ? value.toUpperCase() : value;
|
||||
|
||||
Reference in New Issue
Block a user