diff options
Diffstat (limited to 'node_modules/ajv/lib/ajv.js')
-rw-r--r-- | node_modules/ajv/lib/ajv.js | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/node_modules/ajv/lib/ajv.js b/node_modules/ajv/lib/ajv.js index 14095599e..3148b8e37 100644 --- a/node_modules/ajv/lib/ajv.js +++ b/node_modules/ajv/lib/ajv.js @@ -4,7 +4,7 @@ var compileSchema = require('./compile') , resolve = require('./compile/resolve') , Cache = require('./cache') , SchemaObject = require('./compile/schema_obj') - , stableStringify = require('json-stable-stringify') + , stableStringify = require('fast-json-stable-stringify') , formats = require('./compile/formats') , rules = require('./compile/rules') , $dataMetaSchema = require('./$data') @@ -52,6 +52,7 @@ var META_SUPPORT_DATA = ['/properties']; function Ajv(opts) { if (!(this instanceof Ajv)) return new Ajv(opts); opts = this._opts = util.copy(opts) || {}; + setLogger(this); this._schemas = {}; this._refs = {}; this._fragments = {}; @@ -81,7 +82,7 @@ function Ajv(opts) { /** * 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. [fast-json-stable-stringify](https://github.com/epoberezkin/fast-json-stable-stringify) is used to serialize. * @this Ajv * @param {String|Object} schemaKeyRef key, ref or schema object * @param {Any} data to be validated @@ -125,11 +126,12 @@ function compile(schema, _meta) { * @param {String} key Optional schema key. Can be passed to `validate` method instead of schema object or id/ref. One schema per instance can have empty `id` and `key`. * @param {Boolean} _skipValidation true to skip schema validation. Used internally, option validateSchema should be used instead. * @param {Boolean} _meta true if schema is a meta-schema. Used internally, addMetaSchema should be used instead. + * @return {Ajv} this for method chaining */ function addSchema(schema, key, _skipValidation, _meta) { if (Array.isArray(schema)){ for (var i=0; i<schema.length; i++) this.addSchema(schema[i], undefined, _skipValidation, _meta); - return; + return this; } var id = this._getId(schema); if (id !== undefined && typeof id != 'string') @@ -137,6 +139,7 @@ function addSchema(schema, key, _skipValidation, _meta) { key = resolve.normalizeId(key || id); checkUnique(this, key); this._schemas[key] = this._addSchema(schema, _skipValidation, _meta, true); + return this; } @@ -147,9 +150,11 @@ function addSchema(schema, key, _skipValidation, _meta) { * @param {Object} schema schema object * @param {String} key optional schema key * @param {Boolean} skipValidation true to skip schema validation, can be used to override validateSchema option for meta-schema + * @return {Ajv} this for method chaining */ function addMetaSchema(schema, key, skipValidation) { this.addSchema(schema, key, skipValidation, true); + return this; } @@ -166,7 +171,7 @@ function validateSchema(schema, throwOrLogError) { throw new Error('$schema must be a string'); $schema = $schema || this._opts.defaultMeta || defaultMeta(this); if (!$schema) { - console.warn('meta-schema not available'); + this.logger.warn('meta-schema not available'); this.errors = null; return true; } @@ -179,7 +184,7 @@ function validateSchema(schema, throwOrLogError) { finally { this._formats.uri = currentUriFormat; } if (!valid && throwOrLogError) { var message = 'schema is invalid: ' + this.errorsText(); - if (this._opts.validateSchema == 'log') console.error(message); + if (this._opts.validateSchema == 'log') this.logger.error(message); else throw new Error(message); } return valid; @@ -246,25 +251,26 @@ function _getSchemaObj(self, keyRef) { * Even if schema is referenced by other schemas it still can be removed as other schemas have local references. * @this Ajv * @param {String|Object|RegExp} schemaKeyRef key, ref, pattern to match key/ref or schema object + * @return {Ajv} this for method chaining */ function removeSchema(schemaKeyRef) { if (schemaKeyRef instanceof RegExp) { _removeAllSchemas(this, this._schemas, schemaKeyRef); _removeAllSchemas(this, this._refs, schemaKeyRef); - return; + return this; } switch (typeof schemaKeyRef) { case 'undefined': _removeAllSchemas(this, this._schemas); _removeAllSchemas(this, this._refs); this._cache.clear(); - return; + return this; case 'string': var schemaObj = _getSchemaObj(this, schemaKeyRef); if (schemaObj) this._cache.del(schemaObj.cacheKey); delete this._schemas[schemaKeyRef]; delete this._refs[schemaKeyRef]; - return; + return this; case 'object': var serialize = this._opts.serialize; var cacheKey = serialize ? serialize(schemaKeyRef) : schemaKeyRef; @@ -276,6 +282,7 @@ function removeSchema(schemaKeyRef) { delete this._refs[id]; } } + return this; } @@ -378,15 +385,15 @@ function chooseGetId(opts) { } } - +/* @this Ajv */ function _getId(schema) { - if (schema.$id) console.warn('schema $id ignored', schema.$id); + if (schema.$id) this.logger.warn('schema $id ignored', schema.$id); return schema.id; } - +/* @this Ajv */ function _get$Id(schema) { - if (schema.id) console.warn('schema id ignored', schema.id); + if (schema.id) this.logger.warn('schema id ignored', schema.id); return schema.$id; } @@ -426,10 +433,12 @@ function errorsText(errors, options) { * @this Ajv * @param {String} name format name * @param {String|RegExp|Function} format string is converted to RegExp; function should return boolean (true when valid) + * @return {Ajv} this for method chaining */ function addFormat(name, format) { if (typeof format == 'string') format = new RegExp(format); this._formats[name] = format; + return this; } @@ -475,3 +484,19 @@ function getMetaSchemaOptions(self) { delete metaOpts[META_IGNORE_OPTIONS[i]]; return metaOpts; } + + +function setLogger(self) { + var logger = self._opts.logger; + if (logger === false) { + self.logger = {log: noop, warn: noop, error: noop}; + } else { + if (logger === undefined) logger = console; + if (!(typeof logger == 'object' && logger.log && logger.warn && logger.error)) + throw new Error('logger must implement log, warn and error methods'); + self.logger = logger; + } +} + + +function noop() {} |