refactor using options ignored for meta schemas

master
Evgeny Poberezkin 2016-03-20 18:12:14 +00:00
parent dc94106b7d
commit dc10816fc3
1 changed files with 20 additions and 20 deletions

View File

@ -59,14 +59,16 @@ function Ajv(opts) {
this._addSchema = _addSchema;
this._compile = _compile;
opts.loopRequired = opts.loopRequired || Infinity;
if (opts.async || opts.transpile) async.setup(opts);
if (opts.beautify === true) opts.beautify = { indent_size: 2 };
if (opts.errorDataPath == 'property') opts._errorDataPathProperty = true;
this._metaOpts = getMetaSchemaOptions();
addInitialSchemas();
if (opts.formats) addInitialFormats();
if (opts.errorDataPath == 'property') opts._errorDataPathProperty = true;
if (opts.v5) v5.enable(this);
if (typeof self._opts.meta == 'object') addMetaSchema(self._opts.meta);
opts.loopRequired = opts.loopRequired || Infinity;
if (opts.beautify) opts.beautify = opts.beautify === true ? { indent_size: 2 } : opts.beautify;
if (opts.async || opts.transpile) async.setup(opts);
if (typeof opts.meta == 'object') addMetaSchema(opts.meta);
/**
@ -87,7 +89,7 @@ function Ajv(opts) {
}
var valid = v(data);
if (v.async) return opts.async == '*' ? co(valid) : valid;
if (v.async) return self._opts.async == '*' ? co(valid) : valid;
self.errors = v.errors;
return valid;
}
@ -282,27 +284,17 @@ function Ajv(opts) {
}
schemaObj.compiling = true;
var currentOpts = {}, i, opt;
var currentOpts;
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;
}
}
currentOpts = self._opts;
self._opts = self._metaOpts;
}
var v;
try { v = compileSchema.call(self, schemaObj.schema, root, schemaObj.localRefs); }
finally {
schemaObj.compiling = false;
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 (schemaObj.meta) self._opts = currentOpts;
}
schemaObj.validate = v;
@ -380,4 +372,12 @@ function Ajv(opts) {
if (self._schemas[id] || self._refs[id])
throw new Error('schema with key or id "' + id + '" already exists');
}
function getMetaSchemaOptions() {
var metaOpts = util.copy(self._opts);
for (var i=0; i<META_IGNORE_OPTIONS.length; i++)
delete metaOpts[META_IGNORE_OPTIONS[i]];
return metaOpts;
}
}