Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Configuration Driven Validation
JavaScript

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
test
.gitignore
.jshintrc
LICENSE
README.md
package.json
the-thing-is.js

README.md

the-thing-is

"You see, the thing is..."

...now you've got a simple tool for doing complex object validations. It's a bit like Joi, only leaner so it's more suitable for the front-end.

the-thing-is uses is-too for the comparisons. See its README for a list of what's available.

Array of Standards

If you've got a single variable that needs to be within certain bounds then use an array to describe it with a series of checks that will be evaluated in order.

var whatYouExpect = ['present', 'integer', {greaterThan:0, lessThan:256}]

the(16).is(whatYouExpect) // true
the(640).is(whatYouExpect) // false

Tree of Standards

This is where the true value of the-thing-is is. Trees can be as deep as you want, and the-thing-is walks through it to check each node. Should the check fail, it'll record the path to the offending node on the tree and store it in the.last.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) // false
the.last.error // [{ 'address.zip': ['string'] }]

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') // true
the.last.thing // undefined
the.last.error // ['present']

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')
// => TypeError("`gonnaThrowUp` isn't a valid comparison method.")
Something went wrong with that request. Please try again.