Fix validation blocking valid submission

This commit is contained in:
Mark Kaylor 2023-02-03 11:43:09 +01:00
parent 3c33ea5650
commit caa4720005
2 changed files with 19 additions and 22 deletions

View File

@ -213,28 +213,23 @@ const createYupSchemaAttribute = (type, validations, options) => {
}
if (type === 'json') {
if (validations.required) {
schema = yup.mixed(errorsTrads.required).test('required', errorsTrads.required, (value) => {
return value === undefined;
schema = yup
.mixed(errorsTrads.json)
.test('isJSON', errorsTrads.json, (value) => {
try {
JSON.parse(value);
return true;
} catch (err) {
return false;
}
})
.nullable()
.test('required', errorsTrads.required, (value) => {
if (validations.required && !value.length) return false;
return true;
});
} else {
schema = yup
.mixed(errorsTrads.json)
.test('isJSON', errorsTrads.json, (value) => {
if (value === undefined) {
return true;
}
try {
JSON.parse(value);
return true;
} catch (err) {
return false;
}
})
.nullable();
}
}
if (type === 'email') {

View File

@ -146,7 +146,9 @@ const GenericInput = ({
hint={hint}
required={required}
onChange={(json) => {
onChange({ target: { name, value: json } });
// Default to null when the field is required and there is no input value
const value = !attribute.required && !json.length ? 'null' : json;
onChange({ target: { name, value } });
}}
minHeight={pxToRem(252)}
maxHeight={pxToRem(504)}