Skip to content
This repository has been archived by the owner on Apr 12, 2021. It is now read-only.

Latest commit

 

History

History
278 lines (184 loc) · 7.65 KB

README.en.md

File metadata and controls

278 lines (184 loc) · 7.65 KB

validator.js

I am very sorry to announce that I will no longer maintain this project, please refer to other similar projects

npm version GitHub version

A Laravel styled JavaScript Object/Form/JSON validation library. Laravel Validation

  • Support mixin validation rules
  • Support recursive validation on complicate objects
  • Support user-defined validator

validator.js is easy to use in form or JSON validation, and it is extensible.

Example

var example = {
      text: 'Hello world!',
      date: '2015-07-07',
      attachment: {
        name: 'note1',
        count: 1,
      },
      comments: null,
    },
    rules = {
      text: 'required|string',
      date: 'date|date_format:yyyy-MM-dd',
      attachment: {
        name: 'required|string',
        content: 'integer',
      },
      comments: 'array',
    };

console.log(Validator.validate(example, rules));
// => {status: 'failed', [{object: [Object], field: "comments", rule: "array"}]}

Basic Usage

Import validator.js library(for native JavaScript code)

<script type="text/javascript" src="./validator.js"></script>

Or

<script type="text/javascript" src="./dist/validator.min.js"></script>

Initialization(skip this if you are using native JavaScript code)

Node.js

npm install js-validator --save
var validator = require('js-validator');

RequireJS

requirejs(["./validator"], function(validator) {
  ...
});

Sea.js

define(function (require, exports, module) {
  var validator = require('./validator');
  ...
});

Make rules

You can have different rules for a single field, using | as the separator.

var rules = {
  text: 'required|string',
  date: 'date|date_format:yyyy-MM-dd',
  comments: 'integer',
};

About string escape

When '|', ':' or ',' has to be in your rule's values, please add '\\' in front of them, just like this:

var person = {
      nickname: 'Harry|Poter'
    },
    rules = {
      nickname: 'in:Harry\\|Potter,Hermione\\:Granger,Ron\\,Weasley'
    }

Validate the rules

// Validator.validate if you are using native JavaScript code
validator.validate(object_to_be_tested, rules);

Result

Return an object with 'status' and 'rejects' properties.

If the validated object meets all the rules, the 'status' property will be 'success' and the 'rejects' array will be empty; otherwise 'status' will be 'failed' and 'rejects' will contain details that causes the failure.

Add a validator

You can use add() Function to add a validator, along with a name as its first argument and a validation method as second argument. Validation method can either be RegExp object or Function. When it's a Function, its first argument is the object to be tested, the second argument is the value of current validating field, and it should return true when the validation succeeded.

// Validator.add if you are using native JavaScript code
validator.add('older_than', function (object, value, age) {
  return value > age;
});

var rules = {
  age: 'integer|older_than:17',
};

Configuration

// Validator.setConfig if you are using native JavaScript code
validator.setConfig({...});

Available configurations

resumeOnFailed

Default: false

Whether the validation continues when it failed on any rule.

Available Validation Rules

accepted

The field under validation must be yes, on, 1, or true. This is useful for validating "Terms of Service" acceptance.

after:date

The field under validation must be a value after a given date.

alpha

The field under validation must be entirely alphabetic characters.

alpha_dash

The field under validation must be entirely alphabetic characters.

alpha_num

The field under validation must be entirely alpha-numeric characters.

array

The field under validation must be of type array.

before:date

The field under validation must be a value preceding the given date.

between:min,max

The field under validation must have a size between the given min and max. Strings, numerics and files(FileList/File) are evaluated. Files are evaluated in kilobytes.

boolean