Skip to content

Commit

Permalink
fix(setErrorOnSchema): set error as object of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Przodała committed Mar 20, 2020
1 parent 7fa8186 commit 6e286a0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/components/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import isObject from 'lodash/isObject';
import Storage from './Storage';

export const setErrorOnSchema = (schema, path, error) => {
if (isObject(error)) {
if (isObject(error) || Array.isArray(error)) {
Object.keys(error).forEach((key) => {
schema.setModelError(`${path}.${key}`, error[key]);
if (error[key]) {
if (typeof error[key] === 'string') {
schema.setModelError(path, error[key]);
return;
}
setErrorOnSchema(schema, `${path}.${key}`, error[key]);
}
});
return;
}
Expand Down
70 changes: 70 additions & 0 deletions src/demo/book/CustomField.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React, { Component } from 'react';
import Schema from 'form-schema-validation';
import isEmpty from 'lodash/isEmpty';
import PropTypes from 'prop-types';
import { FieldConnect } from '../../components/FieldConnect';

const getInternalSchema = () => new Schema({
name: {
type: String,
validators: [
{
validator: (value) => {
if (value.length === 2) {
return 'FieldValidatorError';
}
return true;
},
},
],
},
surname: {
type: String,
},
age: {
type: String,
},
});

class CustomField extends Component {
constructor(props) {
super(props);
this.state = {};
}

componentDidMount() {
this.context.setFieldValidator(this.setAuthorsValidator);
}

componentWillUnmount() {
this.context.removeFieldValidator(this.setAuthorsValidator);
}


setAuthorsValidator = (_model, value) => {
const schema = getInternalSchema();
const allErrors = [];
value.forEach((author) => {
const errors = schema.validate(author);
allErrors.push(!isEmpty(errors) ? errors : undefined);
});
return allErrors.length ? allErrors : true;
};

render() {
return (
<div />
);
}
}

CustomField.contextTypes = {
setFieldValidator: PropTypes.func,
removeFieldValidator: PropTypes.func,
};

CustomField.propTypes = {};

CustomField.defaultProps = {};

export default FieldConnect(CustomField);
13 changes: 13 additions & 0 deletions src/demo/book/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { listWrapper, objectFormField, objectFieldClassName } from '../demo.css';
import { FormEventsEmitter } from '../../components';
import { consoleData } from '../demoHelpers';
import CustomField from './CustomField';

const options = [
{
Expand All @@ -32,6 +33,16 @@ const personSchema = new Schema({
name: {
type: String,
required: true,
validators: [
{
validator: (value) => {
if (value.length < 3) {
return 'personError';
}
return true;
},
},
],
},
surname: {
type: String,
Expand Down Expand Up @@ -104,6 +115,7 @@ const resetModel = {
status: true,
};


const eventsEmitter = new FormEventsEmitter();
const BookForm = () => (
<Form
Expand All @@ -116,6 +128,7 @@ const BookForm = () => (
<h4>BOOK FORM</h4>
<TextField name="title" type="text" />
<SelectField name="category" />
<CustomField name="authors" />
<ListField
name="authors"
className={listWrapper}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/Form.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ describe('setErrorOnSchema', () => {

it('should set error on schema when error has object structure', () => {
setErrorOnSchema(schema, 'foo.0', { bar: 'barError' });
expect(schema.setModelError).toHaveBeenCalledWith('foo.0.bar', 'barError');
expect(schema.setModelError).toHaveBeenCalledWith('foo.0', 'barError');
});
it('should set error on schema when error is string', () => {
setErrorOnSchema(schema, 'foo.bar', 'barError');
Expand Down

0 comments on commit 6e286a0

Please sign in to comment.