Mr. Checkbox
Validates a value against a given set of requirements. Named after a former colleague.
Mr. Checkbox (the function, not the person) is curried; validators can be created with partially-applied invocation.
Domain
A Requirement
is an object with an error
and validator
. An
error
is any value, usually a string, that is used to identify any values
that fail to meet the requirement. A validator
is a function that accepts a
value as input and returns true
if the requirement is met; false
if it is
not met.
{
error: 'minimumLength',
validator: function (value) { return value.length > 8; }
}
Mr. Checkbox produces an array of error
values. If the string abc
were
validated against the requirements above, the result would be:
['minimumLength']
.
Usage & Examples
Using partial-application.
var mrCheckbox = ; { return value === value;} var validatePassword = ; ; 'Password cannot be "password"' 'Password must be a palindrome.'; ; 'Password must 8 characters or longer' 'Password must be a palindrome.' ; 'Password cannot start with the letter Q.' ; 'Password must be 50 characters or shorter' ;
Or, non-curried invocation.
var mrCheckbox = ; { return value === value;} var requirements = error: 'Password must 8 characters or longer' { return valuelength > 7; } error: 'Password must be 50 characters or shorter' { return valuelength < 51; } error: 'Password cannot be "password"' { return value !== 'password'; } error: 'Password cannot start with the letter Q.' { return value !== 'Q'; } error: 'Password must be a palindrome.' validator: isPalindrome ; ; 'Password cannot be "password"' 'Password must be a palindrome.'; ; 'Password must 8 characters or longer' 'Password must be a palindrome.' ; 'Password cannot start with the letter Q.' ; 'Password must be 50 characters or shorter' ;
Changelog
- v1.1.2: Adds
patch-
,minor-
, andmajor-release
NPM scripts (6/23/16) - v1.1.1: Adds eslint to project (6/21/16)
- v1.1.0: Replaces Ramda's curry with auto-curry (3/14/16)
- v1.0.0: Initial commit (3/11/16)