mirror of
https://github.com/strapi/strapi.git
synced 2025-11-12 00:03:40 +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
|
schema = yup
|
||||||
.mixed(errorsTrads.json)
|
.mixed(errorsTrads.json)
|
||||||
.test('isJSON', errorsTrads.json, (value) => {
|
.test('isJSON', errorsTrads.json, (value) => {
|
||||||
|
if (!value || !value.length) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
JSON.parse(value);
|
JSON.parse(value);
|
||||||
|
|
||||||
@ -226,7 +230,9 @@ const createYupSchemaAttribute = (type, validations, options) => {
|
|||||||
})
|
})
|
||||||
.nullable()
|
.nullable()
|
||||||
.test('required', errorsTrads.required, (value) => {
|
.test('required', errorsTrads.required, (value) => {
|
||||||
if (validations.required && !value.length) return false;
|
if (validations.required && (!value || !value.length)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|||||||
@ -5,14 +5,6 @@ const createDefaultForm = (attributes, allComponentsSchema) => {
|
|||||||
const attribute = get(attributes, [current], {});
|
const attribute = get(attributes, [current], {});
|
||||||
const { default: defaultValue, component, type, required, min, repeatable } = attribute;
|
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) {
|
if (defaultValue !== undefined) {
|
||||||
acc[current] = defaultValue;
|
acc[current] = defaultValue;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,11 +11,6 @@ describe('CONTENT MANAGER | utils | createDefaultForm', () => {
|
|||||||
expect(createDefaultForm(attributes, {})).toEqual({});
|
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', () => {
|
it('should init the requide dynamic zone type with an empty array', () => {
|
||||||
expect(createDefaultForm({ test: { type: 'dynamiczone', required: true } })).toEqual({
|
expect(createDefaultForm({ test: { type: 'dynamiczone', required: true } })).toEqual({
|
||||||
test: [],
|
test: [],
|
||||||
|
|||||||
@ -146,7 +146,7 @@ const GenericInput = ({
|
|||||||
required={required}
|
required={required}
|
||||||
onChange={(json) => {
|
onChange={(json) => {
|
||||||
// Default to null when the field is not required and there is no input value
|
// 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 } });
|
onChange({ target: { name, value } });
|
||||||
}}
|
}}
|
||||||
minHeight={pxToRem(252)}
|
minHeight={pxToRem(252)}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user