Keith
An extendable tool used for basic data validation that doesn't force you into a specific debugging path.
Installation
Simply use npm
to install keith
!
npm install keith
Supported Checks
keith
supports all basic data validation; we'll be adding options to extend its functionality soon.
Currently, that list consists of the following:
Usage
In its basic form, keith
wil check parameters given to it and return a list of issues.
In this case, everything's fine.
const keith = let foo = 'I am a string.'let errors = console// null
But if we change the value of foo
:
let foo = falselet errors = console// [ 'Parameter is not a string.' ]
We can also provide names to our singularly-checked variables for prettier messages:
let errors = console// [ '"foo" is not a string.' ]
Keith can keep checking as many parameters as you want:
let foo = 'I am a string'let bar = truelet baz = 24let qux = one: 'two' let errors = console/* [ '"foo" is not a number.', '"baz" is not a string.', '"qux" is not a array.' ] */
You can scan through objects (and nested objects) with keith
too, even in the same chain as individual checks:
let args = username: 'john' email: 'john@doe.net' gender: 'male' age: 24 location: place: 'London' let yarn = false let errors = console/* [ 'Key "dob" does not exist.', 'Key "dob" should be a date.', '"Yarn" is not a string.' ] */
Optional checks are also available when scanning objects, meaning validation will be applied if a key exists, otherwise will be ignored:
let args = username: 'john' bio: false let errors = console// [ 'Key "bio" should be a string.' ]
keith
can even build an object for you as you parse parameters. Simply pass either a blank object or an object of defaults into the keith()
call:
let args = username: 'john' email: 'john@doe.net' gender: 'male' age: 24 llamas: true let checker = console// null console// {service: 'Google', username: 'john', age: 24}