Skip to content

Commit fba24c6

Browse files
committed
Added support to include invalid references
All functions that take an options object now support options.includeInvalid to allow you to find references that are invalid. Fixes #47
1 parent 84d94c1 commit fba24c6

7 files changed

+78
-11
lines changed

browser/json-refs-min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

browser/json-refs-standalone-min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

browser/json-refs-standalone.js

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

browser/json-refs.js

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The options used for various JsonRefs APIs.
3636
| Param | Type | Default | Description |
3737
| --- | --- | --- | --- |
3838
| [filter] | <code>string</code> &#124; <code>Array.&lt;string&gt;</code> &#124; <code>function</code> | <code>&quot;function () {return true;}&quot;</code> | The filter to use when gathering JSON References *(If this value is a single string or an array of strings, the value(s) are expected to be the `type(s)` you are interested in collecting as described in [getRefDetails](#module_JsonRefs.getRefDetails). If it is a function, it is expected that the function behaves like [RefDetailsFilter](#module_JsonRefs..RefDetailsFilter).)* |
39+
| [includeInvalid] | <code>boolean</code> | <code>false</code> | Whether or not to include invalid JSON Reference details *(This will make it so that objects that are like JSON Reference objects, as in they are an `Object` and the have a `$ref` property, but fail validation will be included. This is very useful for when you want to know if you have invalid JSON Reference definitions. This will not mean that APIs will process invalid JSON References but the reasons as to why the JSON References are invalid will be included in the returned metadata.)* |
3940
| [loaderOptions] | <code>object</code> | | The options to pass to [PathLoader~load](https://github.com/whitlockjc/path-loader/blob/master/docs/API.md#module_PathLoader.load). |
4041
| [options.relativeBase] | <code>string</code> | | The base location to use when resolving relative references *(Only useful for APIs that do remote reference resolution. If this value is not defined, [path-loader](https://github.com/whitlockjc/path-loader) will use `window.location.href` for the browser and `process.cwd()` for Node.js.)* |
4142
| [options.subDocPath] | <code>string</code> &#124; <code>Array.&lt;string&gt;</code> | <code>&quot;[]&quot;</code> | The JSON Pointer or array of path segments to the sub document location to search from |

index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ function filterRefs (options, refs) {
156156
var refDetails = refs[refPtr];
157157

158158
if (refFilter(refDetails, pathFromPtr(refPtr)) === true &&
159-
refPtr.indexOf(subDocPrefix) > -1) {
159+
refPtr.indexOf(subDocPrefix) > -1 &&
160+
(refDetails.type !== 'invalid' || options.includeInvalid === true)) {
160161
filtered[refPtr] = refDetails;
161162
}
162163
});
@@ -475,6 +476,9 @@ function validateOptions (options) {
475476
!isType(options.filter, 'Function') &&
476477
!isType(options.filter, 'String')) {
477478
throw new TypeError('options.filter must be an Array, a Function of a String');
479+
} else if (!isType(options.includeInvalid, 'Undefined') &&
480+
!isType(options.includeInvalid, 'Boolean')) {
481+
throw new TypeError('options.includeInvalid must be a Boolean');
478482
}
479483
}
480484
}
@@ -497,6 +501,11 @@ function validateOptions (options) {
497501
* References *(If this value is a single string or an array of strings, the value(s) are expected to be the `type(s)`
498502
* you are interested in collecting as described in {@link module:JsonRefs.getRefDetails}. If it is a function, it is
499503
* expected that the function behaves like {@link module:JsonRefs~RefDetailsFilter}.)*
504+
* @param {boolean} [includeInvalid=false] - Whether or not to include invalid JSON Reference details *(This will make
505+
* it so that objects that are like JSON Reference objects, as in they are an `Object` and the have a `$ref` property,
506+
* but fail validation will be included. This is very useful for when you want to know if you have invalid JSON
507+
* Reference definitions. This will not mean that APIs will process invalid JSON References but the reasons as to why
508+
* the JSON References are invalid will be included in the returned metadata.)*
500509
* @param {object} [loaderOptions] - The options to pass to
501510
* {@link https://github.com/whitlockjc/path-loader/blob/master/docs/API.md#module_PathLoader.load|PathLoader~load}.
502511
* @param {string} [options.relativeBase] - The base location to use when resolving relative references *(Only useful
@@ -709,7 +718,7 @@ function findRefs (obj, options) {
709718
if (isRefLike(node)) {
710719
refDetails = getRefDetails(node);
711720

712-
if (refDetails.type !== 'invalid') {
721+
if (refDetails.type !== 'invalid' || options.includeInvalid === true) {
713722
if (refFilter(refDetails, path) === true) {
714723
refs[pathToPtr(path)] = refDetails;
715724
}
@@ -798,6 +807,8 @@ function findRefsAt (location, options) {
798807
delete cOptions.filter;
799808
delete cOptions.subDocPath;
800809

810+
cOptions.includeInvalid = true;
811+
801812
remoteCache[location].refs = findRefs(res, cOptions);
802813

803814
// Filter out the references based on options.filter and options.subDocPath

test/test-json-refs.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ describe('json-refs', function () {
525525
[[], objTypeError],
526526
[['wrongType'], objTypeError],
527527
[[{}, 1], optionsTypeError],
528+
[[{}, {includeInvalid: 'wrongType'}], new TypeError('options.includeInvalid must be a Boolean')],
528529
[[[], {subDocPath: 1}], osdpTypeError],
529530
[[{}, {subDocPath: '#/missing'}], osdpMissingError],
530531
[[{}, {filter: 1}], ofTypeError]
@@ -573,6 +574,10 @@ describe('json-refs', function () {
573574
});
574575
});
575576

577+
it('options.includeInvalid', function () {
578+
runRefDetailsTestScenarios(JsonRefs.findRefs(testDocument, {includeInvalid: true}), expectedAllReferences);
579+
});
580+
576581
describe('options.subDocPath', function () {
577582
it('option as array', function () {
578583
runRefDetailsTestScenarios(JsonRefs.findRefs(testDocument, {subDocPath: ['array', '0']}), {
@@ -925,6 +930,34 @@ describe('json-refs', function () {
925930
})
926931
.then(done, done);
927932
});
933+
934+
it('should support options.includeInvalid', function (done) {
935+
JsonRefs.resolveRefs(testDocument, {
936+
includeInvalid: true,
937+
loaderOptions: {
938+
processContent: yamlContentProcessor
939+
},
940+
relativeBase: relativeBase
941+
})
942+
.then(function (res) {
943+
var expectedAllResolvedRefs = _.cloneDeep(expectedValidResolveRefs);
944+
945+
expectedAllResolvedRefs['#/invalid'] = {
946+
def: testDocument.invalid,
947+
uri: testDocument.invalid.$ref,
948+
uriDetails: URI.parse(testDocument.invalid.$ref),
949+
error: 'URI is not strictly valid.',
950+
type: 'invalid'
951+
};
952+
953+
// Validate the resolved document is the same as when options.includeInvalid is false
954+
assert.deepEqual(res.resolved, expectedFullyResolved);
955+
956+
// Validate the reference metadata includes the invalid reference details
957+
validateResolvedRefDetails(res.refs, expectedAllResolvedRefs);
958+
})
959+
.then(done, done);
960+
});
928961
});
929962

930963
describe('#resolveRefsAt', function () {

0 commit comments

Comments
 (0)