parent
f57b3b96bd
commit
41ed37f36f
|
@ -17,55 +17,62 @@ function compile(schema, root, localRefs) {
|
|||
, refVal = [ undefined ]
|
||||
, refs = {};
|
||||
|
||||
var isRoot = !root || (root && root.schema == schema);
|
||||
root = root || { schema: schema, refVal: refVal, refs: refs };
|
||||
var rootRefs = root.refs;
|
||||
var rootRefVal = root.refVal;
|
||||
|
||||
var formats = this._formats;
|
||||
|
||||
var validateCode = validateGenerator({
|
||||
isTop: true,
|
||||
schema: schema,
|
||||
isRoot: isRoot,
|
||||
root: root,
|
||||
schemaPath: '',
|
||||
errorPath: '""',
|
||||
dataPath: '',
|
||||
RULES: RULES,
|
||||
validate: validateGenerator,
|
||||
util: util,
|
||||
resolve: resolve,
|
||||
resolveRef: resolveRef,
|
||||
opts: this.opts,
|
||||
formats: formats
|
||||
});
|
||||
return localCompile(schema, root, localRefs);
|
||||
|
||||
if (this.opts.beautify) {
|
||||
var opts = this.opts.beautify === true ? { indent_size: 2 } : this.opts.beautify;
|
||||
if (beautify) validateCode = beautify(validateCode, opts);
|
||||
else console.error('"npm install js-beautify" to use beautify option');
|
||||
}
|
||||
// console.log('\n\n\n *** \n', validateCode);
|
||||
var validate;
|
||||
try {
|
||||
eval(validateCode);
|
||||
refVal[0] = validate;
|
||||
} catch(e) {
|
||||
console.log('Error compiling schema, function code:', validateCode);
|
||||
throw e;
|
||||
|
||||
function localCompile(_schema, _root, localRefs) {
|
||||
var isRoot = !_root || (_root && _root.schema == _schema);
|
||||
if (_root.schema != root.schema)
|
||||
return compile.call(self, _schema, _root, localRefs);
|
||||
|
||||
var validateCode = validateGenerator({
|
||||
isTop: true,
|
||||
schema: _schema,
|
||||
isRoot: isRoot,
|
||||
root: _root,
|
||||
schemaPath: '',
|
||||
errorPath: '""',
|
||||
dataPath: '',
|
||||
RULES: RULES,
|
||||
validate: validateGenerator,
|
||||
util: util,
|
||||
resolve: resolve,
|
||||
resolveRef: resolveRef,
|
||||
opts: self.opts,
|
||||
formats: formats
|
||||
});
|
||||
|
||||
if (self.opts.beautify) {
|
||||
var opts = self.opts.beautify === true ? { indent_size: 2 } : self.opts.beautify;
|
||||
if (beautify) validateCode = beautify(validateCode, opts);
|
||||
else console.error('"npm install js-beautify" to use beautify option');
|
||||
}
|
||||
// console.log('\n\n\n *** \n', validateCode);
|
||||
var validate;
|
||||
try {
|
||||
eval(validateCode);
|
||||
refVal[0] = validate;
|
||||
} catch(e) {
|
||||
console.log('Error compiling schema, function code:', validateCode);
|
||||
throw e;
|
||||
}
|
||||
|
||||
validate.schema = _schema;
|
||||
validate.errors = null;
|
||||
validate.refs = refs;
|
||||
validate.refVal = refVal;
|
||||
validate.root = isRoot ? validate : _root;
|
||||
|
||||
return validate;
|
||||
}
|
||||
|
||||
validate.schema = schema;
|
||||
validate.errors = null;
|
||||
validate.refs = refs;
|
||||
validate.refVal = refVal;
|
||||
validate.root = isRoot ? validate : root;
|
||||
|
||||
return validate;
|
||||
|
||||
|
||||
function resolveRef(baseId, ref) {
|
||||
function resolveRef(baseId, ref, isRoot) {
|
||||
ref = resolve.url(baseId, ref);
|
||||
if (refs[ref]) return 'refVal[' + refs[ref] + ']';
|
||||
if (!isRoot) {
|
||||
|
@ -75,7 +82,7 @@ function compile(schema, root, localRefs) {
|
|||
}
|
||||
|
||||
var refCode = addLocalRef(ref, compiledRef);
|
||||
var v = resolve.call(self, compile, root, ref);
|
||||
var v = resolve.call(self, localCompile, root, ref);
|
||||
if (!v) {
|
||||
var localSchema = localRefs[ref];
|
||||
if (localSchema) v = compile.call(self, localSchema, root, localRefs);
|
||||
|
@ -111,6 +118,4 @@ function compile(schema, root, localRefs) {
|
|||
* Functions below are used inside compiled validations function
|
||||
*/
|
||||
|
||||
var getProperty = util.getProperty
|
||||
, ucs2length = util.ucs2length
|
||||
, stableStringify = util.stableStringify;
|
||||
var ucs2length = util.ucs2length;
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
{{# def.validateRef:'rootRefVal[0]' }}
|
||||
{{?}}
|
||||
{{??}}
|
||||
{{ var $refVal = it.resolveRef(it.baseId, $schema, it.rootId); }}
|
||||
{{ var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot); }}
|
||||
{{? $refVal === undefined }}
|
||||
{{ var $message = 'can\'t resolve reference ' + $schema + ' from id ' + it.baseId; }}
|
||||
{{? it.opts.missingRefs == 'fail' }}
|
||||
|
|
|
@ -29,7 +29,7 @@ module.exports = function anonymous(it) {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
var $refVal = it.resolveRef(it.baseId, $schema, it.rootId);
|
||||
var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot);
|
||||
if ($refVal === undefined) {
|
||||
var $message = 'can\'t resolve reference ' + $schema + ' from id ' + it.baseId;
|
||||
if (it.opts.missingRefs == 'fail') {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
[
|
||||
{
|
||||
"skip": true,
|
||||
"description": "root ref in remote ref (#13)",
|
||||
"schema": {
|
||||
"id": "http://localhost:1234/object",
|
||||
|
|
Loading…
Reference in New Issue