Skip to content

Commit

Permalink
Merge pull request #89 from Emilios1995/pass_field_to_msg
Browse files Browse the repository at this point in the history
Pass the field/key when errorMessage is a function
  • Loading branch information
busypeoples committed Jul 23, 2017
2 parents 7dd0171 + 458bef6 commit e7546e1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
9 changes: 6 additions & 3 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Also works with deep nested objects. `spected` is curried.

#### Arguments

1. `rules` *(Object)*: An object of rules, consisting of arrays containing predicate function / error message tuple, f.e. `{name: [[a => a.length > 2, 'Minimum length 3.']]}`
1. `rules` *(Object)*: An object of rules, consisting of arrays containing predicate function / error message tuple, f.e. `{name: [[a => a.length > 2, 'Minimum length 3.']]}`.
The error message can also be a function with this signature: `(value, key) => message`

2. `input` *(Object)*: The data to be validated.

Expand All @@ -32,13 +33,15 @@ Depending on the status of the input either a `true` or a list of error messages
```js
import spected from 'spected'

const capitalLetterMsg = (value, key) => `The field ${key} should contain at least one uppercase letter. '${value}' is missing an uppercase letter.`

const spec = {
name: [
[isNotEmpty, 'Name should not be empty.']
],
random: [
[isLengthGreaterThan(7), 'Minimum Random length of 8 is required.'],
[hasCapitalLetter, 'Random should contain at least one uppercase letter.'],
[hasCapitalLetter, capitalLetterMsg],
]
}

Expand All @@ -50,7 +53,7 @@ spected(spec, input)
// name: true,
// random: [
// 'Minimum Random length of 8 is required.',
// 'Random should contain at least one uppercase letter.'
// 'The field random should contain at least one uppercase letter. 'r' is missing an uppercase letter.'
// ]
// }

Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ const transform = (successFn: Function, failFn: Function, input: Array<any>): an
*/
const runPredicate = ([predicate, errorMsg]:[Function, string],
value:any,
inputs:Object) => predicate(value, inputs) // eslint-disable-line no-nested-ternary
inputs:Object, field:string) => predicate(value, inputs) // eslint-disable-line no-nested-ternary
? true
: typeof errorMsg === 'function'
? errorMsg(value)
? errorMsg(value, field)
: errorMsg

/**
Expand All @@ -50,7 +50,7 @@ export const validate = curry((successFn: Function, failFn: Function, spec: Obje
const value = input[key]
const predicates = spec[key]
if (Array.isArray(predicates)) {
return { ...result, [key]: transform(() => successFn(value), failFn, map(f => runPredicate(f, value, input), predicates)) }
return { ...result, [key]: transform(() => successFn(value), failFn, map(f => runPredicate(f, value, input, key), predicates)) }
} else if (typeof predicates === 'object') {
return { ...result, [key]: validate(successFn, failFn, predicates, value) }
} else if (typeof predicates === 'function') {
Expand Down
10 changes: 6 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
not,
path,
prop,
flip,
uncurryN
} from 'ramda'

import spected, {validate} from '../src/'
Expand Down Expand Up @@ -483,12 +485,12 @@ describe('spected', () => {
}, result)
})

it('should pass the value to the error message if it is a function', () => {
it('should pass the key and the value to the error message if it is a function', () => {
const validationRules = {
password: [[hasCapitalLetter, capitalLetterMsgWithValue('Password')]],
password: [[hasCapitalLetter, compose(flip, uncurryN(2))(capitalLetterMsgWithValue)]],
}
const result = verify(validationRules, {password: 'foobar'})
deepEqual({password: 'Password should contain at least one uppercase letter. foobar is missing an uppercase letter.'}, result)
const result = verify(validationRules, { password: 'foobar' })
deepEqual({ password: 'password should contain at least one uppercase letter. foobar is missing an uppercase letter.' }, result)
})

it('should work with dynamic rules: an array of inputs', () => {
Expand Down

0 comments on commit e7546e1

Please sign in to comment.