Merge pull request #4465 from strapi/fix/json

Fix non required JSON
This commit is contained in:
Alexandre BODIN 2019-11-18 10:11:41 +01:00 committed by GitHub
commit 04b51784c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 62 deletions

View File

@ -1,12 +0,0 @@
import React from 'react';
import { shallow } from 'enzyme';
import { LoadingIndicatorPage } from 'strapi-helper-plugin';
import { App } from '../../App';
describe('<App />', () => {
it('should render the <AppLoader />', () => {
const renderedComponent = shallow(<App getDataSucceeded={jest.fn()} />);
expect(renderedComponent.find(LoadingIndicatorPage)).toHaveLength(1);
});
});

View File

@ -6,7 +6,7 @@
/* eslint-disable react/require-default-props */ /* eslint-disable react/require-default-props */
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { isEmpty, isObject, merge } from 'lodash'; import { isEmpty, merge } from 'lodash';
// Design // Design
import InputAddonWithErrors from '../InputAddonWithErrors'; import InputAddonWithErrors from '../InputAddonWithErrors';
@ -22,7 +22,11 @@ import InputTextAreaWithErrors from '../InputTextAreaWithErrors';
import InputTextWithErrors from '../InputTextWithErrors'; import InputTextWithErrors from '../InputTextWithErrors';
import InputToggleWithErrors from '../InputToggleWithErrors'; import InputToggleWithErrors from '../InputToggleWithErrors';
const DefaultInputError = ({ type }) => <div>Your input type: <b>{type}</b> does not exist</div>; const DefaultInputError = ({ type }) => (
<div>
Your input type: <b>{type}</b> does not exist
</div>
);
const inputs = { const inputs = {
addon: InputAddonWithErrors, addon: InputAddonWithErrors,
@ -55,7 +59,7 @@ function InputsIndex(props) {
inputValue = props.value || []; inputValue = props.value || [];
break; break;
case 'json': case 'json':
inputValue = isObject(props.value) ? props.value : null; inputValue = props.value || null;
break; break;
default: default:
inputValue = props.value || ''; inputValue = props.value || '';
@ -78,10 +82,7 @@ InputsIndex.defaultProps = {
}; };
InputsIndex.propTypes = { InputsIndex.propTypes = {
addon: PropTypes.oneOfType([ addon: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
PropTypes.bool,
PropTypes.string,
]),
customInputs: PropTypes.object, customInputs: PropTypes.object,
type: PropTypes.string.isRequired, type: PropTypes.string.isRequired,
value: PropTypes.any, value: PropTypes.any,

View File

@ -15,13 +15,12 @@ import 'codemirror/addon/selection/mark-selection';
import 'codemirror/lib/codemirror.css'; import 'codemirror/lib/codemirror.css';
import 'codemirror/theme/3024-night.css'; import 'codemirror/theme/3024-night.css';
import { isEmpty, isObject, trimStart } from 'lodash'; import { isEmpty, trimStart } from 'lodash';
import jsonlint from './jsonlint'; import jsonlint from './jsonlint';
import Wrapper from './components'; import Wrapper from './components';
const WAIT = 600; const WAIT = 600;
const stringify = JSON.stringify; const stringify = JSON.stringify;
const parse = JSON.parse;
const DEFAULT_THEME = '3024-night'; const DEFAULT_THEME = '3024-night';
class InputJSON extends React.Component { class InputJSON extends React.Component {
@ -65,16 +64,15 @@ class InputJSON extends React.Component {
setInitValue = () => { setInitValue = () => {
const { value } = this.props; const { value } = this.props;
if (isObject(value) && value !== null) {
try { try {
parse(stringify(value));
this.setState({ hasInitValue: true }); this.setState({ hasInitValue: true });
if (value === null) return this.codeMirror.setValue('');
return this.codeMirror.setValue(stringify(value, null, 2)); return this.codeMirror.setValue(stringify(value, null, 2));
} catch (err) { } catch (err) {
return this.setState({ error: true }); return this.setState({ error: true });
} }
}
}; };
setSize = () => this.codeMirror.setSize('100%', 'auto'); setSize = () => this.codeMirror.setSize('100%', 'auto');
@ -125,10 +123,8 @@ class InputJSON extends React.Component {
const { name, onChange } = this.props; const { name, onChange } = this.props;
let value = this.codeMirror.getValue(); let value = this.codeMirror.getValue();
try { if (value === '') {
value = parse(value); value = null;
} catch (err) {
// Silent
} }
// Update the parent // Update the parent

View File

@ -217,11 +217,7 @@ InputJSONWithErrors.propTypes = {
resetProps: PropTypes.bool, resetProps: PropTypes.bool,
tabIndex: PropTypes.string, tabIndex: PropTypes.string,
validations: PropTypes.object, validations: PropTypes.object,
value: PropTypes.oneOfType([ value: PropTypes.any,
PropTypes.array,
PropTypes.object,
PropTypes.bool,
]),
}; };
export default InputJSONWithErrors; export default InputJSONWithErrors;

View File

@ -19,7 +19,7 @@ export const cleanData = (retrievedData, ctLayout, groupLayouts) => {
switch (attrType) { switch (attrType) {
case 'json': case 'json':
cleanedData = value; cleanedData = JSON.parse(value);
break; break;
case 'date': case 'date':
cleanedData = cleanedData =

View File

@ -1,12 +1,4 @@
import { import { get, isBoolean, isNaN } from 'lodash';
get,
isBoolean,
isNaN,
isNumber,
isNull,
isArray,
isObject,
} from 'lodash';
import * as yup from 'yup'; import * as yup from 'yup';
import { translatedErrors as errorsTrads } from 'strapi-helper-plugin'; import { translatedErrors as errorsTrads } from 'strapi-helper-plugin';
@ -99,20 +91,13 @@ const createYupSchemaAttribute = (type, validations) => {
schema = yup schema = yup
.mixed(errorsTrads.json) .mixed(errorsTrads.json)
.test('isJSON', errorsTrads.json, value => { .test('isJSON', errorsTrads.json, value => {
try { if (value === undefined) {
if (
isObject(value) ||
isBoolean(value) ||
isNumber(value) ||
isArray(value) ||
isNaN(value) ||
isNull(value)
) {
JSON.parse(JSON.stringify(value));
return true; return true;
} }
return false; try {
JSON.parse(value);
return true;
} catch (err) { } catch (err) {
return false; return false;
} }