Implemented addErrors and removeErrors

This commit is contained in:
jos 2016-11-12 14:10:10 +01:00
parent 8da5ece3b8
commit fe3bc56d53
3 changed files with 138 additions and 2 deletions

View File

@ -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 * Expand or collapse one or multiple items or properties
* @param {JSONData} data * @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.<JSONSchemaError>} 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.<JSONSchemaError>} 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 * Test whether a path exists in the json data
* @param {JSONData} data * @param {JSONData} data

View File

@ -31,6 +31,11 @@
* }} JSONPatchResult * }} JSONPatchResult
* *
* @typedef {{ * @typedef {{
* dataPath: string,
* message: string
* }} JSONSchemaError
*
* @typedef {{
* name: string?, * name: string?,
* mode: 'code' | 'form' | 'text' | 'tree' | 'view'?, * mode: 'code' | 'form' | 'text' | 'tree' | 'view'?,
* modes: string[]?, * modes: string[]?,

View File

@ -1,7 +1,8 @@
import test from 'ava'; import test from 'ava';
import { import {
jsonToData, dataToJson, expand, patchData, pathExists, jsonToData, dataToJson, patchData, pathExists,
parseJSONPointer, compileJSONPointer parseJSONPointer, compileJSONPointer,
expand, addErrors, removeErrors
} from '../src/jsonData' } 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 => { test('jsonToData', t => {
function expand (path) { function expand (path) {
return true return true
@ -650,3 +732,11 @@ test('jsonpatch test (fail: value not equal)', t => {
t.deepEqual(revert, []) t.deepEqual(revert, [])
t.is(result.error.toString(), 'Error: Test failed, value differs') 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)
})