-
-
Notifications
You must be signed in to change notification settings - Fork 863
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from epoberezkin/inline
Inline referenced schemas without refs in them, #6
- Loading branch information
Showing
10 changed files
with
493 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
'use strict'; | ||
module.exports = function anonymous(it) { | ||
var out = ' '; | ||
var $lvl = it.level, | ||
$dataLvl = it.dataLevel, | ||
$schema = it.schema['$ref'], | ||
$schemaPath = it.schemaPath + '.' + '$ref', | ||
$breakOnError = !it.opts.allErrors; | ||
var $data = 'data' + ($dataLvl || ''), | ||
$valid = 'valid' + $lvl, | ||
$errs = 'errs' + $lvl; | ||
if ($schema == '#' || $schema == '#/') { | ||
if (it.isRoot) { | ||
if ($breakOnError && it.wasTop) { | ||
out += ' if (! ' + ('validate') + '(' + ($data) + ', (dataPath || \'\')'; | ||
if (it.errorPath != '""') { | ||
out += ' + ' + (it.errorPath); | ||
} | ||
out += ') ) return false; else { '; | ||
} else { | ||
out += ' if (! ' + ('validate') + '(' + ($data) + ', (dataPath || \'\')'; | ||
if (it.errorPath != '""') { | ||
out += ' + ' + (it.errorPath); | ||
} | ||
out += ') ) { if (vErrors === null) vErrors = ' + ('validate') + '.errors; else vErrors = vErrors.concat(' + ('validate') + '.errors); errors = vErrors.length; } '; | ||
if ($breakOnError) { | ||
out += ' else { '; | ||
} | ||
} | ||
} else { | ||
out += ' if (! ' + ('root.refVal[0]') + '(' + ($data) + ', (dataPath || \'\')'; | ||
if (it.errorPath != '""') { | ||
out += ' + ' + (it.errorPath); | ||
} | ||
out += ') ) { if (vErrors === null) vErrors = ' + ('root.refVal[0]') + '.errors; else vErrors = vErrors.concat(' + ('root.refVal[0]') + '.errors); errors = vErrors.length; } '; | ||
if ($breakOnError) { | ||
out += ' else { '; | ||
} | ||
} | ||
} else { | ||
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') { | ||
console.log($message); | ||
if (it.wasTop && $breakOnError) { | ||
out += ' validate.errors = [ { keyword: \'' + ('$ref') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'can\\\'t resolve reference ' + (it.util.escapeQuotes($schema)) + '\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: ' + (it.util.toQuotedString($schema)) + ', data: ' + ($data); | ||
} | ||
out += ' }]; return false; '; | ||
} else { | ||
out += ' var err = { keyword: \'' + ('$ref') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'can\\\'t resolve reference ' + (it.util.escapeQuotes($schema)) + '\' '; | ||
if (it.opts.verbose) { | ||
out += ', schema: ' + (it.util.toQuotedString($schema)) + ', data: ' + ($data); | ||
} | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
if ($breakOnError) { | ||
out += ' if (false) { '; | ||
} | ||
} else if (it.opts.missingRefs == 'ignore') { | ||
console.log($message); | ||
if ($breakOnError) { | ||
out += ' if (true) { '; | ||
} | ||
} else { | ||
throw new Error($message); | ||
} | ||
} else if (typeof $refVal == 'string') { | ||
out += ' if (! ' + ($refVal) + '(' + ($data) + ', (dataPath || \'\')'; | ||
if (it.errorPath != '""') { | ||
out += ' + ' + (it.errorPath); | ||
} | ||
out += ') ) { if (vErrors === null) vErrors = ' + ($refVal) + '.errors; else vErrors = vErrors.concat(' + ($refVal) + '.errors); errors = vErrors.length; } '; | ||
if ($breakOnError) { | ||
out += ' else { '; | ||
} | ||
} else { | ||
var $it = it.util.copy(it), | ||
$closingBraces = ''; | ||
$it.level++; | ||
$it.schema = $refVal.schema; | ||
$it.schemaPath = ''; | ||
var $code = it.validate($it); | ||
if (/validate\.schema/.test($code)) { | ||
out += ' var rootSchema' + ($it.level) + ' = validate.schema; validate.schema = ' + ($refVal.code) + '; ' + ($code) + ' validate.schema = rootSchema' + ($it.level) + '; '; | ||
} else { | ||
out += ' ' + ($code) + ' '; | ||
} | ||
if ($breakOnError) { | ||
out += ' if (valid' + ($it.level) + ') { '; | ||
} | ||
} | ||
} | ||
return out; | ||
} |
Oops, something went wrong.