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

fix: throw error for invalid $refs missing the / #102

Merged
merged 9 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
7 changes: 7 additions & 0 deletions lib/dereference.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { ono } = require('@jsdevtools/ono');

const Pointer = require('./pointer');
const $Ref = require('./ref');
const { InvalidPointerError } = require('./util/errors');
const url = require('./util/url');

/**
Expand Down Expand Up @@ -82,6 +83,12 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
const keyPath = Pointer.join(path, key);
const keyPathFromRoot = Pointer.join(pathFromRoot, key);
const value = obj[key];
const splitRef = value?.$ref?.split("/");

if (splitRef?.length > 1 && splitRef?.includes("#components")) {
darrenyong marked this conversation as resolved.
Show resolved Hide resolved
throw new InvalidPointerError(value.$ref, keyPath);
}

let circular = false;

if ($Ref.isAllowed$Ref(value, options)) {
Expand Down
10 changes: 10 additions & 0 deletions test/specs/invalid-pointers/invalid-pointers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ describe('Schema with invalid pointers', function () {
}
});

it('should throw an error for an invalid reference', async function () {
try {
await $RefParser.dereference(path.rel('specs/invalid-pointers/invalid-reference.json'));
helper.shouldNotGetCalled();
} catch (err) {
expect(err).to.be.an.instanceOf(InvalidPointerError);
expect(err.message).to.contain('Invalid $ref pointer "#components/". Pointers must begin with "#/"');
}
});

it('should throw a grouped error for an invalid pointer if continueOnError is true', async function () {
const parser = new $RefParser();
try {
Expand Down
5 changes: 5 additions & 0 deletions test/specs/invalid-pointers/invalid-reference.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"foo": {
"$ref": "#components/"
}
}
Loading