From 5b2ba08813c2d67cc8b8abe18b4d6cc1f9b5e862 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Wed, 17 Jun 2015 22:23:49 +0100 Subject: [PATCH] missingRefs option - "ignore", "fail" or throw error during compilation (default) --- README.md | 3 ++- lib/dot/$ref.jst | 21 +++++++++++++-------- package.json | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index af84b12..c993fe7 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,8 @@ Returns the text with all errors in a String. Options can have these properties: - _format_: formats validation mode ('fast' by default). Pass 'full' for more correct and slow validation or `false` not to validate formats at all. E.g., 25:00:00 and 2015/14/33 will be invalid time and date in 'full' mode but it will be valid in 'fast' mode. - _formats_: an object with custom formats. Keys and values will be passed to `addFormat` method. - _meta_: add [meta-schema](http://json-schema.org/documentation.html) so it can be used by other schemas (true by default). -- _validateSchema: validate added/compiled schemas against meta-schema (true by default). `$schema` property in the schema can either be absent (draft-4 meta-schema will be used) or can be a reference to any previously added schema. If the validation fails, the exception is thrown. Pass "log" in this option to log error instead of throwing exception. +- _validateSchema_: validate added/compiled schemas against meta-schema (true by default). `$schema` property in the schema can either be absent (draft-4 meta-schema will be used) or can be a reference to any previously added schema. If the validation fails, the exception is thrown. Pass "log" in this option to log error instead of throwing exception. +- _missingRefs_: by default if the reference cannot be resolved during compilation the exception is thrown. Pass 'ignore' to log error during compilation and pass validation. Pass 'fail' to log error and successfully compile schema but fail validation if this rule is checked. - _uniqueItems_: validate `uniqueItems` keyword (true by default). - _unicode_: calculate correct length of strings with unicode pairs (true by default). Pass `false` to use `.length` of strings that is faster, but gives "incorrect" lengths of strings with unicode pairs - each unicode pair is counted as two characters. - _beautify_: format the generated function with [js-beautify](https://github.com/beautify-web/js-beautify) (the validating function is generated without line-breaks). `npm install js-beautify` to use this option. `true` or js-beautify options can be passed. diff --git a/lib/dot/$ref.jst b/lib/dot/$ref.jst index 10b2665..046832d 100644 --- a/lib/dot/$ref.jst +++ b/lib/dot/$ref.jst @@ -26,18 +26,23 @@ } {{? $breakOnError }} else { {{?}} {{?}} {{??}} - {{ var $v = 'v' + $lvl; }} - var {{=$v}} = root.refVal[0]; - {{# def.validateRef:$v }} + {{# def.validateRef:'root.refVal[0]' }} {{?}} {{??}} {{ var $refVal = it.resolveRef(it.baseId, $schema, it.rootId); }} {{? $refVal === undefined }} - {{# def.error:'$ref' }} - {{? $breakOnError }} if (false) { {{?}} + {{ var $message = 'can\'t resolve reference ' + $schema; }} + {{? it.opts.missingRefs == 'fail' }} + {{ console.log($message); }} + {{# def.error:'$ref' }} + {{? $breakOnError }} if (false) { {{?}} + {{?? it.opts.missingRefs == 'ignore' }} + {{ console.log($message); }} + {{? $breakOnError }} if (true) { {{?}} + {{??}} + {{ throw new Error($message); }} + {{?}} {{??}} - {{ var $v = 'v' + $lvl; }} - var {{=$v}} = {{=$refVal}}; - {{# def.validateRef:$v }} + {{# def.validateRef:$refVal }} {{?}} {{?}} diff --git a/package.json b/package.json index 975c62a..80afcaa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ajv", - "version": "0.5.3", + "version": "0.5.4", "description": "Another JSON schema Validator", "main": "lib/ajv.js", "scripts": {