Skip to content

Commit

Permalink
feat: extend custom messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Idered committed Aug 22, 2017
1 parent 88c35ad commit 3c71e09
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 5 additions & 6 deletions examples/basic.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { validate } = require('../src')
const {validate} = require('../src')

export default () => {
const data = {
firstName: 'John',
firstName: '',
lastName: 'Doe',
age: [16, 24],
gender: 'male'
Expand All @@ -12,16 +12,15 @@ export default () => {
firstName: 'required|min:2',
lastName: 'required|min:2',
'age.*': 'required|numeric|min:18'
// gender: 'in:male,female|exists:tag,name'
}

const messages = {
required: 'field is required',
'firstName.required': 'First name is required',
'age.*': {
min: 'Every age must be at least :min'
}
}

validate(data, rules, messages)
.then(() => console.log())
.catch(console.log)
validate(data, rules, messages).then(console.log).catch(console.log)
}
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,17 @@ class Validator {

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

console.log(this.customMEssages, attribute, lowerRule)

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

Expand Down

0 comments on commit 3c71e09

Please sign in to comment.