mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-03-26 10:14:26 +08:00
* added types for yup@0.26 * added types for is-docker * Revert "added types for is-docker" This reverts commit 753dac6d555112d294caf27c931b1e8cbb79045c.
This commit is contained in:
104
types/yup/index.d.ts
vendored
104
types/yup/index.d.ts
vendored
@@ -1,17 +1,32 @@
|
||||
// Type definitions for yup 0.24
|
||||
// Type definitions for yup 0.26
|
||||
// Project: https://github.com/jquense/yup
|
||||
// Definitions by: Dominik Hardtke <https://github.com/dhardtke>,
|
||||
// Vladyslav Tserman <https://github.com/vtserman>,
|
||||
// Moreton Bay Regional Council <https://github.com/MoretonBayRC>,
|
||||
// Sindre Seppola <https://github.com/sseppola>
|
||||
// Yash Kulshrestha <https://github.com/YashdalfTheGray>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.2
|
||||
|
||||
export function reach<T>(schema: Schema<T>, path: string, value?: any, context?: any): Schema<T>;
|
||||
export function addMethod<T extends Schema<any>>(schemaCtor: AnySchemaConstructor, name: string, method: (this: T, ...args: any[]) => T): void;
|
||||
export function reach<T>(
|
||||
schema: Schema<T>,
|
||||
path: string,
|
||||
value?: any,
|
||||
context?: any
|
||||
): Schema<T>;
|
||||
export function addMethod<T extends Schema<any>>(
|
||||
schemaCtor: AnySchemaConstructor,
|
||||
name: string,
|
||||
method: (this: T, ...args: any[]) => T
|
||||
): void;
|
||||
export function ref(path: string, options?: { contextPrefix: string }): Ref;
|
||||
export function lazy<T>(fn: (value: T) => Schema<T>): Lazy;
|
||||
export function ValidationError(errors: string | string[], value: any, path: string, type?: any): ValidationError;
|
||||
export function ValidationError(
|
||||
errors: string | string[],
|
||||
value: any,
|
||||
path: string,
|
||||
type?: any
|
||||
): ValidationError;
|
||||
export function setLocale(customLocale: LocaleObject): void;
|
||||
|
||||
export const mixed: MixedSchemaConstructor;
|
||||
@@ -23,7 +38,8 @@ export const date: DateSchemaConstructor;
|
||||
export const array: ArraySchemaConstructor;
|
||||
export const object: ObjectSchemaConstructor;
|
||||
|
||||
export type AnySchemaConstructor = MixedSchemaConstructor
|
||||
export type AnySchemaConstructor =
|
||||
| MixedSchemaConstructor
|
||||
| StringSchemaConstructor
|
||||
| NumberSchemaConstructor
|
||||
| BooleanSchemaConstructor
|
||||
@@ -40,6 +56,8 @@ export interface Schema<T> {
|
||||
concat(schema: this): this;
|
||||
validate(value: T, options?: ValidateOptions): Promise<T>;
|
||||
validateSync(value: T, options?: ValidateOptions): T;
|
||||
validateAt(path: string, value: T, options?: ValidateOptions): Promise<T>;
|
||||
validateSyncAt(path: string, value: T, options?: ValidateOptions): T;
|
||||
isValid(value: T, options?: any): Promise<boolean>;
|
||||
isValidSync(value: T, options?: any): boolean;
|
||||
cast(value: any, options?: any): T;
|
||||
@@ -55,29 +73,43 @@ 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: (this: TestContext, value?: any) => boolean | ValidationError | Promise<boolean | ValidationError>, callbackStyleAsync?: boolean): this;
|
||||
test(
|
||||
name: string,
|
||||
message:
|
||||
| string
|
||||
| ((params: object & Partial<TestMessageParams>) => string),
|
||||
test: (
|
||||
this: TestContext,
|
||||
value?: any
|
||||
) => boolean | ValidationError | Promise<boolean | ValidationError>,
|
||||
callbackStyleAsync?: boolean
|
||||
): this;
|
||||
test(options: TestOptions): this;
|
||||
transform(fn: TransformFunction<this>): this;
|
||||
}
|
||||
|
||||
export interface MixedSchemaConstructor {
|
||||
(): MixedSchema;
|
||||
new(options?: { type?: string, [key: string]: any }): MixedSchema;
|
||||
new (options?: { type?: string; [key: string]: any }): MixedSchema;
|
||||
}
|
||||
|
||||
// tslint:disable-next-line:no-empty-interface
|
||||
export interface MixedSchema extends Schema<any> {
|
||||
}
|
||||
export interface MixedSchema extends Schema<any> {}
|
||||
|
||||
export interface StringSchemaConstructor {
|
||||
(): StringSchema;
|
||||
new(): StringSchema;
|
||||
new (): StringSchema;
|
||||
}
|
||||
|
||||
export interface StringSchema extends Schema<string> {
|
||||
min(limit: number | Ref, message?: string): StringSchema;
|
||||
max(limit: number | Ref, message?: string): StringSchema;
|
||||
matches(regex: RegExp, messageOrOptions?: string | { message?: string; excludeEmptyString?: boolean }): StringSchema;
|
||||
matches(
|
||||
regex: RegExp,
|
||||
messageOrOptions?:
|
||||
| string
|
||||
| { message?: string; excludeEmptyString?: boolean }
|
||||
): StringSchema;
|
||||
email(message?: string): StringSchema;
|
||||
url(message?: string): StringSchema;
|
||||
ensure(): StringSchema;
|
||||
@@ -88,7 +120,7 @@ export interface StringSchema extends Schema<string> {
|
||||
|
||||
export interface NumberSchemaConstructor {
|
||||
(): NumberSchema;
|
||||
new(): NumberSchema;
|
||||
new (): NumberSchema;
|
||||
}
|
||||
|
||||
export interface NumberSchema extends Schema<number> {
|
||||
@@ -105,16 +137,15 @@ export interface NumberSchema extends Schema<number> {
|
||||
|
||||
export interface BooleanSchemaConstructor {
|
||||
(): BooleanSchema;
|
||||
new(): BooleanSchema;
|
||||
new (): BooleanSchema;
|
||||
}
|
||||
|
||||
// tslint:disable-next-line:no-empty-interface
|
||||
export interface BooleanSchema extends Schema<boolean> {
|
||||
}
|
||||
export interface BooleanSchema extends Schema<boolean> {}
|
||||
|
||||
export interface DateSchemaConstructor {
|
||||
(): DateSchema;
|
||||
new(): DateSchema;
|
||||
new (): DateSchema;
|
||||
}
|
||||
|
||||
export interface DateSchema extends Schema<Date> {
|
||||
@@ -124,7 +155,7 @@ export interface DateSchema extends Schema<Date> {
|
||||
|
||||
export interface ArraySchemaConstructor {
|
||||
<T>(schema?: Schema<T>): ArraySchema<T>;
|
||||
new(): ArraySchema<{}>;
|
||||
new (): ArraySchema<{}>;
|
||||
}
|
||||
|
||||
export interface ArraySchema<T> extends Schema<T[]> {
|
||||
@@ -137,11 +168,14 @@ export interface ArraySchema<T> extends Schema<T[]> {
|
||||
|
||||
export interface ObjectSchemaConstructor {
|
||||
<T>(fields?: { [field in keyof T]: Schema<T[field]> }): ObjectSchema<T>;
|
||||
new(): ObjectSchema<{}>;
|
||||
new (): ObjectSchema<{}>;
|
||||
}
|
||||
|
||||
export interface ObjectSchema<T> extends Schema<T> {
|
||||
shape(fields: { [field in keyof T]: Schema<T[field]> }, noSortEdges?: Array<[string, string]>): ObjectSchema<T>;
|
||||
shape(
|
||||
fields: { [field in keyof T]: Schema<T[field]> },
|
||||
noSortEdges?: Array<[string, string]>
|
||||
): ObjectSchema<T>;
|
||||
from(fromKey: string, toKey: string, alias?: boolean): ObjectSchema<T>;
|
||||
noUnknown(onlyKnownKeys?: boolean, message?: string): ObjectSchema<T>;
|
||||
transformKeys(callback: (key: any) => any): void;
|
||||
@@ -149,7 +183,11 @@ export interface ObjectSchema<T> extends Schema<T> {
|
||||
constantCase(): ObjectSchema<T>;
|
||||
}
|
||||
|
||||
export type TransformFunction<T> = ((this: T, value: any, originalValue: any) => any);
|
||||
export type TransformFunction<T> = ((
|
||||
this: T,
|
||||
value: any,
|
||||
originalValue: any
|
||||
) => any);
|
||||
|
||||
export interface WhenOptionsBuilder<T> {
|
||||
(value: any, schema: T): T;
|
||||
@@ -158,8 +196,9 @@ export interface WhenOptionsBuilder<T> {
|
||||
(v1: any, v2: any, v3: any, v4: any, schema: T): T;
|
||||
}
|
||||
|
||||
export type WhenOptions<T> = WhenOptionsBuilder<T>
|
||||
| { is: boolean | ((value: any) => boolean), then: any, otherwise: any }
|
||||
export type WhenOptions<T> =
|
||||
| WhenOptionsBuilder<T>
|
||||
| { is: boolean | ((value: any) => boolean); then: any; otherwise: any }
|
||||
| object;
|
||||
|
||||
export interface TestContext {
|
||||
@@ -167,7 +206,7 @@ export interface TestContext {
|
||||
options: ValidateOptions;
|
||||
parent: any;
|
||||
schema: Schema<any>;
|
||||
createError: (params: { path: string, message: string }) => ValidationError;
|
||||
createError: (params: { path: string; message: string }) => ValidationError;
|
||||
}
|
||||
|
||||
export interface ValidateOptions {
|
||||
@@ -193,6 +232,13 @@ export interface ValidateOptions {
|
||||
context?: object;
|
||||
}
|
||||
|
||||
export interface TestMessageParams {
|
||||
path: string;
|
||||
value: any;
|
||||
originalValue: any;
|
||||
label: string;
|
||||
}
|
||||
|
||||
export interface TestOptions {
|
||||
/**
|
||||
* Unique name identifying the test
|
||||
@@ -202,12 +248,17 @@ export interface TestOptions {
|
||||
/**
|
||||
* Test function, determines schema validity
|
||||
*/
|
||||
test: (this: TestContext, value: any) => boolean | ValidationError | Promise<boolean | ValidationError>;
|
||||
test: (
|
||||
this: TestContext,
|
||||
value: any
|
||||
) => boolean | ValidationError | Promise<boolean | ValidationError>;
|
||||
|
||||
/**
|
||||
* The validation error message
|
||||
*/
|
||||
message?: string;
|
||||
message?:
|
||||
| string
|
||||
| ((params: object & Partial<TestMessageParams>) => string);
|
||||
|
||||
/**
|
||||
* Values passed to message for interpolation
|
||||
@@ -253,8 +304,7 @@ export interface Ref {
|
||||
}
|
||||
|
||||
// tslint:disable-next-line:no-empty-interface
|
||||
export interface Lazy extends Schema<any> {
|
||||
}
|
||||
export interface Lazy extends Schema<any> {}
|
||||
|
||||
export interface LocaleObject {
|
||||
mixed?: { [key in keyof MixedSchema]?: string };
|
||||
|
||||
@@ -1,128 +1,170 @@
|
||||
import * as yup from 'yup';
|
||||
import * as yup from "yup";
|
||||
|
||||
// tslint:disable-next-line:no-duplicate-imports
|
||||
import { reach, date, Schema, ObjectSchema, ValidationError, MixedSchema, SchemaDescription, TestOptions, ValidateOptions, NumberSchema, TestContext } from 'yup';
|
||||
import {
|
||||
reach,
|
||||
date,
|
||||
Schema,
|
||||
ObjectSchema,
|
||||
ValidationError,
|
||||
MixedSchema,
|
||||
SchemaDescription,
|
||||
TestOptions,
|
||||
ValidateOptions,
|
||||
NumberSchema,
|
||||
TestContext
|
||||
} from "yup";
|
||||
|
||||
// reach function
|
||||
let schema = yup.object().shape({
|
||||
nested: yup.object().shape({
|
||||
arr: yup.array().of(
|
||||
yup.object().shape({ num: yup.number().max(4) })
|
||||
)
|
||||
arr: yup.array().of(yup.object().shape({ num: yup.number().max(4) }))
|
||||
})
|
||||
});
|
||||
reach(schema, 'nested.arr.num');
|
||||
reach(schema, 'nested.arr[].num');
|
||||
});
|
||||
reach(schema, "nested.arr.num");
|
||||
reach(schema, "nested.arr[].num");
|
||||
|
||||
// addMethod function
|
||||
yup.addMethod<NumberSchema>(yup.number, 'minimum', function(this, minValue: number, message: string) {
|
||||
yup.addMethod<NumberSchema>(yup.number, "minimum", function(
|
||||
this,
|
||||
minValue: number,
|
||||
message: string
|
||||
) {
|
||||
return this.min(minValue, message);
|
||||
});
|
||||
yup.addMethod(yup.date, 'newMethod', function(this: yup.DateSchema, date: Date, message?: string) {
|
||||
yup.addMethod(yup.date, "newMethod", function(
|
||||
this: yup.DateSchema,
|
||||
date: Date,
|
||||
message?: string
|
||||
) {
|
||||
return this.max(date, message);
|
||||
});
|
||||
|
||||
// ref function
|
||||
schema = yup.object().shape({
|
||||
baz: yup.ref('foo.bar'),
|
||||
baz: yup.ref("foo.bar"),
|
||||
foo: yup.object().shape({
|
||||
bar: yup.string()
|
||||
bar: yup.string()
|
||||
}),
|
||||
x: yup.ref('$x')
|
||||
x: yup.ref("$x")
|
||||
});
|
||||
|
||||
schema.cast({ foo: { bar: 'boom' } }, { context: { x: 5 } });
|
||||
schema.cast({ foo: { bar: "boom" } }, { context: { x: 5 } });
|
||||
|
||||
// lazy function
|
||||
const node: ObjectSchema<any> = yup.object().shape({
|
||||
id: yup.number(),
|
||||
child: yup.lazy(() =>
|
||||
node.default(undefined)
|
||||
)
|
||||
child: yup.lazy(() => node.default(undefined))
|
||||
});
|
||||
const renderable = yup.lazy(value => {
|
||||
switch (typeof value) {
|
||||
case 'number':
|
||||
return yup.number();
|
||||
case 'string':
|
||||
return yup.string();
|
||||
default:
|
||||
return yup.mixed();
|
||||
case "number":
|
||||
return yup.number();
|
||||
case "string":
|
||||
return yup.string();
|
||||
default:
|
||||
return yup.mixed();
|
||||
}
|
||||
});
|
||||
const renderables = yup.array().of(renderable);
|
||||
|
||||
// ValidationError
|
||||
let error: ValidationError = yup.ValidationError('error', 'value', 'path');
|
||||
error = yup.ValidationError(['error', 'error2'], true, 'path');
|
||||
error = yup.ValidationError(['error', 'error2'], 5, 'path');
|
||||
error = yup.ValidationError(['error', 'error2'], {name: 'value'}, 'path');
|
||||
error = yup.ValidationError(['error', 'error2'], {name: 'value'}, 'path', 'type');
|
||||
let error: ValidationError = yup.ValidationError("error", "value", "path");
|
||||
error = yup.ValidationError(["error", "error2"], true, "path");
|
||||
error = yup.ValidationError(["error", "error2"], 5, "path");
|
||||
error = yup.ValidationError(["error", "error2"], { name: "value" }, "path");
|
||||
error = yup.ValidationError(
|
||||
["error", "error2"],
|
||||
{ name: "value" },
|
||||
"path",
|
||||
"type"
|
||||
);
|
||||
error = {
|
||||
name: 'ValidationError',
|
||||
message: 'error',
|
||||
path: 'path',
|
||||
errors: ['error'],
|
||||
inner: [yup.ValidationError('error', true, 'path')],
|
||||
type: 'date',
|
||||
value: {start: '2017-11-10'}
|
||||
name: "ValidationError",
|
||||
message: "error",
|
||||
path: "path",
|
||||
errors: ["error"],
|
||||
inner: [yup.ValidationError("error", true, "path")],
|
||||
type: "date",
|
||||
value: { start: "2017-11-10" }
|
||||
};
|
||||
error.value = 'value';
|
||||
error.value = "value";
|
||||
error.value = true;
|
||||
error.value = 5;
|
||||
error.value = {name: 'value'};
|
||||
error.value = { name: "value" };
|
||||
error.type = {};
|
||||
error.type = [];
|
||||
error.errors = ['error'];
|
||||
error.errors = ["error"];
|
||||
|
||||
// mixed
|
||||
let mixed: MixedSchema = yup.mixed();
|
||||
mixed.clone();
|
||||
mixed.label('label');
|
||||
mixed.meta({ meta: 'value' });
|
||||
mixed.label("label");
|
||||
mixed.meta({ meta: "value" });
|
||||
mixed.describe().label;
|
||||
mixed.describe().meta;
|
||||
mixed.describe().tests;
|
||||
mixed.describe().type;
|
||||
mixed.concat(yup.string());
|
||||
mixed.validate({});
|
||||
mixed.validate({ hello: 'world' }, { strict: true }).then(value => value);
|
||||
mixed.validate({ hello: "world" }, { strict: true }).then(value => value);
|
||||
mixed.validateSync({ hello: "world" }, { strict: true });
|
||||
mixed.validateAt("path", {}, { strict: true, context: {} });
|
||||
mixed
|
||||
.validateAt("path", {}, { strict: true, context: {} })
|
||||
.then(value => value);
|
||||
mixed.validateSyncAt("path", {}, { strict: true, context: {} });
|
||||
mixed.isValid(undefined, (valid: true) => true);
|
||||
mixed.isValid({ hello: 'world' }).then(valid => valid);
|
||||
mixed.isValid({ hello: "world" }).then(valid => valid);
|
||||
mixed.cast({});
|
||||
mixed.isType('hello');
|
||||
mixed.isType("hello");
|
||||
mixed.strict(true);
|
||||
mixed.strip(true);
|
||||
mixed.withMutation(schema => {});
|
||||
mixed.default({ number: 5});
|
||||
mixed.default(() => ({ number: 5}));
|
||||
mixed.default({ number: 5 });
|
||||
mixed.default(() => ({ number: 5 }));
|
||||
mixed.default();
|
||||
mixed.nullable(true);
|
||||
mixed.required();
|
||||
mixed.notRequired(); // $ExpectType MixedSchema
|
||||
mixed.typeError('type error');
|
||||
mixed.oneOf(['hello', 'world'], 'message');
|
||||
mixed.notOneOf(['hello', 'world'], 'message');
|
||||
mixed.when('isBig', {
|
||||
mixed.typeError("type error");
|
||||
mixed.oneOf(["hello", "world"], "message");
|
||||
mixed.notOneOf(["hello", "world"], "message");
|
||||
mixed.when("isBig", {
|
||||
is: value => true,
|
||||
then: yup.number().min(5),
|
||||
otherwise: yup.number().min(0)
|
||||
});
|
||||
mixed.when('isBig', {
|
||||
is: true,
|
||||
then: yup.number().min(5),
|
||||
otherwise: yup.number().min(0)
|
||||
}).when('$other', (value: any, schema: MixedSchema) => value === 4 ? schema.required() : schema);
|
||||
mixed
|
||||
.when("isBig", {
|
||||
is: true,
|
||||
then: yup.number().min(5),
|
||||
otherwise: yup.number().min(0)
|
||||
})
|
||||
.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} is not Jimmy", value => value === "jimmy");
|
||||
mixed.test(
|
||||
"is-jimmy",
|
||||
({ path, value }) => `${path} has an error, it is ${value}`,
|
||||
value => value === "jimmy"
|
||||
);
|
||||
mixed.test({
|
||||
name: 'lessThan5',
|
||||
name: "lessThan5",
|
||||
exclusive: true,
|
||||
// tslint:disable-next-line:no-invalid-template-strings
|
||||
message: '${path} must be less than 5 characters',
|
||||
message: "${path} must be less than 5 characters",
|
||||
test: value => value == null || value.length <= 5
|
||||
});
|
||||
mixed.test('with-promise', 'It contains invalid value', value => new Promise(resolve => true));
|
||||
mixed.test(
|
||||
"with-promise",
|
||||
"It contains invalid value",
|
||||
value => new Promise(resolve => true)
|
||||
);
|
||||
const testContext = function(this: TestContext) {
|
||||
// $ExpectType string
|
||||
this.path;
|
||||
@@ -133,22 +175,30 @@ const testContext = function(this: TestContext) {
|
||||
// $ExpectType Schema<any>
|
||||
this.schema;
|
||||
// $ExpectType ValidationError
|
||||
this.createError({ path: '1', message: '1' });
|
||||
this.createError({ path: "1", message: "1" });
|
||||
return true;
|
||||
};
|
||||
mixed.test('with-context', 'it uses function context', testContext);
|
||||
mixed.test("with-context", "it uses function context", testContext);
|
||||
mixed.test({
|
||||
test: testContext
|
||||
});
|
||||
|
||||
// Async ValidationError
|
||||
const asyncValidationErrorTest = function(this: TestContext): Promise<ValidationError> {
|
||||
return new Promise(resolve => resolve(this.createError({ path: "testPath", message: "testMessage" })));
|
||||
const asyncValidationErrorTest = function(
|
||||
this: TestContext
|
||||
): Promise<ValidationError> {
|
||||
return new Promise(resolve =>
|
||||
resolve(this.createError({ path: "testPath", message: "testMessage" }))
|
||||
);
|
||||
};
|
||||
|
||||
mixed.test('async-validation-error', 'Returns async ValidationError', asyncValidationErrorTest);
|
||||
mixed.test(
|
||||
"async-validation-error",
|
||||
"Returns async ValidationError",
|
||||
asyncValidationErrorTest
|
||||
);
|
||||
mixed.test({
|
||||
test: asyncValidationErrorTest,
|
||||
test: asyncValidationErrorTest
|
||||
});
|
||||
|
||||
// Sync ValidationError
|
||||
@@ -156,9 +206,13 @@ const syncValidationErrorTest = function(this: TestContext): ValidationError {
|
||||
return this.createError({ path: "testPath", message: "testMessage" });
|
||||
};
|
||||
|
||||
mixed.test('sync-validation-error', 'Returns sync ValidationError', syncValidationErrorTest);
|
||||
mixed.test(
|
||||
"sync-validation-error",
|
||||
"Returns sync ValidationError",
|
||||
syncValidationErrorTest
|
||||
);
|
||||
mixed.test({
|
||||
test: syncValidationErrorTest,
|
||||
test: syncValidationErrorTest
|
||||
});
|
||||
|
||||
yup.string().transform(function(this, value: any, originalvalue: any) {
|
||||
@@ -171,7 +225,7 @@ mixed = new ExtendsMixed();
|
||||
|
||||
class ExtendsMixed2 extends yup.mixed {
|
||||
constructor() {
|
||||
super({ type: 'CustomType' });
|
||||
super({ type: "CustomType" });
|
||||
}
|
||||
}
|
||||
mixed = new ExtendsMixed2();
|
||||
@@ -182,25 +236,27 @@ mixed = new ExtendsMixed2();
|
||||
class DateSchema extends yup.date {
|
||||
isWednesday(message?: string): DateSchema {
|
||||
return this.clone().test({
|
||||
name: 'Wednesday',
|
||||
name: "Wednesday",
|
||||
// tslint:disable-next-line:no-invalid-template-strings
|
||||
message: message || '${path} must be Wednesday',
|
||||
test: value => true /* Check that day is Wednesday */,
|
||||
message: message || "${path} must be Wednesday",
|
||||
test: value => true /* Check that day is Wednesday */
|
||||
});
|
||||
}
|
||||
}
|
||||
yup.object().shape({
|
||||
startDate: new DateSchema().isWednesday().required()
|
||||
}).isValidSync({
|
||||
startDate: '2017-11-29',
|
||||
});
|
||||
yup.object()
|
||||
.shape({
|
||||
startDate: new DateSchema().isWednesday().required()
|
||||
})
|
||||
.isValidSync({
|
||||
startDate: "2017-11-29"
|
||||
});
|
||||
|
||||
// String schema
|
||||
const strSchema = yup.string(); // $ExpectType StringSchema
|
||||
strSchema.isValid('hello'); // => true
|
||||
strSchema.isValid("hello"); // => true
|
||||
strSchema.required();
|
||||
strSchema.min(5, 'message');
|
||||
strSchema.max(5, 'message');
|
||||
strSchema.min(5, "message");
|
||||
strSchema.max(5, "message");
|
||||
strSchema.matches(/(hi|bye)/);
|
||||
strSchema.email();
|
||||
strSchema.url();
|
||||
@@ -212,16 +268,19 @@ strSchema.uppercase();
|
||||
// Number schema
|
||||
const numSchema = yup.number(); // $ExpectType NumberSchema
|
||||
numSchema.isValid(10); // => true
|
||||
numSchema.min(5, 'message');
|
||||
numSchema.max(5, 'message');
|
||||
numSchema.min(5, "message");
|
||||
numSchema.max(5, "message");
|
||||
numSchema.positive();
|
||||
numSchema.negative();
|
||||
numSchema.lessThan(5);
|
||||
numSchema.moreThan(5);
|
||||
numSchema.integer();
|
||||
numSchema.truncate();
|
||||
numSchema.round('floor');
|
||||
numSchema.validate(5, { strict: true }).then(value => value).catch(err => err);
|
||||
numSchema.round("floor");
|
||||
numSchema
|
||||
.validate(5, { strict: true })
|
||||
.then(value => value)
|
||||
.catch(err => err);
|
||||
|
||||
// Boolean Schema
|
||||
const boolSchema = yup.boolean();
|
||||
@@ -231,17 +290,17 @@ boolSchema.isValid(true); // => true
|
||||
const dateSchema = yup.date();
|
||||
dateSchema.isValid(new Date()); // => true
|
||||
dateSchema.min(new Date());
|
||||
dateSchema.min('2017-11-12');
|
||||
dateSchema.min(new Date(), 'message');
|
||||
dateSchema.min('2017-11-12', 'message');
|
||||
dateSchema.min("2017-11-12");
|
||||
dateSchema.min(new Date(), "message");
|
||||
dateSchema.min("2017-11-12", "message");
|
||||
dateSchema.max(new Date());
|
||||
dateSchema.max('2017-11-12');
|
||||
dateSchema.max(new Date(), 'message');
|
||||
dateSchema.max('2017-11-12', 'message');
|
||||
dateSchema.max("2017-11-12");
|
||||
dateSchema.max(new Date(), "message");
|
||||
dateSchema.max("2017-11-12", "message");
|
||||
|
||||
// Array Schema
|
||||
const arrSchema = yup.array().of(yup.number().min(2));
|
||||
arrSchema.isValid([2, 3]); // => true
|
||||
arrSchema.isValid([2, 3]); // => true
|
||||
arrSchema.isValid([1, -24]); // => false
|
||||
arrSchema.required();
|
||||
arrSchema.ensure();
|
||||
@@ -254,9 +313,13 @@ yup.array().of(yup.string()); // $ExpectType ArraySchema<string>
|
||||
// Object Schema
|
||||
const objSchema = yup.object().shape({
|
||||
name: yup.string().required(),
|
||||
age: yup.number().required().positive().integer(),
|
||||
age: yup
|
||||
.number()
|
||||
.required()
|
||||
.positive()
|
||||
.integer(),
|
||||
email: yup.string().email(),
|
||||
website: yup.string().url(),
|
||||
website: yup.string().url()
|
||||
});
|
||||
yup.object().shape({
|
||||
num: yup.number()
|
||||
@@ -266,35 +329,35 @@ yup.object({
|
||||
num: yup.number()
|
||||
});
|
||||
|
||||
objSchema.from('prop', 'myProp');
|
||||
objSchema.from('prop', 'myProp', true);
|
||||
objSchema.from("prop", "myProp");
|
||||
objSchema.from("prop", "myProp", true);
|
||||
objSchema.noUnknown();
|
||||
objSchema.noUnknown(true);
|
||||
objSchema.noUnknown(true, 'message');
|
||||
objSchema.noUnknown(true, "message");
|
||||
objSchema.transformKeys(key => key.toUpperCase());
|
||||
objSchema.camelCase();
|
||||
objSchema.constantCase();
|
||||
|
||||
const description: SchemaDescription = {
|
||||
type: 'type',
|
||||
label: 'label',
|
||||
meta: { key: 'value' },
|
||||
tests: ['test1', 'test2']
|
||||
type: "type",
|
||||
label: "label",
|
||||
meta: { key: "value" },
|
||||
tests: ["test1", "test2"]
|
||||
};
|
||||
|
||||
const testOptions: TestOptions = {
|
||||
name: 'name',
|
||||
name: "name",
|
||||
test: value => true,
|
||||
message: 'validation error message',
|
||||
params: { param1: 'value'},
|
||||
message: "validation error message",
|
||||
params: { param1: "value" },
|
||||
exclusive: true
|
||||
};
|
||||
|
||||
const testOptionsWithPromise: TestOptions = {
|
||||
name: 'name',
|
||||
name: "name",
|
||||
test: value => new Promise(resolve => true),
|
||||
message: 'validation error message',
|
||||
params: { param1: 'value'},
|
||||
message: "validation error message",
|
||||
params: { param1: "value" },
|
||||
exclusive: true
|
||||
};
|
||||
|
||||
@@ -304,13 +367,13 @@ const validateOptions: ValidateOptions = {
|
||||
stripUnknown: true,
|
||||
recursive: true,
|
||||
context: {
|
||||
key: 'value'
|
||||
key: "value"
|
||||
}
|
||||
};
|
||||
|
||||
yup.setLocale({
|
||||
number: { max: "Max message", min: "Min message" },
|
||||
string: { email: "String message"}
|
||||
string: { email: "String message" }
|
||||
});
|
||||
|
||||
interface MyInterface {
|
||||
@@ -328,10 +391,12 @@ interface SubInterface {
|
||||
const typedSchema = yup.object<MyInterface>({
|
||||
stringField: yup.string().required(), // $ExpectType StringSchema
|
||||
numberField: yup.number().required(), // $ExpectType NumberSchema
|
||||
subFields: yup.object({
|
||||
testField: yup.string().required(),
|
||||
}).required(),
|
||||
arrayField: yup.array(yup.string()).required(), // $ExpectType ArraySchema<string>
|
||||
subFields: yup
|
||||
.object({
|
||||
testField: yup.string().required()
|
||||
})
|
||||
.required(),
|
||||
arrayField: yup.array(yup.string()).required() // $ExpectType ArraySchema<string>
|
||||
});
|
||||
|
||||
const testObject: MyInterface = {
|
||||
@@ -340,7 +405,7 @@ const testObject: MyInterface = {
|
||||
subFields: {
|
||||
testField: "test2"
|
||||
},
|
||||
arrayField: ["hi"],
|
||||
arrayField: ["hi"]
|
||||
};
|
||||
|
||||
typedSchema.validateSync(testObject); // $ExpectType MyInterface
|
||||
@@ -348,35 +413,41 @@ typedSchema.validateSync(testObject); // $ExpectType MyInterface
|
||||
// $ExpectError
|
||||
yup.object<MyInterface>({
|
||||
stringField: yup.string().required(),
|
||||
subFields: yup.object({
|
||||
testField: yup.string().required(),
|
||||
}).required(),
|
||||
arrayField: yup.array(yup.string()).required(),
|
||||
subFields: yup
|
||||
.object({
|
||||
testField: yup.string().required()
|
||||
})
|
||||
.required(),
|
||||
arrayField: yup.array(yup.string()).required()
|
||||
});
|
||||
|
||||
// $ExpectError
|
||||
yup.object<MyInterface>({
|
||||
stringField: yup.number().required(),
|
||||
numberField: yup.number().required(),
|
||||
subFields: yup.object({
|
||||
testField: yup.string().required(),
|
||||
}).required(),
|
||||
arrayField: yup.array(yup.string()).required(),
|
||||
subFields: yup
|
||||
.object({
|
||||
testField: yup.string().required()
|
||||
})
|
||||
.required(),
|
||||
arrayField: yup.array(yup.string()).required()
|
||||
});
|
||||
|
||||
// $ExpectError
|
||||
yup.object<MyInterface>({
|
||||
stringField: yup.string().required(),
|
||||
numberField: yup.number().required(),
|
||||
arrayField: yup.array(yup.string()).required(),
|
||||
arrayField: yup.array(yup.string()).required()
|
||||
});
|
||||
|
||||
// $ExpectError
|
||||
yup.object<MyInterface>({
|
||||
stringField: yup.string().required(),
|
||||
numberField: yup.number().required(),
|
||||
subFields: yup.object({
|
||||
testField: yup.number().required(),
|
||||
}).required(),
|
||||
arrayField: yup.array(yup.string()).required(),
|
||||
subFields: yup
|
||||
.object({
|
||||
testField: yup.number().required()
|
||||
})
|
||||
.required(),
|
||||
arrayField: yup.array(yup.string()).required()
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user