Skip to content

Commit

Permalink
feat: implement array validation and custom messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Idered committed Aug 22, 2017
1 parent d255377 commit 88c35ad
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
14 changes: 10 additions & 4 deletions examples/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ export default () => {
const data = {
firstName: 'John',
lastName: 'Doe',
age: 16,
age: [16, 24],
gender: 'male'
}

const rules = {
firstName: 'required|min:2',
lastName: 'required|min:2',
age: 'required|numeric|min:18',
gender: 'in:male,female|exists:tag,name'
'age.*': 'required|numeric|min:18'
// gender: 'in:male,female|exists:tag,name'
}

validate(data, rules)
const messages = {
'age.*': {
min: 'Every age must be at least :min'
}
}

validate(data, rules, messages)
.then(() => console.log())
.catch(console.log)
}
57 changes: 49 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ class Validator {

setRules(rules) {
// Map attribute to array of rules
this.rules = Object.keys(rules).reduce((all, attribute) => ({
...all,
[attribute]: this.parseRules(rules[attribute])
}), {})
this.rules = Object.keys(rules).reduce((all, attribute) => {
if (attribute.indexOf('*') >= 0) {
this.wildcards.push(attribute.split('.')[0])
}

return {
...all,
[attribute.split('.')[0]]: this.parseRules(rules[attribute])
}
}, {})
}

parseRules(rules) {
Expand Down Expand Up @@ -57,7 +63,10 @@ class Validator {
rule === 'Bool' ? 'Boolean' : rule
}

validate(data = {}, rules = {}) {
validate(data = {}, rules = {}, messages = {}) {
this.customMessages = messages
this.wildcards = []

return new Promise((resolve, reject) => {
try {
this.setData(data)
Expand Down Expand Up @@ -95,20 +104,37 @@ class Validator {
return rulesToResolve
}

isWildcard(attribute) {
return this.wildcards.indexOf(attribute) >= 0
}

validateAttribute(attribute, { rule, parameters }) {
// TODO: Handle file input
const value = this.data[attribute]
const method = validators[`validate${rule}`]

if (method) {
const passed = method.call(this, attribute, value, parameters)
const value = this.data[attribute]
let passed

if (this.isWildcard(attribute)) {
passed = value
.map(item => method.call(this, attribute, item, parameters))
} else {
passed = method.call(this, attribute, value, parameters)
}

if (passed instanceof Promise) {
passed.then(result => {
if (!result) {
this.addError(attribute, rule, parameters)
}
})
} else if (Array.isArray(passed)) {
passed.forEach(item => {
if (item === false) {
this.addError(attribute, rule, parameters)
}
})
} else if (!passed) {
this.addError(attribute, rule, parameters)
}
Expand Down Expand Up @@ -137,7 +163,22 @@ class Validator {
message = message[type]
}

return message
const customMessage = this.getCustomMessage(attribute, rule)

return customMessage || message
}

getCustomMessage(attribute, rule) {
const lowerRule = snakeCase(rule)

attribute = this.isWildcard(attribute) ? `${attribute}.*` : attribute

if (
this.customMessages[attribute] &&
this.customMessages[attribute][lowerRule]
) {
return this.customMessages[attribute][lowerRule]
}
}

hasRule(attribute, rules) {
Expand Down

0 comments on commit 88c35ad

Please sign in to comment.