Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: throw error for invalid $refs #108

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/dereference.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
const refSlashSplit = value?.$ref?.split('/');

// Catches external file refs: /absolute-root/absolute-root.yaml
const refHashSplit = value?.ref?.split('#');
const refHashSplit = value?.$ref?.split('#');
darrenyong marked this conversation as resolved.
Show resolved Hide resolved

// Valid refs split on `/` should have an element with '#' as the last character
// #components/schemas will not return a "validRef"
const validRef = refSlashSplit?.find(ele => ele.slice(-1) === '#');
if (refSlashSplit?.length > 1 && refHashSplit?.length > 1 && !validRef) {

if (refSlashSplit?.length > 1 && refHashSplit?.length > 1 && !validRef && !options.continueOnError) {
throw new InvalidPointerError(value.$ref, keyPath);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Schema with invalid pointers', function () {
helper.shouldNotGetCalled();
} catch (err) {
expect(err).to.be.an.instanceOf(InvalidPointerError);
expect(err.message).to.contain('Invalid $ref pointer "f". Pointers must begin with "#/"');
expect(err.message).to.contain('Invalid $ref pointer "./invalid.json#f". Pointers must begin with "#/"');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These types of refs are caught earlier now so it gives a more descriptive path on the invalid pointer.

}
});

Expand Down
Loading