1.2.1 • Published 8 years ago

field-test v1.2.1

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

field-test

This is a small object schema validation test builder. It does not come with a validation library built in. You simply provide a schema with test functions to test against object properties, and provide error messages if those tests fail.

Ideally, object properties should be strings but they can be anything you choose.

NPM

Quick Example

/* works great for form validation */

const validate = require('field-test')

/* schema */
const schema = {
  username: [{
    test: function (str) { return str.length >=5 && str.length < 20 },
    message: 'username must contain 5 - 20 characters'
  }],
  email: [{
    test: function (str) { return /@/.test(str) },
    message: 'email must contain an @ character'
  },
  {
    test: function (str) { return /\.com/.test(str) },
    message: 'email must contain a .com extension'
  }]
}

validate({ username: 'java', email: 'javascript@rules.com' }, schema)

/* returns { username: ['username must contain 5 - 20 characters'] } */

Why?

Most of the validation and object schema validation libraries come with a lot of validation and sanitation methods that you might not need. Some of those libraries also don't have useful error messages (looking at you Joi). This allows you to write as many tests as you like with whatever error messages you want.

Each test also accepts an argument property if you need to pass a configuration object to your test. This is useful if you choose to use this with a validation library,

example

/* import validator library from npm using es6 */

import { isLength, isEmail } from 'validator'
import validate from 'field-test'

/* using same example as above */
const schema = {
  username: [{
    test: isLength,
    argument: { min: 5, max: 20 },
    message: 'username must be between 5 - 20 characters'
  }],
  password: [{
    test: isEmail,
    message: 'email must be valid email format'
  }]
}

validate({ username: 'java', 'javascript@rules.com' }, schema)

/* returns { username: ['username must contain 5 - 20 characters'] } */

If all tests pass field-test will just return an empty object

1.2.1

8 years ago

1.2.0

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago