Skip to content

Commit

Permalink
renamed opts property of ajv instance to _opts; options object passed…
Browse files Browse the repository at this point in the history
… to constructor is copied
  • Loading branch information
epoberezkin committed Jan 20, 2016
1 parent 4fa6147 commit a8a7b2d
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 39 deletions.
43 changes: 22 additions & 21 deletions lib/ajv.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ var compileSchema = require('./compile')
, stableStringify = require('json-stable-stringify')
, formats = require('./compile/formats')
, rules = require('./compile/rules')
, v5 = require('./v5');
, v5 = require('./v5')
, util = require('./compile/util');

module.exports = Ajv;

Expand All @@ -32,11 +33,11 @@ function Ajv(opts) {
if (!(this instanceof Ajv)) return new Ajv(opts);
var self = this;

this.opts = opts || {};
this._opts = util.copy(opts) || {};
this._schemas = {};
this._refs = {};
this._formats = formats(this.opts.format);
this._cache = this.opts.cache || new Cache;
this._formats = formats(this._opts.format);
this._cache = this._opts.cache || new Cache;
this._loadingSchemas = {};
this.RULES = rules();

Expand All @@ -56,14 +57,14 @@ function Ajv(opts) {
this._compile = _compile;

addInitialSchemas();
if (this.opts.formats) addInitialFormats();
if (this._opts.formats) addInitialFormats();

if (this.opts.errorDataPath == 'property')
this.opts._errorDataPathProperty = true;
if (this._opts.errorDataPath == 'property')
this._opts._errorDataPathProperty = true;

if (this.opts.v5) v5.enable(this);
if (this._opts.v5) v5.enable(this);

this.opts.loopRequired = this.opts.loopRequired || Infinity;
this._opts.loopRequired = this._opts.loopRequired || Infinity;


/**
Expand Down Expand Up @@ -136,7 +137,7 @@ function Ajv(opts) {
* @return {Boolean}
*/
function validateSchema(schema, throwOrLogError) {
var $schema = schema.$schema || (self.opts.v5 ? v5.META_SCHEMA_ID : META_SCHEMA_ID);
var $schema = schema.$schema || (self._opts.v5 ? v5.META_SCHEMA_ID : META_SCHEMA_ID);
var currentUriFormat = self._formats.uri;
self._formats.uri = typeof currentUriFormat == 'function'
? SCHEMA_URI_FORMAT_FUNC
Expand All @@ -145,7 +146,7 @@ function Ajv(opts) {
self._formats.uri = currentUriFormat;
if (!valid && throwOrLogError) {
var message = 'schema is invalid:' + errorsText();
if (self.opts.validateSchema == 'log') console.error(message);
if (self._opts.validateSchema == 'log') console.error(message);
else throw new Error(message);
}
return valid;
Expand Down Expand Up @@ -203,12 +204,12 @@ function Ajv(opts) {
var cached = self._cache.get(jsonStr);
if (cached) return cached;

shouldAddSchema = shouldAddSchema || self.opts.addUsedSchema !== false;
shouldAddSchema = shouldAddSchema || self._opts.addUsedSchema !== false;

var id = resolve.normalizeId(schema.id);
if (id && shouldAddSchema) checkUnique(id);

if (self.opts.validateSchema !== false && !skipValidation)
if (self._opts.validateSchema !== false && !skipValidation)
validateSchema(schema, true);

var localRefs = resolve.ids.call(self, schema);
Expand Down Expand Up @@ -241,9 +242,9 @@ function Ajv(opts) {
if (schemaObj.meta) {
for (i=0; i<META_IGNORE_OPTIONS.length; i++) {
opt = META_IGNORE_OPTIONS[i];
if (self.opts[opt]) {
currentOpts[opt] = self.opts[opt];
self.opts[opt] = false;
if (self._opts[opt]) {
currentOpts[opt] = self._opts[opt];
self._opts[opt] = false;
}
}
}
Expand All @@ -255,7 +256,7 @@ function Ajv(opts) {
if (schemaObj.meta) {
for (i=0; i<META_IGNORE_OPTIONS.length; i++) {
opt = META_IGNORE_OPTIONS[i];
if (currentOpts[opt]) self.opts[opt] = currentOpts[opt];
if (currentOpts[opt]) self._opts[opt] = currentOpts[opt];
}
}
}
Expand Down Expand Up @@ -310,22 +311,22 @@ function Ajv(opts) {


function addInitialSchemas() {
if (self.opts.meta !== false) {
if (self._opts.meta !== false) {
var metaSchema = require('./refs/json-schema-draft-04.json');
addMetaSchema(metaSchema, META_SCHEMA_ID, true);
self._refs['http://json-schema.org/schema'] = META_SCHEMA_ID;
}

var optsSchemas = self.opts.schemas;
var optsSchemas = self._opts.schemas;
if (!optsSchemas) return;
if (Array.isArray(optsSchemas)) addSchema(optsSchemas);
else for (var key in optsSchemas) addSchema(optsSchemas[key], key);
}


function addInitialFormats() {
for (var name in self.opts.formats) {
var format = self.opts.formats[name];
for (var name in self._opts.formats) {
var format = self._opts.formats[name];
addFormat(name, format);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function compileAsync(schema, callback) {
if (schemaObj.validate)
setTimeout(function() { callback(null, schemaObj.validate); });
else {
if (typeof this.opts.loadSchema != 'function')
if (typeof this._opts.loadSchema != 'function')
throw new Error('options.loadSchema should be a function');
_compileAsync(schema, callback, true);
}
Expand Down Expand Up @@ -46,7 +46,7 @@ module.exports = function compileAsync(schema, callback) {
_callbacks[_callbacks.length] = schemaLoaded;
} else {
self._loadingSchemas[ref] = schemaLoaded;
self.opts.loadSchema(ref, function (err, sch) {
self._opts.loadSchema(ref, function (err, sch) {
var _callbacks = self._loadingSchemas[ref];
delete self._loadingSchemas[ref];
if (typeof _callbacks == 'function')
Expand Down
10 changes: 5 additions & 5 deletions lib/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function compile(schema, root, localRefs, baseId) {
usePattern: usePattern,
useDefault: useDefault,
useCustomRule: useCustomRule,
opts: self.opts,
opts: self._opts,
formats: formats,
self: self
});
Expand All @@ -63,8 +63,8 @@ function compile(schema, root, localRefs, baseId) {
+ vars(defaults, defaultCode) + vars(customRules, customRuleCode)
+ validateCode;

if (self.opts.beautify) {
var opts = self.opts.beautify === true ? { indent_size: 2 } : self.opts.beautify;
if (self._opts.beautify) {
var opts = self._opts.beautify === true ? { indent_size: 2 } : self._opts.beautify;
/* istanbul ignore else */
if (beautify) validateCode = beautify(validateCode, opts);
else console.error('"npm install js-beautify" to use beautify option');
Expand Down Expand Up @@ -111,7 +111,7 @@ function compile(schema, root, localRefs, baseId) {
if (!v) {
var localSchema = localRefs && localRefs[ref];
if (localSchema) {
v = resolve.inlineRef(localSchema, self.opts.inlineRefs)
v = resolve.inlineRef(localSchema, self._opts.inlineRefs)
? localSchema
: compile.call(self, localSchema, root, localRefs, baseId);
}
Expand Down Expand Up @@ -179,7 +179,7 @@ function compile(schema, root, localRefs, baseId) {
validate = compile.call(self, schema, parentSchema);
else if (macro) {
validate = macro.call(self, schema, parentSchema);
if (self.opts.validateSchema !== false) self.validateSchema(validate, true);
if (self._opts.validateSchema !== false) self.validateSchema(validate, true);
} else if (inline)
validate = inline.call(self, it, rule.keyword, schema, parentSchema);
else
Expand Down
4 changes: 2 additions & 2 deletions lib/compile/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function resolve(compile, root, ref) {

refVal = refVal || this._schemas[ref];
if (refVal instanceof SchemaObject)
return inlineRef(refVal.schema, this.opts.inlineRefs)
return inlineRef(refVal.schema, this._opts.inlineRefs)
? refVal.schema
: refVal.validate || this._compile(refVal);

Expand All @@ -38,7 +38,7 @@ function resolve(compile, root, ref) {
if (schema instanceof SchemaObject)
v = schema.validate || compile.call(this, schema.schema, root, undefined, baseId);
else if (schema)
v = inlineRef(schema, this.opts.inlineRefs)
v = inlineRef(schema, this._opts.inlineRefs)
? schema
: compile.call(this, schema, root, undefined, baseId);

Expand Down
3 changes: 0 additions & 3 deletions lib/dotjs/README.md

This file was deleted.

2 changes: 1 addition & 1 deletion lib/v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {


function enableV5(ajv) {
if (ajv.opts.meta !== false) {
if (ajv._opts.meta !== false) {
var metaSchema = require('./refs/json-schema-v5.json');
ajv.addMetaSchema(metaSchema, META_SCHEMA_ID);
}
Expand Down
2 changes: 1 addition & 1 deletion spec/custom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ describe('Custom keywords', function () {
};

var validate = ajv.compile(schema);
var numErrors = ajv.opts.allErrors ? 4 : 2;
var numErrors = ajv._opts.allErrors ? 4 : 2;

shouldBeInvalid(validate, 2, 2);
shouldBeInvalid(validate, 3, numErrors);
Expand Down
4 changes: 2 additions & 2 deletions spec/errors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('Validation errors', function () {
if (errorDataPath == 'property') {
fullValidate.errors
.filter(function(err) { return err.keyword == 'additionalProperties'; })
.map(function(err) { return fullAjv.opts.jsonPointers ? err.dataPath.substr(1) : err.dataPath.slice(2,-2); })
.map(function(err) { return fullAjv._opts.jsonPointers ? err.dataPath.substr(1) : err.dataPath.slice(2,-2); })
.forEach(function(p) { delete invalidData[p]; });

invalidData .should.eql({ foo: 1, bar: 2 });
Expand Down Expand Up @@ -500,7 +500,7 @@ describe('Validation errors', function () {
var validate = ajv.compile(schema);
shouldBeValid(validate, data);
shouldBeInvalid(validate, invalidData);
shouldBeError(validate.errors[0], 'type', schPath, ajv.opts.jsonPointers ? '/foo' : '.foo');
shouldBeError(validate.errors[0], 'type', schPath, ajv._opts.jsonPointers ? '/foo' : '.foo');
}


Expand Down
2 changes: 1 addition & 1 deletion spec/json-schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jsonSchemaTest(instances, {
],
assert: require('./chai').assert,
afterError: function (res) {
console.log('ajv options:', res.validator.opts);
console.log('ajv options:', res.validator._opts);
},
// afterEach: function (res) {
// console.log(res.errors);
Expand Down
2 changes: 1 addition & 1 deletion spec/v5.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jsonSchemaTest(instances, {
suites: testSuites(),
assert: require('./chai').assert,
afterError: function (res) {
console.log('ajv options:', res.validator.opts);
console.log('ajv options:', res.validator._opts);
},
// afterEach: function (res) {
// console.log(res.errors);
Expand Down

0 comments on commit a8a7b2d

Please sign in to comment.