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 * 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. * 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 * @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`). * @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. * Create validating function for passed schema.
* @param {Object} schema schema object * @param {Object|Boolean} schema schema object
* @return {Function} validating function * @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. * 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. * `loadSchema` option should be a function that accepts schema uri and node-style callback.
* @this Ajv * @this Ajv
* @param {Object} schema schema object * @param {Object|Boolean} schema schema object
* @param {Boolean} meta optional true to compile meta-schema; this parameter can be skipped * @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. * @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 * @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. * 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. * @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; addMetaSchema(schema: Object, key?: string): void;
/** /**
* Validate schema * Validate schema
* @param {Object} schema schema to validate * @param {Object|Boolean} schema schema to validate
* @return {Boolean} true if schema is valid * @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`. * 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). * @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 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. * 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. * 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 * Add custom format
* @param {String} name format name * @param {String} name format name
@ -109,9 +109,9 @@ declare namespace ajv {
parentData?: Object | Array<any>, parentData?: Object | Array<any>,
parentDataProperty?: string | number, parentDataProperty?: string | number,
rootData?: Object | Array<any> rootData?: Object | Array<any>
): boolean | Thenable<boolean>; ): boolean | Thenable<any>;
errors?: Array<ErrorObject>; errors?: Array<ErrorObject>;
schema?: Object; schema?: Object | boolean;
} }
interface Options { interface Options {
@ -127,7 +127,7 @@ declare namespace ajv {
schemas?: Array<Object> | Object; schemas?: Array<Object> | Object;
missingRefs?: true | 'ignore' | 'fail'; missingRefs?: true | 'ignore' | 'fail';
extendRefs?: 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'; removeAdditional?: boolean | 'all' | 'failing';
useDefaults?: boolean | 'shared'; useDefaults?: boolean | 'shared';
coerceTypes?: boolean | 'array'; coerceTypes?: boolean | 'array';
@ -158,25 +158,28 @@ declare namespace ajv {
interface KeywordDefinition { interface KeywordDefinition {
type?: string | Array<string>; type?: string | Array<string>;
async?: boolean; async?: boolean;
$data?: boolean;
errors?: boolean | string; errors?: boolean | string;
metaSchema?: Object;
// schema: false makes validate not to expect schema (ValidateFunction) // schema: false makes validate not to expect schema (ValidateFunction)
schema?: boolean; schema?: boolean;
// one and only one of the following properties should be present // one and only one of the following properties should be present
validate?: ValidateFunction | SchemaValidateFunction; validate?: SchemaValidateFunction;
compile?: (schema: Object, parentSchema: Object) => ValidateFunction; compile?: (schema: any, parentSchema: Object) => ValidateFunction;
macro?: (schema: Object, parentSchema: Object) => Object; macro?: (schema: any, parentSchema: Object) => Object | boolean;
inline?: (it: Object, keyword: string, schema: Object, parentSchema: Object) => string; inline?: (it: Object, keyword: string, schema: any, parentSchema: Object) => string;
} }
interface SchemaValidateFunction { interface SchemaValidateFunction {
( (
schema: Object, schema: any,
data: any, data: any,
parentSchema?: Object, parentSchema?: Object,
dataPath?: string, dataPath?: string,
parentData?: Object | Array<any>, parentData?: Object | Array<any>,
parentDataProperty?: string | number parentDataProperty?: string | number,
): boolean | Thenable<boolean>; rootData?: Object | Array<any>
): boolean | Thenable<any>;
errors?: Array<ErrorObject>; errors?: Array<ErrorObject>;
} }
@ -190,10 +193,12 @@ declare namespace ajv {
dataPath: string; dataPath: string;
schemaPath: string; schemaPath: string;
params: ErrorParameters; params: ErrorParameters;
// Added to validation errors of propertyNames keyword schema
propertyName?: string;
// Excluded if messages set to false. // Excluded if messages set to false.
message?: string; message?: string;
// These are added with the `verbose` option. // These are added with the `verbose` option.
schema?: Object; schema?: any;
parentSchema?: Object; parentSchema?: Object;
data?: any; data?: any;
} }
@ -203,7 +208,7 @@ declare namespace ajv {
MultipleOfParams | PatternParams | RequiredParams | MultipleOfParams | PatternParams | RequiredParams |
TypeParams | UniqueItemsParams | CustomParams | TypeParams | UniqueItemsParams | CustomParams |
PatternGroupsParams | PatternRequiredParams | PatternGroupsParams | PatternRequiredParams |
SwitchParams | NoParams; PropertyNamesParams | SwitchParams | NoParams;
interface RefParams { interface RefParams {
ref: string; ref: string;
@ -269,6 +274,10 @@ declare namespace ajv {
missingPattern: string; missingPattern: string;
} }
interface PropertyNamesParams {
propertyName: string;
}
interface SwitchParams { interface SwitchParams {
caseIndex: number; caseIndex: number;
} }

View File

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