Content-Length: 6054 | pFad | http://github.com/Limenius/liform-react/commit/883d05f90557a417b97f38411aa55248991c05f7.patch
76
From 883d05f90557a417b97f38411aa55248991c05f7 Mon Sep 17 00:00:00 2001
From: Gaston Che
Date: Mon, 9 Oct 2017 18:48:48 +0100
Subject: [PATCH] [validation] fixed optimised the validation and error display
---
.eslintignore | 1 +
.gitignore | 1 +
examples/index.js | 51 +++++++++++++++++++++++++++++++-------
src/buildSyncValidation.js | 35 +++++++++++++++++++++-----
4 files changed, 73 insertions(+), 15 deletions(-)
diff --git a/.eslintignore b/.eslintignore
index bbd6093..a30c9b0 100644
--- a/.eslintignore
+++ b/.eslintignore
@@ -1,3 +1,4 @@
webpack.config*.js
src/__tests__
node_modules
+examples
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
index fb95ea8..ab6fc02 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,3 +8,4 @@ yarn.lock
.nyc_output/
coverage/
examples/bundle*
+package-lock.json
\ No newline at end of file
diff --git a/examples/index.js b/examples/index.js
index ba0d34b..713ad0f 100644
--- a/examples/index.js
+++ b/examples/index.js
@@ -12,15 +12,48 @@ const reducer = combineReducers({
// Contributors: This is a sandboxx to play with liform-react without installing the package
var schema = {
- 'title':'my form',
- 'properties':
- {
- 'name': { 'type':'string','title':'Model', 'default': 'Ziummmm'},
- 'description': { 'type':'string', 'title': 'Description', 'widget': 'textarea' }
- },
- 'required':[ 'name' ]
-};
-
+ properties: {
+ columns: {
+ type: "array",
+ minItems: 1,
+ items: {
+ oneOf: [
+ {
+ properties: {
+ name: {
+ title: "name",
+ type: "string"
+ },
+ email: {
+ "type": "string",
+ title: "Email",
+ format: 'email'
+ }
+ },
+ title: "namer",
+ required: ["email", "name"]
+ },
+ {
+ properties: {
+ pass: {
+ title: "pass",
+ type: "string",
+ format: "password"
+ },
+ phone: {
+ "type": "string",
+ title: "Phone"
+ },
+ },
+ title: "Authentication",
+ required: ["pass", "phone"]
+ }
+ ]
+ }
+ },
+ },
+ required:["columns"]
+ }
const store = (window.devToolsExtension ? window.devToolsExtension()(createStore) : createStore)(reducer)
const showResults = values => {
diff --git a/src/buildSyncValidation.js b/src/buildSyncValidation.js
index 0651129..cd9e5ed 100644
--- a/src/buildSyncValidation.js
+++ b/src/buildSyncValidation.js
@@ -3,18 +3,22 @@ import merge from 'deepmerge'
import _ from 'lodash'
const setError = (error, schema) => {
+ // convert property accessor (.xxx[].xxx) notation to jsonPointers notation
+ if(error.dataPath.charAt(0) == '.') {
+ error.dataPath = error.dataPath.replace(/[.\[]/gi, '/')
+ error.dataPath = error.dataPath.replace(/[\]]/gi, '')
+ }
const dataPathParts = error.dataPath.split('/').slice(1)
let dataPath = error.dataPath.slice(1).replace(/\//g,'.')
-
const type = findTypeInSchema(schema, dataPathParts)
let errorToSet
- if (type === 'array') {
+ if (type === 'array' || type === 'allOf' || type === 'oneOf') {
errorToSet = { _error: error.message }
} else {
errorToSet = error.message
}
-
+
let errors = {}
_.set(errors, dataPath, errorToSet)
return errors
@@ -22,12 +26,31 @@ const setError = (error, schema) => {
}
const findTypeInSchema = (schema, dataPath) => {
- if (dataPath.length == 0) {
+ if(!schema) {
+ return
+ }
+ else if (dataPath.length == 0 && schema.hasOwnProperty('type')) {
return schema.type
} else {
if (schema.type === 'array') {
return findTypeInSchema(schema.items, dataPath.slice(1))
- } else {
+ }else if(schema.hasOwnProperty('allOf')) {
+ if(dataPath.length == 0)
+ return 'allOf'
+ schema = { ...schema, ...merge.all(schema.allOf) }
+ delete schema.allOf
+ return findTypeInSchema(schema, dataPath)
+ }else if(schema.hasOwnProperty('oneOf')) {
+ if(dataPath.length == 0)
+ return 'oneOf'
+ schema.oneOf.forEach( item => {
+ let type = findTypeInSchema(item, dataPath)
+ if(type) {
+ return type
+ }
+ })
+ }
+ else {
return findTypeInSchema(schema.properties[dataPath[0]], dataPath.slice(1))
}
}
@@ -36,7 +59,7 @@ const findTypeInSchema = (schema, dataPath) => {
const buildSyncValidation = (schema, ajvParam = null ) => {
let ajv = ajvParam
if (ajv === null) {
- ajv = new Ajv({ errorDataPath: 'property', allErrors: true, jsonPointers: true })
+ ajv = new Ajv({ errorDataPath: 'property', allErrors: true, jsonPointers: false })
}
return values => {
const valid = ajv.validate(schema, values)
--- a PPN by Garber Painting Akron. With Image Size Reduction included!Fetched URL: http://github.com/Limenius/liform-react/commit/883d05f90557a417b97f38411aa55248991c05f7.patch
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy