This is an enhanced _.pick
that runs validation expressions on picked attributes.
The function signature looks much like the one you're already used to: pick(object, [expressions...], [thisArg])
In addition to the standard usage of _.pick
pick({name: 'moe', age: 50, userid: 'moe1'}, 'name', 'age')
=> {name: 'moe', age: 50}
...you can do this with pedantic-pick:
pick({name: 'moe', age: 50, userid: 'moe1'}, '!string::name', 'number::age')
=> {name: 'moe', age: 50}
or, of course, use the shorthand form:
pick({name: 'moe', age: 50, userid: 'moe1'}, '!s::name', 'n::age')
=> {name: 'moe', age: 50}
and when something doesn't pass your rules an error is thrown (protip: use try/catch):
pick({name: 'moe', age: 50, userid: 'moe1'}, '!s::name', '!alias')
=> Error: alias failed validation
Each given expression argument must conform to this "grammar": [!][validator::]key
The following validators are built-in (and later we'll accept custom validation functions as arguments):
- required (prefix the expression with
!
) - number (or
num
orn
) - boolean (or
bool
orb
) - function (or
fun
orf
) - object (or
o
) - string (or
s
) - array (or
a
) - nempstring (non-empty string; or
nes
) - nemparray (non-empty array; or
nea
)
See LICENSE