mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 18:33:55 +00:00
Merge pull request #15778 from strapi/fix/non-required-json-input
This commit is contained in:
commit
f78ba41c59
@ -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;
|
||||
});
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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: [],
|
||||
|
||||
@ -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)}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user