Skip to content

Commit

Permalink
Improve toDataPath to replace all occurences
Browse files Browse the repository at this point in the history
* extend regex to replace all occurences
* add test cases
  • Loading branch information
eneufeld committed Apr 21, 2019
1 parent 7453f15 commit d761e82
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/util/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export const compose = (path1: string, path2: string) => {
*/
export const toDataPathSegments = (schemaPath: string): string[] => {
const s = schemaPath
.replace(/anyOf\/[\d]\//, '')
.replace(/allOf\/[\d]\//, '')
.replace(/oneOf\/[\d]\//, '');
.replace(/anyOf\/[\d]\//g, '')
.replace(/allOf\/[\d]\//g, '')
.replace(/oneOf\/[\d]\//g, '');
const segments = s.split('/');

const startFromRoot = segments[0] === '#' || segments[0] === '';
Expand Down
17 changes: 17 additions & 0 deletions packages/core/test/util/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ test('resolve ', t => {
test('toDataPath ', t => {
t.is(toDataPath('#/properties/foo/properties/bar'), 'foo.bar');
});
test('toDataPath replace anyOf', t => {
t.is(toDataPath('/anyOf/1/properties/foo/anyOf/1/properties/bar'), 'foo.bar');
});
test('toDataPath replace allOf', t => {
t.is(toDataPath('/allOf/1/properties/foo/allOf/1/properties/bar'), 'foo.bar');
});
test('toDataPath replace oneOf', t => {
t.is(toDataPath('/oneOf/1/properties/foo/oneOf/1/properties/bar'), 'foo.bar');
});
test('toDataPath replace all combinators', t => {
t.is(
toDataPath(
'/oneOf/1/properties/foo/anyOf/1/properties/bar/allOf/1/properties/foobar'
),
'foo.bar.foobar'
);
});
test('toDataPath use of keywords', t => {
t.is(toDataPath('#/properties/properties'), 'properties');
});
Expand Down

0 comments on commit d761e82

Please sign in to comment.