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
15 changes: 7 additions & 8 deletions packages/core/src/reducers/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
UPDATE_CORE,
UpdateCoreAction,
} from '../actions';
import { createAjv, isOneOfEnumSchema, Reducer } from '../util';
import { createAjv, decode, isOneOfEnumSchema, Reducer } from '../util';
import type { JsonSchema, UISchemaElement } from '../models';

export const validate = (
Expand Down Expand Up @@ -356,13 +356,9 @@ const getInvalidProperty = (error: ErrorObject): string | undefined => {
};

export const getControlPath = (error: ErrorObject) => {
const dataPath = (error as any).dataPath;
// older AJV version
if (dataPath) {
return dataPath.replace(/\//g, '.').substr(1);
}
// dataPath was renamed to instancePath in AJV v8
let controlPath: string = error.instancePath;
// Up until AJV v7 the path property was called 'dataPath'
// With AJV v8 the property was renamed to 'instancePath'
let controlPath = (error as any).dataPath || error.instancePath || '';

// change '/' chars to '.'
controlPath = controlPath.replace(/\//g, '.');
Expand All @@ -374,6 +370,9 @@ export const getControlPath = (error: ErrorObject) => {

// remove '.' chars at the beginning of paths
controlPath = controlPath.replace(/^./, '');

// decode JSON Pointer escape sequences
controlPath = decode(controlPath);
return controlPath;
};

Expand Down
27 changes: 26 additions & 1 deletion packages/core/test/reducers/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
THE SOFTWARE.
*/
import test from 'ava';
import Ajv from 'ajv';
import Ajv, { ErrorObject } from 'ajv';
import { coreReducer } from '../../src/reducers';
import {
init,
Expand All @@ -39,6 +39,7 @@ import {
JsonFormsCore,
validate,
subErrorsAt,
getControlPath,
} from '../../src/reducers/core';

import { cloneDeep } from 'lodash';
Expand Down Expand Up @@ -1812,3 +1813,27 @@ test('core reducer - setSchema - schema with id', (t) => {
const after: JsonFormsCore = coreReducer(before, setSchema(updatedSchema));
t.is(after.schema.properties.animal.minLength, 5);
});

test('core reducer helpers - getControlPath - converts JSON Pointer notation to dot notation', (t) => {
const errorObject = { instancePath: '/group/name' } as ErrorObject;
const controlPath = getControlPath(errorObject);
t.is(controlPath, 'group.name');
});

test('core reducer helpers - getControlPath - fallback to AJV <=7 errors', (t) => {
const errorObject = { dataPath: '/group/name' } as unknown as ErrorObject;
const controlPath = getControlPath(errorObject);
t.is(controlPath, 'group.name');
});

test('core reducer helpers - getControlPath - fallback to AJV <=7 errors does not crash for empty paths', (t) => {
const errorObject = { dataPath: '' } as unknown as ErrorObject;
const controlPath = getControlPath(errorObject);
t.is(controlPath, '');
});

test('core reducer helpers - getControlPath - decodes JSON Pointer escape sequences', (t) => {
const errorObject = { instancePath: '/~0group/~1name' } as ErrorObject;
const controlPath = getControlPath(errorObject);
t.is(controlPath, '~group./name');
});