Skip to content
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
38 changes: 24 additions & 14 deletions packages/core/src/util/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/

import isEmpty from 'lodash/isEmpty';
import range from 'lodash/range';

export const compose = (path1: string, path2: string) => {
let p1 = path1;
Expand Down Expand Up @@ -55,18 +54,29 @@ export { compose as composePaths };
* @returns {string[]} an array containing only non-schema-specific segments
*/
export const toDataPathSegments = (schemaPath: string): string[] => {
const s = schemaPath
.replace(/(anyOf|allOf|oneOf)\/[\d]+\//g, '')
.replace(/(then|else)\//g, '');
const segments = s.split('/');

const decodedSegments = segments.map(decode);

const startFromRoot = decodedSegments[0] === '#' || decodedSegments[0] === '';
const startIndex = startFromRoot ? 2 : 1;
return range(startIndex, decodedSegments.length, 2).map(
(idx) => decodedSegments[idx]
);
const segments = schemaPath.split('/').map(decode);
Comment thread
lucas-koehler marked this conversation as resolved.
Resolved
const startFromRoot = segments[0] === '#' || segments[0] === '';
const dataSegments: string[] = [];
// Walk the pointer segment by segment so that keywords are only skipped
// where a keyword is expected, never inside or in place of a property name
let i = startFromRoot ? 1 : 0;
while (i < segments.length) {
const segment = segments[i];
if (
(segment === 'anyOf' || segment === 'allOf' || segment === 'oneOf') &&
Comment thread
lucas-koehler marked this conversation as resolved.
Resolved
/^\d+$/.test(segments[i + 1] ?? '')
) {
i += 2;
} else if (segment === 'then' || segment === 'else') {
i += 1;
} else {
if (i + 1 < segments.length) {
dataSegments.push(segments[i + 1]);
}
i += 2;
}
}
return dataSegments;
};

/**
Expand Down Expand Up @@ -95,7 +105,7 @@ export const encode = (segment: string) =>
* Decodes a given JSON Pointer segment to its "normal" representation
*/
export const decode = (pointerSegment: string) =>
pointerSegment?.replace(/~1/g, '/').replace(/~0/, '~');
pointerSegment?.replace(/~1/g, '/').replace(/~0/g, '~');

/**
* Transform a dotted path to a uiSchema properties path
Expand Down
33 changes: 32 additions & 1 deletion packages/core/test/util/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
import test from 'ava';
import { JsonSchema } from '../../src/models';
import { Resolve, toDataPath } from '../../src';
import { decode, Resolve, toDataPath } from '../../src';

test('resolve ', (t) => {
const schema: JsonSchema = {
Expand Down Expand Up @@ -108,6 +108,28 @@ test('toDataPath replace all combinators', (t) => {
test('toDataPath use of keywords', (t) => {
t.is(toDataPath('#/properties/properties'), 'properties');
});
test('toDataPath keeps property names ending in then or else', (t) => {
t.is(
toDataPath('#/properties/something_else/properties/name'),
'something_else.name'
);
t.is(toDataPath('#/properties/strengthen/properties/x'), 'strengthen.x');
});
test('toDataPath keeps properties named like keywords', (t) => {
t.is(toDataPath('#/properties/then/properties/name'), 'then.name');
t.is(toDataPath('#/properties/else/properties/name'), 'else.name');
t.is(toDataPath('#/properties/anyOf/properties/0'), 'anyOf.0');
t.is(
toDataPath('/anyOf/1/then/properties/then/properties/else'),
'then.else'
);
});
test('toDataPath drops a trailing combinator index', (t) => {
t.is(toDataPath('#/properties/a/anyOf/0'), 'a');
t.is(toDataPath('#/properties/a/oneOf/1'), 'a');
t.is(toDataPath('#/properties/a/allOf/2'), 'a');
t.is(toDataPath('#/properties/a/properties/b/anyOf/10'), 'a.b');
});
test('toDataPath use of encoded paths', (t) => {
const fooBar = encodeURIComponent('foo.bar');
t.is(toDataPath(`#/properties/${fooBar}`), `${fooBar}`);
Expand Down Expand Up @@ -135,6 +157,15 @@ test('toDataPath use of encoded paths relative without /', (t) => {
test('toDataPath use of encoded special character in pathname', (t) => {
t.is(toDataPath('properties/foo~0bar~1baz'), 'foo~bar/baz');
});
test('decode replaces every escape in a segment', (t) => {
t.is(decode('a~0b~0c'), 'a~b~c');
t.is(decode('a~1b~1c'), 'a/b/c');
t.is(decode('~01'), '~1');
});
test('toDataPath decodes repeated escapes in a segment', (t) => {
t.is(toDataPath('#/properties/a~0b~0c'), 'a~b~c');
t.is(toDataPath('#/properties/a~1b~1c'), 'a/b/c');
});
test('resolve instance', (t) => {
const instance = { foo: 123 };
const result = Resolve.data(instance, toDataPath('#/properties/foo'));
Expand Down
Loading