diff --git a/types/yup/index.d.ts b/types/yup/index.d.ts index 2dcf110a9e..3fc9a33fcd 100644 --- a/types/yup/index.d.ts +++ b/types/yup/index.d.ts @@ -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 , // Vladyslav Tserman , // Moreton Bay Regional Council , // Sindre Seppola +// Yash Kulshrestha // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 -export function reach(schema: Schema, path: string, value?: any, context?: any): Schema; -export function addMethod>(schemaCtor: AnySchemaConstructor, name: string, method: (this: T, ...args: any[]) => T): void; +export function reach( + schema: Schema, + path: string, + value?: any, + context?: any +): Schema; +export function addMethod>( + schemaCtor: AnySchemaConstructor, + name: string, + method: (this: T, ...args: any[]) => T +): void; export function ref(path: string, options?: { contextPrefix: string }): Ref; export function lazy(fn: (value: T) => Schema): 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 { concat(schema: this): this; validate(value: T, options?: ValidateOptions): Promise; validateSync(value: T, options?: ValidateOptions): T; + validateAt(path: string, value: T, options?: ValidateOptions): Promise; + validateSyncAt(path: string, value: T, options?: ValidateOptions): T; isValid(value: T, options?: any): Promise; isValidSync(value: T, options?: any): boolean; cast(value: any, options?: any): T; @@ -55,29 +73,43 @@ export interface Schema { oneOf(arrayOfValues: any[], message?: string): this; notOneOf(arrayOfValues: any[], message?: string): this; when(keys: string | any[], builder: WhenOptions): this; - test(name: string, message: string, test: (this: TestContext, value?: any) => boolean | ValidationError | Promise, callbackStyleAsync?: boolean): this; + test( + name: string, + message: + | string + | ((params: object & Partial) => string), + test: ( + this: TestContext, + value?: any + ) => boolean | ValidationError | Promise, + callbackStyleAsync?: boolean + ): this; test(options: TestOptions): this; transform(fn: TransformFunction): 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 { -} +export interface MixedSchema extends Schema {} export interface StringSchemaConstructor { (): StringSchema; - new(): StringSchema; + new (): StringSchema; } export interface StringSchema extends Schema { 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 { export interface NumberSchemaConstructor { (): NumberSchema; - new(): NumberSchema; + new (): NumberSchema; } export interface NumberSchema extends Schema { @@ -105,16 +137,15 @@ export interface NumberSchema extends Schema { export interface BooleanSchemaConstructor { (): BooleanSchema; - new(): BooleanSchema; + new (): BooleanSchema; } // tslint:disable-next-line:no-empty-interface -export interface BooleanSchema extends Schema { -} +export interface BooleanSchema extends Schema {} export interface DateSchemaConstructor { (): DateSchema; - new(): DateSchema; + new (): DateSchema; } export interface DateSchema extends Schema { @@ -124,7 +155,7 @@ export interface DateSchema extends Schema { export interface ArraySchemaConstructor { (schema?: Schema): ArraySchema; - new(): ArraySchema<{}>; + new (): ArraySchema<{}>; } export interface ArraySchema extends Schema { @@ -137,11 +168,14 @@ export interface ArraySchema extends Schema { export interface ObjectSchemaConstructor { (fields?: { [field in keyof T]: Schema }): ObjectSchema; - new(): ObjectSchema<{}>; + new (): ObjectSchema<{}>; } export interface ObjectSchema extends Schema { - shape(fields: { [field in keyof T]: Schema }, noSortEdges?: Array<[string, string]>): ObjectSchema; + shape( + fields: { [field in keyof T]: Schema }, + 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; @@ -149,7 +183,11 @@ export interface ObjectSchema extends Schema { constantCase(): ObjectSchema; } -export type TransformFunction = ((this: T, value: any, originalValue: any) => any); +export type TransformFunction = (( + this: T, + value: any, + originalValue: any +) => any); export interface WhenOptionsBuilder { (value: any, schema: T): T; @@ -158,8 +196,9 @@ export interface WhenOptionsBuilder { (v1: any, v2: any, v3: any, v4: any, schema: T): T; } -export type WhenOptions = WhenOptionsBuilder - | { is: boolean | ((value: any) => boolean), then: any, otherwise: any } +export type WhenOptions = + | WhenOptionsBuilder + | { 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; - 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; + test: ( + this: TestContext, + value: any + ) => boolean | ValidationError | Promise; /** * The validation error message */ - message?: string; + message?: + | string + | ((params: object & Partial) => 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 { -} +export interface Lazy extends Schema {} export interface LocaleObject { mixed?: { [key in keyof MixedSchema]?: string }; diff --git a/types/yup/yup-tests.ts b/types/yup/yup-tests.ts index c6f367a043..31b2757ec0 100644 --- a/types/yup/yup-tests.ts +++ b/types/yup/yup-tests.ts @@ -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(yup.number, 'minimum', function(this, minValue: number, message: string) { +yup.addMethod(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 = 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 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 { - return new Promise(resolve => resolve(this.createError({ path: "testPath", message: "testMessage" }))); +const asyncValidationErrorTest = function( + this: TestContext +): Promise { + 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 // 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({ 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 + subFields: yup + .object({ + testField: yup.string().required() + }) + .required(), + arrayField: yup.array(yup.string()).required() // $ExpectType ArraySchema }); 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({ 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({ 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({ stringField: yup.string().required(), numberField: yup.number().required(), - arrayField: yup.array(yup.string()).required(), + arrayField: yup.array(yup.string()).required() }); // $ExpectError yup.object({ 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() });