Merge pull request #165 from epoberezkin/typescript

TypeScript type definitions - corrections
master
Evgeny Poberezkin 2016-04-20 21:20:49 +01:00
commit 254deb5a03
3 changed files with 120 additions and 72 deletions

View File

@ -737,7 +737,7 @@ Although `addSchema` does not compile schemas, explicit compilation is not requi
By default the schema is validated against meta-schema before it is added, and if the schema does not pass validation the exception is thrown. This behaviour is controlled by `validateSchema` option.
##### .addMetaSchema(Array<Object>Object schema [, String key])
##### .addMetaSchema(Array<Object>|Object schema [, String key])
Adds meta schema(s) that can be used to validate other schemas. That function should be used instead of `addSchema` because there may be instance options that would compile a meta schema incorrectly (at the moment it is `removeAdditional` option).
@ -810,6 +810,7 @@ Keyword definition is an object with the following properties:
- _macro_: macro function
- _inline_: compiling function that returns code (as string)
- _async_: an optional `true` value if the validation function is asynchronous (whether it is compiled or passed in _validate_ property); in this case it should return a promise that resolves with a value `true` or `false`. This option is ignored in case of "macro" and "inline" keywords.
- _errors_: an optional boolean indicating whether keyword returns errors. If this property is not set Ajv will determine if the errors were set in case of failed validation.
_validate_, _compile_, _macro_ and _inline_ are mutually exclusive, only one should be used at a time.

185
lib/ajv.d.ts vendored
View File

@ -2,52 +2,93 @@ declare function ajv (options?: ajv.Options): ajv.Ajv;
declare namespace ajv {
interface Ajv {
validate(schemaKeyRef: Object | string, data: any): boolean;
compile(schema: Object): ValidateFunction;
compileAsync(schema: Object, cb: (err: any, validate: ValidateFunction) => any): void;
validate(schema: Object | string, data: any): boolean;
compileAsync(schema: Object, callback: (err: Error, validate: ValidateFunction) => any): void;
addSchema(schema: Array<Object> | Object, key?: string): void;
addMetaSchema(schema: Object, key?: string): void;
validateSchema(schema: Object): boolean;
getSchema(key: string): ValidateFunction;
removeSchema(schema: Object|string);
addFormat(name: string, format: RegExp|Function|Object|string): void;
addKeyword(keyword: string, definition: Object): void;
errorsText(errors?: Array<Object>, options?: Object);
getSchema(keyRef: string): ValidateFunction;
removeSchema(schemaKeyRef?: Object | string | RegExp);
addFormat(name: string, format: FormatValidator | FormatDefinition): void;
addKeyword(keyword: string, definition: KeywordDefinition): void;
errorsText(errors?: Array<ErrorObject>, options?: ErrorsTextOptions);
}
interface ValidateFunction {
(data: Object | string): boolean;
(
data: any,
dataPath?: string,
parentData?: Object | Array<any>,
parentDataProperty?: string | number
): boolean | Promise<boolean>;
errors?: Array<ErrorObject>;
}
interface Options {
v5?: boolean;
allErrors?: boolean;
removeAdditional?: boolean;
useDefaults?: boolean;
coerceTypes?: boolean;
verbose?: boolean;
format?: string;
formats?: Object;
schemas?: Object;
meta?: boolean;
validateSchema?: boolean;
addUsedSchema?: boolean;
inlineRefs?: boolean;
loopRequired?: number;
multipleOfPrecision?: boolean;
missingRefs?: boolean;
loadSchema?: (uri, cb: (err, schema) => any) => any;
jsonPointers?: boolean;
uniqueItems?: boolean;
unicode?: boolean;
beautify?: boolean;
cache?: any;
format?: string;
formats?: Object;
schemas?: Array<Object> | Object;
missingRefs?: boolean | string;
loadSchema?: (uri: string, cb: (err, schema) => any) => any;
removeAdditional?: boolean | string;
useDefaults?: boolean | string;
coerceTypes?: boolean;
async?: boolean | string;
transpile?: string | ((code: string) => string);
meta?: boolean | Object;
validateSchema?: boolean | string;
addUsedSchema?: boolean;
inlineRefs?: boolean | number;
passContext?: boolean;
loopRequired?: number;
multipleOfPrecision?: number;
errorDataPath?: string;
jsonPointers?: boolean;
messages?: boolean;
v5?: boolean;
beautify?: boolean | Object;
cache?: Object;
}
interface ErrorsOptions {
type FormatValidator = string | RegExp | ((data: string) => boolean);
interface FormatDefinition {
validate: FormatValidator;
compare: (data1: string, data2: string) => number;
async?: boolean;
}
interface KeywordDefinition {
type?: string | Array<string>;
async?: boolean;
errors?: boolean | string;
// schema: false makes validate not to expect schema (ValidateFunction)
schema?: boolean;
// one and only one of the following properties should be present
validate?: ValidateFunction | SchemaValidateFunction;
compile?: (schema: Object, parentSchema: Object) => ValidateFunction;
macro?: (schema: Object, parentSchema: Object) => Object;
inline?: (it: Object, keyword: string, schema: Object, parentSchema: Object) => string;
}
interface SchemaValidateFunction {
(
schema: Object,
data: any,
parentSchema?: Object,
dataPath?: string,
parentData?: Object | Array<any>,
parentDataProperty?: string | number
): boolean | Promise<boolean>;
errors?: Array<ErrorObject>;
}
interface ErrorsTextOptions {
separator?: string;
dataVar?: string;
}
@ -62,81 +103,85 @@ declare namespace ajv {
// These are added with the `verbose` option.
schema?: Object;
parentSchema?: Object;
data?: Object;
data?: any;
}
interface ErrorParameters {
maxItems?: MinMaxParam;
minItems?: MinMaxParam;
maxLength?: MinMaxParam;
minLength?: MinMaxParam;
maxProperties?: MinMaxParam;
minProperties?: MinMaxParam;
additionalItems?: MinMaxParam;
additionalProperties?: AdditionalPropertyParam;
patternGroups?: PatternGroup[];
dependencies?: Dependency[];
format?: Object;
maximum?: MaximumMinimumParam;
minimum?: MaximumMinimumParam;
multipleOf?: MultipleOfParam;
pattern?: PatternParam;
required?: RequiredParam;
type?: TypeParam;
uniqueItems?: UniqueItemsParam;
$ref?: RefParam;
type ErrorParameters = RefParams | LimitParams | AdditionalPropertiesParams |
DependenciesParams | FormatParams | ComparisonParams |
MultipleOfParams | PatternParams | RequiredParams |
TypeParams | UniqueItemsParams | CustomParams |
PatternGroupsParams | PatternRequiredParams |
SwitchParams | NoParams;
interface RefParams {
ref: string;
}
interface MinMaxParam {
interface LimitParams {
limit: number;
}
interface AdditionalPropertyParam {
interface AdditionalPropertiesParams {
additionalProperty: string;
}
interface PatternGroup {
pattern: string;
reason: string;
limit: number;
}
interface Dependency {
interface DependenciesParams {
property: string;
missingProperty: string;
deps: string;
depsCount: number;
deps: string;
}
interface MaximumMinimumParam {
limit: number;
exclusive: boolean;
interface FormatParams {
format: string
}
interface ComparisonParams {
comparison: string;
limit: number | string;
exclusive: boolean;
}
interface MultipleOfParam {
multipleOf: Object;
interface MultipleOfParams {
multipleOf: number;
}
interface PatternParam {
pattern: Object;
interface PatternParams {
pattern: string;
}
interface RequiredParam {
interface RequiredParams {
missingProperty: string;
}
interface TypeParam {
interface TypeParams {
type: string;
}
interface UniqueItemsParam {
interface UniqueItemsParams {
i: number;
j: number;
}
interface RefParam {
ref: string;
interface CustomParams {
keyword: string;
}
interface PatternGroupsParams {
reason: string;
limit: number;
pattern: string;
}
interface PatternRequiredParams {
missingPattern: string;
}
interface SwitchParams {
caseIndex: number;
}
interface NoParams {}
}
export = ajv;

View File

@ -17,6 +17,7 @@
"test-fast": "AJV_FAST_TEST=true npm run test-spec",
"test-debug": "mocha spec/*.spec.js --debug-brk -R spec",
"test-cov": "istanbul cover -x '**/spec/**' node_modules/mocha/bin/_mocha -- spec/*.spec.js -R spec",
"test-ts": "tsc --target ES2015 lib/ajv.d.ts",
"bundle": "./scripts/bundle . Ajv pure_getters",
"bundle-regenerator": "./scripts/bundle regenerator",
"bundle-nodent": "./scripts/bundle nodent",
@ -24,7 +25,7 @@
"bundle-beautify": "./scripts/bundle js-beautify",
"build": "node scripts/compile-dots.js",
"test-browser": "npm run bundle-all && scripts/prepare-tests && karma start --single-run --browsers PhantomJS",
"test": "npm run jshint && npm run eslint && npm run build && npm run test-cov && npm run test-browser",
"test": "npm run jshint && npm run eslint && npm run test-ts && npm run build && npm run test-cov && npm run test-browser",
"prepublish": "npm run build && npm run bundle-all",
"watch": "watch 'npm run build' ./lib/dot"
},
@ -78,6 +79,7 @@
"pre-commit": "^1.1.1",
"regenerator": "^0.8.42",
"require-globify": "^1.3.0",
"typescript": "^1.8.10",
"uglify-js": "^2.6.1",
"watch": "^0.17.1"
}