From 215fb87974c82135620a33d677ed8a30e20d57d1 Mon Sep 17 00:00:00 2001 From: John Rom Date: Fri, 15 Dec 2017 17:05:31 -0500 Subject: [PATCH 1/5] Expose Ajv validation instance via getValidator() to modify configuration, allowing users to validate jsonschema 4.0 --- src/components/Form.js | 21 ++++++++++++--------- src/index.js | 2 ++ src/validate.js | 14 ++++++++++---- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/components/Form.js b/src/components/Form.js index 79215ffae8..0d1ca06857 100644 --- a/src/components/Form.js +++ b/src/components/Form.js @@ -7,9 +7,11 @@ import { shouldRender, toIdSchema, setState, - getDefaultRegistry, + getDefaultRegistry } from "../utils"; -import validateFormData from "../validate"; +import { getValidator, validateFormData } from "../validate"; + +export { getValidator }; export default class Form extends Component { static defaultProps = { @@ -18,7 +20,7 @@ export default class Form extends Component { liveValidate: false, safeRenderCompletion: false, noHtml5Validate: false, - ErrorList: DefaultErrorList, + ErrorList: DefaultErrorList }; constructor(props) { @@ -43,7 +45,7 @@ export default class Form extends Component { ? this.validate(formData, schema) : { errors: state.errors || [], - errorSchema: state.errorSchema || {}, + errorSchema: state.errorSchema || {} }; const idSchema = toIdSchema( schema, @@ -58,7 +60,7 @@ export default class Form extends Component { formData, edit, errors, - errorSchema, + errorSchema }; } @@ -155,7 +157,7 @@ export default class Form extends Component { ObjectFieldTemplate: this.props.ObjectFieldTemplate, FieldTemplate: this.props.FieldTemplate, definitions: this.props.schema.definitions || {}, - formContext: this.props.formContext || {}, + formContext: this.props.formContext || {} }; } @@ -172,7 +174,7 @@ export default class Form extends Component { autocomplete, enctype, acceptcharset, - noHtml5Validate, + noHtml5Validate } = this.props; const { schema, uiSchema, formData, errorSchema, idSchema } = this.state; @@ -191,7 +193,8 @@ export default class Form extends Component { encType={enctype} acceptCharset={acceptcharset} noValidate={noHtml5Validate} - onSubmit={this.onSubmit}> + onSubmit={this.onSubmit} + > {this.renderErrors()} <_SchemaField schema={schema} @@ -251,6 +254,6 @@ if (process.env.NODE_ENV !== "production") { validate: PropTypes.func, transformErrors: PropTypes.func, safeRenderCompletion: PropTypes.bool, - formContext: PropTypes.object, + formContext: PropTypes.object }; } diff --git a/src/index.js b/src/index.js index ee2b982967..1c3d31edbf 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,5 @@ import Form from "./components/Form"; +import { getValidator } from "./components/Form"; +export { getValidator }; export default Form; diff --git a/src/validate.js b/src/validate.js index 1f72b8dcd3..4e28ed523f 100644 --- a/src/validate.js +++ b/src/validate.js @@ -2,7 +2,7 @@ import toPath from "lodash.topath"; import Ajv from "ajv"; const ajv = new Ajv({ errorDataPath: "property", - allErrors: true, + allErrors: true }); // add custom formats ajv.addFormat( @@ -71,7 +71,7 @@ export function toErrorList(errorSchema, fieldName = "root") { errorList = errorList.concat( errorSchema.__errors.map(stack => { return { - stack: `${fieldName}: ${stack}`, + stack: `${fieldName}: ${stack}` }; }) ); @@ -92,7 +92,7 @@ function createErrorHandler(formData) { __errors: [], addError(message) { this.__errors.push(message); - }, + } }; if (isObject(formData)) { return Object.keys(formData).reduce((acc, key) => { @@ -137,11 +137,15 @@ function transformAjvErrors(errors = []) { property, message, params, // specific to ajv - stack: `${property} ${message}`.trim(), + stack: `${property} ${message}`.trim() }; }); } +function getValidator() { + return ajv; +} + /** * This function processes the formData with a user `validate` contributed * function, which receives the form data and an `errorHandler` object that @@ -176,3 +180,5 @@ export default function validateFormData( return { errors: newErrors, errorSchema: newErrorSchema }; } + +export { getValidator, validateFormData }; From 3f95266fb5c3982f2da4b595adf28e7c2e27aa05 Mon Sep 17 00:00:00 2001 From: John Rom Date: Mon, 18 Dec 2017 16:28:11 -0500 Subject: [PATCH 2/5] Rollback formatting changes From 9dd4cce605db26ed9ad65160a16210b1dc63317b Mon Sep 17 00:00:00 2001 From: John Rom Date: Mon, 18 Dec 2017 17:05:44 -0500 Subject: [PATCH 3/5] Rollback formatting fixes. Prettier is not playing nicely with precommit hooks --- src/components/Form.js | 17 ++++++++--------- src/validate.js | 8 ++++---- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/components/Form.js b/src/components/Form.js index 0d1ca06857..d2f8b2a1bc 100644 --- a/src/components/Form.js +++ b/src/components/Form.js @@ -7,7 +7,7 @@ import { shouldRender, toIdSchema, setState, - getDefaultRegistry + getDefaultRegistry, } from "../utils"; import { getValidator, validateFormData } from "../validate"; @@ -20,7 +20,7 @@ export default class Form extends Component { liveValidate: false, safeRenderCompletion: false, noHtml5Validate: false, - ErrorList: DefaultErrorList + ErrorList: DefaultErrorList, }; constructor(props) { @@ -45,7 +45,7 @@ export default class Form extends Component { ? this.validate(formData, schema) : { errors: state.errors || [], - errorSchema: state.errorSchema || {} + errorSchema: state.errorSchema || {}, }; const idSchema = toIdSchema( schema, @@ -60,7 +60,7 @@ export default class Form extends Component { formData, edit, errors, - errorSchema + errorSchema, }; } @@ -157,7 +157,7 @@ export default class Form extends Component { ObjectFieldTemplate: this.props.ObjectFieldTemplate, FieldTemplate: this.props.FieldTemplate, definitions: this.props.schema.definitions || {}, - formContext: this.props.formContext || {} + formContext: this.props.formContext || {}, }; } @@ -174,7 +174,7 @@ export default class Form extends Component { autocomplete, enctype, acceptcharset, - noHtml5Validate + noHtml5Validate, } = this.props; const { schema, uiSchema, formData, errorSchema, idSchema } = this.state; @@ -193,8 +193,7 @@ export default class Form extends Component { encType={enctype} acceptCharset={acceptcharset} noValidate={noHtml5Validate} - onSubmit={this.onSubmit} - > + onSubmit={this.onSubmit}> {this.renderErrors()} <_SchemaField schema={schema} @@ -254,6 +253,6 @@ if (process.env.NODE_ENV !== "production") { validate: PropTypes.func, transformErrors: PropTypes.func, safeRenderCompletion: PropTypes.bool, - formContext: PropTypes.object + formContext: PropTypes.object, }; } diff --git a/src/validate.js b/src/validate.js index 4e28ed523f..ad239f0fef 100644 --- a/src/validate.js +++ b/src/validate.js @@ -2,7 +2,7 @@ import toPath from "lodash.topath"; import Ajv from "ajv"; const ajv = new Ajv({ errorDataPath: "property", - allErrors: true + allErrors: true, }); // add custom formats ajv.addFormat( @@ -71,7 +71,7 @@ export function toErrorList(errorSchema, fieldName = "root") { errorList = errorList.concat( errorSchema.__errors.map(stack => { return { - stack: `${fieldName}: ${stack}` + stack: `${fieldName}: ${stack}`, }; }) ); @@ -92,7 +92,7 @@ function createErrorHandler(formData) { __errors: [], addError(message) { this.__errors.push(message); - } + }, }; if (isObject(formData)) { return Object.keys(formData).reduce((acc, key) => { @@ -137,7 +137,7 @@ function transformAjvErrors(errors = []) { property, message, params, // specific to ajv - stack: `${property} ${message}`.trim() + stack: `${property} ${message}`.trim(), }; }); } From a0e56cbcd9c7bd29706817292d253e91d98177a4 Mon Sep 17 00:00:00 2001 From: John Rom Date: Wed, 14 Mar 2018 12:16:40 -0400 Subject: [PATCH 4/5] Set ajv version because updates broke things --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3a868df3e1..2e1d7963f7 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "react": ">=15" }, "dependencies": { - "ajv": "^5.2.3", + "ajv": "5.2.3", "lodash.topath": "^4.5.2", "prop-types": "^15.5.8", "setimmediate": "^1.0.5" From 971d8e8a507201bb73f2bf03d5bbe282dc76aece Mon Sep 17 00:00:00 2001 From: John Rom Date: Wed, 14 Mar 2018 12:17:06 -0400 Subject: [PATCH 5/5] Revert "Set ajv version because updates broke things" This reverts commit a0e56cbcd9c7bd29706817292d253e91d98177a4. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2e1d7963f7..3a868df3e1 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "react": ">=15" }, "dependencies": { - "ajv": "5.2.3", + "ajv": "^5.2.3", "lodash.topath": "^4.5.2", "prop-types": "^15.5.8", "setimmediate": "^1.0.5"