From fe3bc56d535ef9a9920861e3a9fcc58a3a19e6fd Mon Sep 17 00:00:00 2001 From: jos Date: Sat, 12 Nov 2016 14:10:10 +0100 Subject: [PATCH] Implemented addErrors and removeErrors --- src/jsonData.js | 41 +++++++++++++++++++ src/typedef.js | 5 +++ test/jsonData.test.js | 94 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 138 insertions(+), 2 deletions(-) diff --git a/src/jsonData.js b/src/jsonData.js index c6bc99a..da7b2f4 100644 --- a/src/jsonData.js +++ b/src/jsonData.js @@ -447,6 +447,8 @@ export function test (data, path, value) { } } +// TODO: move expand, collapse, mergeErrors to actions.js + /** * Expand or collapse one or multiple items or properties * @param {JSONData} data @@ -521,6 +523,45 @@ function expandRecursive (data, path, callback, expanded) { } } +/** + * Merge one or multiple errors (for example JSON schema errors) + * into the data + * + * @param {JSONData} data + * @param {Array.} errors + */ +export function addErrors (data, errors) { + let updatedData = data + + errors.forEach(error => { + const dataPath = toDataPath(data, parseJSONPointer(error.dataPath)) + // TODO: do we want to be able to store multiple errors per item? + updatedData = setIn(updatedData, dataPath.concat('error'), error) + }) + + return updatedData +} + +/** + * Remove errors which where added using addErrors + * + * @param {JSONData} data + * @param {Array.} errors + */ +export function removeErrors (data, errors) { + let updatedData = data + + errors.forEach(error => { + const dataPath = toDataPath(data, parseJSONPointer(error.dataPath)) + updatedData = deleteIn(updatedData, dataPath.concat('error')) + }) + + return updatedData +} + +// TODO: implement a function removeAllErrors (data) + + /** * Test whether a path exists in the json data * @param {JSONData} data diff --git a/src/typedef.js b/src/typedef.js index b280657..0ddd711 100644 --- a/src/typedef.js +++ b/src/typedef.js @@ -31,6 +31,11 @@ * }} JSONPatchResult * * @typedef {{ + * dataPath: string, + * message: string + * }} JSONSchemaError + * + * @typedef {{ * name: string?, * mode: 'code' | 'form' | 'text' | 'tree' | 'view'?, * modes: string[]?, diff --git a/test/jsonData.test.js b/test/jsonData.test.js index c1e93cf..4536aca 100644 --- a/test/jsonData.test.js +++ b/test/jsonData.test.js @@ -1,7 +1,8 @@ import test from 'ava'; import { - jsonToData, dataToJson, expand, patchData, pathExists, - parseJSONPointer, compileJSONPointer + jsonToData, dataToJson, patchData, pathExists, + parseJSONPointer, compileJSONPointer, + expand, addErrors, removeErrors } from '../src/jsonData' @@ -236,6 +237,87 @@ const JSON_DATA_EXAMPLE_COLLAPSED_2 = { ] } +const JSON_SCHEMA_ERRORS = [ + {dataPath: '/obj/arr/2/b', message: 'String expected'}, + {dataPath: '/nill', message: 'Null expected'} +] + +const JSON_DATA_EXAMPLE_ERRORS = { + type: 'Object', + expanded: true, + props: [ + { + name: 'obj', + value: { + type: 'Object', + expanded: true, + props: [ + { + name: 'arr', + value: { + type: 'Array', + expanded: true, + items: [ + { + type: 'value', + value: 1 + }, + { + type: 'value', + value: 2 + }, + { + type: 'Object', + expanded: true, + props: [ + { + name: 'a', + value: { + type: 'value', + value: 3 + } + }, + { + name: 'b', + value: { + type: 'value', + value: 4, + error: JSON_SCHEMA_ERRORS[0] + } + } + ] + }, + ] + } + } + ] + } + }, + { + name: 'str', + value: { + type: 'value', + value: 'hello world' + } + }, + { + name: 'nill', + value: { + type: 'value', + value: null, + error: JSON_SCHEMA_ERRORS[1] + } + }, + { + name: 'bool', + value: { + type: 'value', + value: false + } + } + ] +} + test('jsonToData', t => { function expand (path) { return true @@ -650,3 +732,11 @@ test('jsonpatch test (fail: value not equal)', t => { t.deepEqual(revert, []) t.is(result.error.toString(), 'Error: Test failed, value differs') }) + +test('add and remove errors', t => { + const dataWithErrors = addErrors(JSON_DATA_EXAMPLE, JSON_SCHEMA_ERRORS) + t.deepEqual(dataWithErrors, JSON_DATA_EXAMPLE_ERRORS) + + const dataWithoutErrors = removeErrors(dataWithErrors, JSON_SCHEMA_ERRORS) + t.deepEqual(dataWithoutErrors, JSON_DATA_EXAMPLE) +})