Relax JSON input validation

Currently the JSON form input validator only passes if the value is a
valid JSON string. This doesn't accout for cases where a paramater
doesn't need to be set. This patch relaxes the validation rule so it
also allows the value to be an empty string.

Change-Id: I4d135120ac3041b223986eef8f54c4de24f20379
Closes-Bug: #1645266
This commit is contained in:
Florian Fuchs 2016-11-28 12:25:48 +01:00
parent f4ecba71ab
commit 537618fed2
1 changed files with 6 additions and 2 deletions

View File

@ -4,8 +4,12 @@ import Formsy from 'formsy-react';
* Custom validation rules used throughout the app.
*/
export default function initFormsy () {
Formsy.addValidationRule('isJson', (values, value) => {
try { return !!JSON.parse(value); }
/*
* The field is valid if the current value is either empty
* or a valid JSON string.
*/
Formsy.addValidationRule('isJson', (values, value, otherField) => {
try { return value === '' ? true : !!JSON.parse(value); }
catch(e) { return false; }
});
}