diff --git a/types/jest-validate/index.d.ts b/types/jest-validate/index.d.ts new file mode 100644 index 0000000000..5ad3d9b4d3 --- /dev/null +++ b/types/jest-validate/index.d.ts @@ -0,0 +1,95 @@ +// Type definitions for jest-validate 21.0 +// Project: https://github.com/facebook/jest/tree/master/packages/jest-validate +// Definitions by: Ika +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +export class ValidationError extends Error { + name: string; + message: string; + constructor(name: string, message: string, comment?: string); +} + +export function createDidYouMeanMessage( + unrecognized: string, + allowedOptions: string[] +): string; + +export function format(value: any): string; + +export function logValidationWarning( + name: string, + message: string, + commant?: string +): void; + +export interface Title { + deprecation?: string; + error?: string; + warning?: string; +} + +export interface DeprecatedConfig { + [key: string]: (config: object) => string; +} + +export interface ValidationOptions { + /** + * optional string to be rendered below error/warning message. + */ + comment?: string; + /** + * an optional function with validation condition. + */ + condition?(value: any, exampleValue: any): boolean; + /** + * optional object with deprecated config keys. + */ + deprecatedConfig?: DeprecatedConfig; + /** + * the only **required** option with configuration against which you'd like to test. + */ + exampleConfig: object; + /** + * optional object of titles for errors and messages. + */ + title?: Title; + /** + * optional functions responsible for displaying warning and error messages. + */ + deprecate?( + config: object, + key: string, + deprecatedConfig: DeprecatedConfig, + options: ValidationOptions + ): boolean; + /** + * optional functions responsible for displaying warning and error messages. + */ + error?( + key: string, + received: any, + exampleValue: any, + options: ValidationOptions + ): void; + /** + * optional functions responsible for displaying warning and error messages. + */ + unknown?( + config: object, + exampleConfig: object, + key: string, + options: ValidationOptions + ): void; +} + +/** + * By default jest-validate will print generic warning and error messages. + * You can however customize this behavior by providing `options: ValidationOptions` object as a second argument: + * + * Almost anything can be overwritten to suite your needs. + */ +export function validate( + config: object, + options: ValidationOptions +): { hasDeprecationWarnings: boolean; isValid: boolean }; diff --git a/types/jest-validate/jest-validate-tests.ts b/types/jest-validate/jest-validate-tests.ts new file mode 100644 index 0000000000..28757339fe --- /dev/null +++ b/types/jest-validate/jest-validate-tests.ts @@ -0,0 +1,13 @@ +import { validate, format, createDidYouMeanMessage } from 'jest-validate'; + +validate( + { a: 0 }, + { + condition: () => false, + exampleConfig: { a: 1, b: 2 }, + }, +); + +const formatted = format({ c: 3 }); + +const didYouMeanMessage = createDidYouMeanMessage('bbb', ['aaa', 'ccc']); diff --git a/types/jest-validate/tsconfig.json b/types/jest-validate/tsconfig.json new file mode 100644 index 0000000000..942788d2f7 --- /dev/null +++ b/types/jest-validate/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": false, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "jest-validate-tests.ts" + ] +} diff --git a/types/jest-validate/tslint.json b/types/jest-validate/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/jest-validate/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }