feat: update type definitions

allow boolean schemas
async validation promise resolves with data
properties of custom keyword definition ($data and metaSchema)
custom keyword schema can be any value
keyword schema in error object can be any value
optional propertyName property of error object for all keywords (added when validating schema in propertyNames)
propertyNames keyword error params
master
Evgeny Poberezkin 2016-12-25 21:39:36 +00:00
parent ad7ae50c51
commit 6036f3ff07
2 changed files with 41 additions and 32 deletions

55
lib/ajv.d.ts vendored
View File

@ -8,27 +8,27 @@ declare namespace ajv {
/**
* Validate data using schema
* Schema will be compiled and cached (using serialized JSON as key. [json-stable-stringify](https://github.com/substack/json-stable-stringify) is used to serialize.
* @param {String|Object} schemaKeyRef key, ref or schema object
* @param {String|Object|Boolean} schemaKeyRef key, ref or schema object
* @param {Any} data to be validated
* @return {Boolean} validation result. Errors from the last validation will be available in `ajv.errors` (and also in compiled schema: `schema.errors`).
*/
validate(schemaKeyRef: Object | string, data: any): boolean;
validate(schemaKeyRef: Object | string | boolean, data: any): boolean | Thenable<any>;
/**
* Create validating function for passed schema.
* @param {Object} schema schema object
* @param {Object|Boolean} schema schema object
* @return {Function} validating function
*/
compile(schema: Object): ValidateFunction;
compile(schema: Object | boolean): ValidateFunction;
/**
* Creates validating function for passed schema with asynchronous loading of missing schemas.
* `loadSchema` option should be a function that accepts schema uri and node-style callback.
* @this Ajv
* @param {Object} schema schema object
* @param {Boolean} meta optional true to compile meta-schema; this parameter can be skipped
* @param {Object|Boolean} schema schema object
* @param {Boolean} meta optional true to compile meta-schema; this parameter can be skipped
* @param {Function} callback optional node-style callback, it is always called with 2 parameters: error (or null) and validating function.
* @return {Thenable<ValidateFunction>} validating function
*/
compileAsync(schema: Object, meta?: Boolean, callback?: (err: Error, validate: ValidateFunction) => any): Thenable<ValidateFunction>;
compileAsync(schema: Object | boolean, meta?: Boolean, callback?: (err: Error, validate: ValidateFunction) => any): Thenable<ValidateFunction>;
/**
* Adds schema to the instance.
* @param {Object|Array} schema schema or array of schemas. If array is passed, `key` and other parameters will be ignored.
@ -44,10 +44,10 @@ declare namespace ajv {
addMetaSchema(schema: Object, key?: string): void;
/**
* Validate schema
* @param {Object} schema schema to validate
* @param {Object|Boolean} schema schema to validate
* @return {Boolean} true if schema is valid
*/
validateSchema(schema: Object): boolean;
validateSchema(schema: Object | boolean): boolean;
/**
* Get compiled schema from the instance by `key` or `ref`.
* @param {String} keyRef `key` that was passed to `addSchema` or full schema reference (`schema.id` or resolved id).
@ -59,9 +59,9 @@ declare namespace ajv {
* If no parameter is passed all schemas but meta-schemas are removed.
* If RegExp is passed all schemas with key/id matching pattern but meta-schemas are removed.
* Even if schema is referenced by other schemas it still can be removed as other schemas have local references.
* @param {String|Object|RegExp} schemaKeyRef key, ref, pattern to match key/ref or schema object
* @param {String|Object|RegExp|Boolean} schemaKeyRef key, ref, pattern to match key/ref or schema object
*/
removeSchema(schemaKeyRef?: Object | string | RegExp): void;
removeSchema(schemaKeyRef?: Object | string | RegExp | boolean): void;
/**
* Add custom format
* @param {String} name format name
@ -109,9 +109,9 @@ declare namespace ajv {
parentData?: Object | Array<any>,
parentDataProperty?: string | number,
rootData?: Object | Array<any>
): boolean | Thenable<boolean>;
): boolean | Thenable<any>;
errors?: Array<ErrorObject>;
schema?: Object;
schema?: Object | boolean;
}
interface Options {
@ -127,7 +127,7 @@ declare namespace ajv {
schemas?: Array<Object> | Object;
missingRefs?: true | 'ignore' | 'fail';
extendRefs?: true | 'ignore' | 'fail';
loadSchema?: (uri: string, cb?: (err: Error, schema: Object) => void) => Thenable<Object>;
loadSchema?: (uri: string, cb?: (err: Error, schema: Object) => void) => Thenable<Object | boolean>;
removeAdditional?: boolean | 'all' | 'failing';
useDefaults?: boolean | 'shared';
coerceTypes?: boolean | 'array';
@ -158,25 +158,28 @@ declare namespace ajv {
interface KeywordDefinition {
type?: string | Array<string>;
async?: boolean;
$data?: boolean;
errors?: boolean | string;
metaSchema?: Object;
// 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;
validate?: SchemaValidateFunction;
compile?: (schema: any, parentSchema: Object) => ValidateFunction;
macro?: (schema: any, parentSchema: Object) => Object | boolean;
inline?: (it: Object, keyword: string, schema: any, parentSchema: Object) => string;
}
interface SchemaValidateFunction {
(
schema: Object,
schema: any,
data: any,
parentSchema?: Object,
dataPath?: string,
parentData?: Object | Array<any>,
parentDataProperty?: string | number
): boolean | Thenable<boolean>;
parentDataProperty?: string | number,
rootData?: Object | Array<any>
): boolean | Thenable<any>;
errors?: Array<ErrorObject>;
}
@ -190,10 +193,12 @@ declare namespace ajv {
dataPath: string;
schemaPath: string;
params: ErrorParameters;
// Added to validation errors of propertyNames keyword schema
propertyName?: string;
// Excluded if messages set to false.
message?: string;
// These are added with the `verbose` option.
schema?: Object;
schema?: any;
parentSchema?: Object;
data?: any;
}
@ -203,7 +208,7 @@ declare namespace ajv {
MultipleOfParams | PatternParams | RequiredParams |
TypeParams | UniqueItemsParams | CustomParams |
PatternGroupsParams | PatternRequiredParams |
SwitchParams | NoParams;
PropertyNamesParams | SwitchParams | NoParams;
interface RefParams {
ref: string;
@ -269,6 +274,10 @@ declare namespace ajv {
missingPattern: string;
}
interface PropertyNamesParams {
propertyName: string;
}
interface SwitchParams {
caseIndex: number;
}

View File

@ -96,6 +96,7 @@
additionalItems: "'should NOT have more than {{=$schema.length}} items'",
additionalProperties: "'should NOT have additional properties'",
anyOf: "'should match some schema in anyOf'",
const: "'should be equal to constant'",
dependencies: "'should have {{? $deps.length == 1 }}property {{= it.util.escapeQuotes($deps[0]) }}{{??}}properties {{= it.util.escapeQuotes($deps.join(\", \")) }}{{?}} when property {{= it.util.escapeQuotes($property) }} is present'",
'enum': "'should be equal to one of the allowed values'",
format: "'should match format \"{{#def.concatSchemaEQ}}\"'",
@ -108,15 +109,14 @@
not: "'should NOT be valid'",
oneOf: "'should match exactly one schema in oneOf'",
pattern: "'should match pattern \"{{#def.concatSchemaEQ}}\"'",
patternGroups: "'should NOT have {{=$moreOrLess}} than {{=$limit}} properties matching pattern \"{{=it.util.escapeQuotes($pgProperty)}}\"'",
propertyNames: "'property name \\'{{=$invalidName}}\\' is invalid'",
required: "'{{? it.opts._errorDataPathProperty }}is a required property{{??}}should have required property \\'{{=$missingProperty}}\\'{{?}}'",
type: "'should be {{? $typeIsArray }}{{= $typeSchema.join(\",\") }}{{??}}{{=$typeSchema}}{{?}}'",
uniqueItems: "'should NOT have duplicate items (items ## ' + j + ' and ' + i + ' are identical)'",
custom: "'should pass \"{{=$rule.keyword}}\" keyword validation'",
propertyNames: "'property name \\'{{=$invalidName}}\\' is invalid'",
patternGroups: "'should NOT have {{=$moreOrLess}} than {{=$limit}} properties matching pattern \"{{=it.util.escapeQuotes($pgProperty)}}\"'",
patternRequired: "'should have property matching pattern \\'{{=$missingPattern}}\\''",
switch: "'should pass \"switch\" keyword validation'",
const: "'should be equal to constant'",
_formatLimit: "'should be {{=$opStr}} \"{{#def.concatSchemaEQ}}\"'",
_formatExclusiveLimit: "'{{=$exclusiveKeyword}} should be boolean'"
} #}}
@ -131,6 +131,7 @@
additionalItems: "false",
additionalProperties: "false",
anyOf: "validate.schema{{=$schemaPath}}",
const: "validate.schema{{=$schemaPath}}",
dependencies: "validate.schema{{=$schemaPath}}",
'enum': "validate.schema{{=$schemaPath}}",
format: "{{#def.schemaRefOrQS}}",
@ -143,15 +144,14 @@
not: "validate.schema{{=$schemaPath}}",
oneOf: "validate.schema{{=$schemaPath}}",
pattern: "{{#def.schemaRefOrQS}}",
patternGroups: "validate.schema{{=$schemaPath}}",
propertyNames: "validate.schema{{=$schemaPath}}",
required: "validate.schema{{=$schemaPath}}",
type: "validate.schema{{=$schemaPath}}",
uniqueItems: "{{#def.schemaRefOrVal}}",
custom: "validate.schema{{=$schemaPath}}",
propertyNames: "validate.schema{{=$schemaPath}}",
patternGroups: "validate.schema{{=$schemaPath}}",
patternRequired: "validate.schema{{=$schemaPath}}",
switch: "validate.schema{{=$schemaPath}}",
const: "validate.schema{{=$schemaPath}}",
_formatLimit: "{{#def.schemaRefOrQS}}",
_formatExclusiveLimit: "validate.schema{{=$schemaPath}}"
} #}}
@ -165,6 +165,7 @@
additionalItems: "{ limit: {{=$schema.length}} }",
additionalProperties: "{ additionalProperty: '{{=$additionalProperty}}' }",
anyOf: "{}",
const: "{}",
dependencies: "{ property: '{{= it.util.escapeQuotes($property) }}', missingProperty: '{{=$missingProperty}}', depsCount: {{=$deps.length}}, deps: '{{= it.util.escapeQuotes($deps.length==1 ? $deps[0] : $deps.join(\", \")) }}' }",
'enum': "{ allowedValues: schema{{=$lvl}} }",
format: "{ format: {{#def.schemaValueQS}} }",
@ -177,15 +178,14 @@
not: "{}",
oneOf: "{}",
pattern: "{ pattern: {{#def.schemaValueQS}} }",
patternGroups: "{ reason: '{{=$reason}}', limit: {{=$limit}}, pattern: '{{=it.util.escapeQuotes($pgProperty)}}' }",
propertyNames: "{ propertyName: '{{=$invalidName}}' }",
required: "{ missingProperty: '{{=$missingProperty}}' }",
type: "{ type: '{{? $typeIsArray }}{{= $typeSchema.join(\",\") }}{{??}}{{=$typeSchema}}{{?}}' }",
uniqueItems: "{ i: i, j: j }",
custom: "{ keyword: '{{=$rule.keyword}}' }",
propertyNames: "{ propertyName: '{{=$invalidName}}' }",
patternGroups: "{ reason: '{{=$reason}}', limit: {{=$limit}}, pattern: '{{=it.util.escapeQuotes($pgProperty)}}' }",
patternRequired: "{ missingPattern: '{{=$missingPattern}}' }",
switch: "{ caseIndex: {{=$caseIndex}} }",
const: "{}",
_formatLimit: "{ comparison: {{=$opExpr}}, limit: {{#def.schemaValueQS}}, exclusive: {{=$exclusive}} }",
_formatExclusiveLimit: "{}"
} #}}