Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass the field/key when errorMessage is a function #89

Merged
merged 4 commits into from
Jul 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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