Merge pull request #15778 from strapi/fix/non-required-json-input

This commit is contained in:
markkaylor 2023-03-13 14:23:08 +01:00 committed by GitHub
commit f78ba41c59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 15 deletions

View File

@ -216,6 +216,10 @@ const createYupSchemaAttribute = (type, validations, options) => {
schema = yup
.mixed(errorsTrads.json)
.test('isJSON', errorsTrads.json, (value) => {
if (!value || !value.length) {
return true;
}
try {
JSON.parse(value);
@ -226,7 +230,9 @@ const createYupSchemaAttribute = (type, validations, options) => {
})
.nullable()
.test('required', errorsTrads.required, (value) => {
if (validations.required && !value.length) return false;
if (validations.required && (!value || !value.length)) {
return false;
}
return true;
});

View File

@ -5,14 +5,6 @@ const createDefaultForm = (attributes, allComponentsSchema) => {
const attribute = get(attributes, [current], {});
const { default: defaultValue, component, type, required, min, repeatable } = attribute;
if (type === 'json') {
acc[current] = null;
}
if (type === 'json' && required === true) {
acc[current] = {};
}
if (defaultValue !== undefined) {
acc[current] = defaultValue;
}

View File

@ -11,11 +11,6 @@ describe('CONTENT MANAGER | utils | createDefaultForm', () => {
expect(createDefaultForm(attributes, {})).toEqual({});
});
it('should set the json type with the correct value', () => {
expect(createDefaultForm({ test: { type: 'json' } }, {})).toEqual({ test: null });
expect(createDefaultForm({ test: { type: 'json', required: true } }, {})).toEqual({ test: {} });
});
it('should init the requide dynamic zone type with an empty array', () => {
expect(createDefaultForm({ test: { type: 'dynamiczone', required: true } })).toEqual({
test: [],

View File

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