Validate objects with meaningful errors

Validate objects with meaningful errors.

$ npm install checkfor

Define a validator:

checkfor = require('checkfor')
validate = checkfor({
  title: { is: String, required: true, len: [2, 150] },
  content: String

and run it against objects:

validate({ title: 'hello', content: 'Lorem ipsum' })
// => undefined 
validate({ title: '' })
// => [Error "title" needs to be at least 2 characthers length.] 

See the reference and tests for more info.

Defines the expected type. String, Number and Boolean supported. Objects and Arrays aren't implemented.

Validation fails if the content doesn't have the expected field. It's false by default.

Specifies allowed Regex character groups for a string:

{ is: String, allowed: ['a-z', '0-9', '-', '_'] }

Fails if the input doesn't match the defined regex pattern:

{ is: String, matches: /^[a-z]+$/ }

Fails if the input is not a valid e-mail:

{ is: String, email: true }

Fails if the input is not longer/shorter than expected:

{ is: String, len: [3, 24] }

Fails if the input is higher than expected:

{ is: Number, lower: 100 }
  • 99 will pass.
  • 100+ will fail.

Fails if the input is lower than expected:

{ is: Number, higher: 0 }
  • 1 will pass.
  • 0 and lower numbers will fail.