feat(jest-validate): initial commit

This commit is contained in:
ikatyang 2017-09-08 23:11:41 +08:00
parent 0a02fbe82a
commit e8bbb05940
4 changed files with 131 additions and 0 deletions

95
types/jest-validate/index.d.ts vendored Normal file
View File

@ -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 <https://github.com/ikatyang>
// 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 };

View File

@ -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']);

View File

@ -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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }