Skip to content

Commit

Permalink
fix: local refs should not contain any whitespace characters (#89)
Browse files Browse the repository at this point in the history
* fix: local refs should not contain any whitespace characters

* chore: # edge case and tests

* fix: make regex less strict
  • Loading branch information
rainum authored May 17, 2021
1 parent a27048d commit 867da5b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
34 changes: 31 additions & 3 deletions src/__tests__/isLocalRef.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
import { isLocalRef } from '../isLocalRef';

describe('isLocalRef', () => {
it.each(['#', '#/', '#/foo', '#/0/1', '#/~1'])('should treat %s as local reference', ref => {
it.each([
'#',
'#/foo',
'#/foo/0',
'#/',
'#/a~1b',
'#/c%25d',
'#/e%5Ef',
'#/g%7Ch',
'#/i%5Cj',
'#/k%22l',
'#/%20',
'#/m~0n',
'#./d~8z',
])('should treat %s as local reference', ref => {
expect(isLocalRef(ref)).toEqual(true);
});

it.each(['', '../#', 'http://a#', '/foo/#', '../test.yaml#/'])('should not treat %s as local reference', ref => {
expect(isLocalRef(ref)).toEqual(false);
it.each([
'',
'../#',
'http://a#',
'/foo/#',
'../test.yaml#/',
'#/foo\n/bar',
'#/foo\t/bar',
'#/foo\u2006​/bar',
' #/foo/bar',
'#/foo /bar',
'#/foo/bar ',
'# Hello, world!',
'foo',
])('should not treat %s as local reference', ref => {
expect(isLocalRef(ref as string)).toEqual(false);
});
});
2 changes: 1 addition & 1 deletion src/isLocalRef.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const isLocalRef = (pointer: string) => pointer.length > 0 && pointer[0] === '#';
export const isLocalRef = (pointer: string) => pointer.length > 0 && (pointer === '#' || /^#\S*$/.test(pointer));

0 comments on commit 867da5b

Please sign in to comment.