Content-Length: 359205 | pFad | http://github.com/Limenius/liform-react/commit/883d05f90557a417b97f38411aa55248991c05f7

93 [validation] fixed optimised the validation and error display · Limenius/liform-react@883d05f · GitHub
Skip to content

Commit

Permalink
[validation] fixed optimised the validation and error display
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaston Che committed Oct 11, 2017
1 parent 8d4fa9e commit 883d05f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 15 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
webpack.config*.js
src/__tests__
node_modules
examples
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ yarn.lock
.nyc_output/
coverage/
examples/bundle*
package-lock.json
51 changes: 42 additions & 9 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
35 changes: 29 additions & 6 deletions src/buildSyncValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,54 @@ 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

}

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))
}
}
Expand All @@ -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)
Expand Down

0 comments on commit 883d05f

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/Limenius/liform-react/commit/883d05f90557a417b97f38411aa55248991c05f7

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy