Add missing properties, refactor Schema.when

This commit is contained in:
Vladyslav Tserman
2017-11-29 16:10:57 -08:00
parent 074627c1f9
commit cfd43ded94
3 changed files with 17 additions and 5 deletions
+4 -3
View File
@@ -4,8 +4,6 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
export as namespace yup;
export function reach(schema: Schema, path: string, value?: any, context?: any): Schema;
export function addMethod(schemaType: Schema, name: string, method: () => Schema): void;
export function ref(path: string, options: { contextPrefix: string }): Ref;
@@ -42,9 +40,11 @@ export interface Schema {
clone(): Schema;
label(label: string): Schema;
meta(metadata: any): Schema;
meta(): any;
describe(): SchemaDescription;
concat(schema: Schema): Schema;
validate(value: any, options?: ValidateOptions, callback?: () => void): Promise<any>;
validateSync(value: any, options?: ValidateOptions): any;
isValid(value: any, options?: any, callback?: () => void): Promise<boolean>;
isValidSync(value: any, options?: any): boolean;
cast(value: any): any;
@@ -59,7 +59,7 @@ export interface Schema {
typeError(message?: string): Schema;
oneOf(arrayOfValues: any[], message?: string): Schema;
notOneOf(arrayOfValues: any[], message?: string): Schema;
when(keys: string | any[], builder: any | ((value: any, schema: Schema) => Schema)): Schema;
when(keys: string | any[], builder: ((value: any, schema: Schema) => Schema) | object): Schema;
test(name: string, message: string, test: (value: any) => boolean, callbackStyleAsync?: boolean): Schema;
test(options: TestOptions): Schema;
transform(transformation: (currentValue: any, originalValue: any) => any): Schema;
@@ -140,6 +140,7 @@ export interface ObjectSchema extends Schema {
shape(fields: any, noSortEdges?: Array<[string, string]>): ObjectSchema;
from(fromKey: string, toKey: string, alias: boolean): ObjectSchema;
noUnknown(onlyKnownKeys: boolean, message?: string): ObjectSchema;
transformKeys(callback: (key: any) => any): void
camelCase(): ObjectSchema;
constantCase(): ObjectSchema;
}
+2 -2
View File
@@ -1,7 +1,7 @@
{
{
"extends": "dtslint/dt.json",
"rules": {
"no-empty-interface": false,
"no-any-union": false
"no-any-union": true
}
}
+11
View File
@@ -33,3 +33,14 @@ yup.object().shape({
const valid = schema.isValidSync({
startDate: '2017-11-29',
});
// test when
yup.object().shape({
checkWednesday: yup.bool().required(),
name: yup.string().required(),
startDate: date()
.required()
.when(['checkWednesday', 'name'], (checkWednesday: boolean, name: string, schema: CustomDateSchema) => {
return checkWednesday ? schema.isWednesday() : schema;
})
});