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 */
import React from 'react';
import PropTypes from 'prop-types';
import { isEmpty, isObject, merge } from 'lodash';
import { isEmpty, merge } from 'lodash';
// Design
import InputAddonWithErrors from '../InputAddonWithErrors';
@ -22,7 +22,11 @@ import InputTextAreaWithErrors from '../InputTextAreaWithErrors';
import InputTextWithErrors from '../InputTextWithErrors';
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 = {
addon: InputAddonWithErrors,
@ -55,14 +59,14 @@ function InputsIndex(props) {
inputValue = props.value || [];
break;
case 'json':
inputValue = isObject(props.value) ? props.value : null;
inputValue = props.value || null;
break;
default:
inputValue = props.value || '';
}
merge(inputs, props.customInputs);
const Input = inputs[type] ? inputs[type] : DefaultInputError;
return <Input {...props} value={inputValue} />;
@ -78,10 +82,7 @@ InputsIndex.defaultProps = {
};
InputsIndex.propTypes = {
addon: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string,
]),
addon: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
customInputs: PropTypes.object,
type: PropTypes.string.isRequired,
value: PropTypes.any,

View File

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

View File

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

View File

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

View File

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