the-thing-is
"You see, the thing is..."
...now you can thoroughly and precisely measure your subjects against a series of standards.
the('16').is(['present', 'numberString', {greaterThanOrEqualTo:0}]) // true
This thing is great for front-end form validation.
is-too
does comparisons behind the scenes. the-thing-is
handles the organization of multiple checks. See is-too
's README for a list of what's available.
Basic usage
var the = require('the-thing-is')
the('thing').is('present')
Array of Standards
Use an array to describe your subject with a series of attributes that will be evaluated in order.
var whatYouExpect = ['present', 'integer', {greaterThan:0, lessThan:256}]
the(16).is(whatYouExpect)
the(640).is(whatYouExpect)
Tree of Standards
This is where the true value of the-thing-is
is. Trees can be as deep as you want, the-thing-is
works through it recursively.
Should the check fail, it'll record the path to the offending branch of the tree and store it as an error.
var userSchema = {
name: ['string'],
address: {
street1: ['present', 'string', {matches: /.*/}],
street2: ['string'],
city: ['present', 'string'],
state: ['present', 'string', {matches: /^[A-Z]{2}$/}],
zip: ['present', 'string', {matches: /^[0-9]{5}$/}]
}
}
var user = {
name: "Joe Bob",
address: {
street1: '123 Any St.',
street2: '',
city: 'Anytown',
state: 'NE',
zip: 12345
}
}
the(user).is(userSchema)
the.last.error
Negative Standard
If your subject is like an unruly teenager and you expect it to fail to live up to your standards all the time, then just say so.
var teenager = undefined
the(teenager).isnt('present')
the.last.error
Errors
If your subject fails to live up to the standard you've set then the-thing-is
will dutifully list all the ways it does so, in a way intended to be useful enough for you to deliver a useful message to your users.
- Failed simple checks will return a string.
- Failed objects will return an object with a key referring to the branch in the tree, and an array of failures.
Altogether, the-thing-is
will return an array of all your subject's failures.
the.last.error == ['number']
the.last.error == [{greaterThan:0}]
the.last.error == [{'foo.bar': ['number']}]
the.last.error == [{'foo.bar': ['number', {greaterThan:0}]}]
Additionally, if you describe your object using standards that don't exist in is-too
then the-thing-is
will throw a TypeError.
the('thing').is('gonnaThrowUp')