Skip to content

Commit

Permalink
Merge 1afdc52 into 3a3b017
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Jan 8, 2015
2 parents 3a3b017 + 1afdc52 commit 2daa08e
Show file tree
Hide file tree
Showing 11 changed files with 991 additions and 703 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ logs
results

node_modules
npm-debug.log
npm-debug.log

coverage/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ lib-cov/
Makefile
.travis.yml
logo.png
coverage/
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '0.10'
- '0.8'
script: make test-coveralls
- "0.11"
script: "make test-travis"
after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls"
36 changes: 22 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
TESTS = test/*.test.js
REPORTER = spec
TIMEOUT = 1000
REPORTER = tap
TIMEOUT = 3000
MOCHA_OPTS =

install:
@npm install
@npm install --registry=http://registry.npm.taobao.org

test: install
@NODE_ENV=test ./node_modules/mocha/bin/mocha \
test:
@NODE_ENV=test ./node_modules/mocha/bin/mocha\
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
--require should \
$(MOCHA_OPTS) \
$(TESTS)

test-cov:
@rm -f coverage.html
@$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=html-cov > coverage.html
@$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=travis-cov
@ls -lh coverage.html
@NODE_ENV=test node \
node_modules/.bin/istanbul cover \
./node_modules/.bin/_mocha \
-- -u exports \
--require should \
$(TESTS) \
--bail

test-coveralls: test
@echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID)
@-$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js

test-all: test test-cov
test-travis:
@NODE_ENV=test node \
node_modules/.bin/istanbul cover \
./node_modules/.bin/_mocha \
--report lcovonly \
-- -u exports \
--require should \
$(TESTS) \
--bail

.PHONY: test
266 changes: 119 additions & 147 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
parameter [![Build Status](https://secure.travis-ci.org/fengmk2/parameter.png)](http://travis-ci.org/fengmk2/parameter) [![Coverage Status](https://coveralls.io/repos/fengmk2/parameter/badge.png)](https://coveralls.io/r/fengmk2/parameter)
parameter
=======

[![NPM](https://nodei.co/npm/parameter.png?downloads=true&stars=true)](https://nodei.co/npm/parameter/)
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![David deps][david-image]][david-url]
[![node version][node-image]][node-url]

[npm-image]: https://img.shields.io/npm/v/parameter.svg?style=flat-square
[npm-url]: https://npmjs.org/package/parameter
[travis-image]: https://img.shields.io/travis/node-modules/parameter.svg?style=flat-square
[travis-url]: https://travis-ci.org/node-modules/parameter
[coveralls-image]: https://img.shields.io/coveralls/node-modules/parameter.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/node-modules/parameter?branch=master
[david-image]: https://img.shields.io/david/node-modules/parameter.svg?style=flat-square
[david-url]: https://david-dm.org/node-modules/parameter
[node-image]: https://img.shields.io/badge/node.js-%3E=_0.10-green.svg?style=flat-square
[node-url]: http://nodejs.org/download/

![logo](https://raw.github.com/fengmk2/parameter/master/logo.png)

Expand All @@ -10,174 +25,131 @@ A parameter verify tools.
## Install

```bash
$ npm install parameter
$ npm install parameter --save
```

## Docs
## Usage

```js
/**
* Verify data with rules.
*
* @param {Object} data input data parameters
* @param {Object} rules verify rules
* {
* key1: parameter.Id, // must be string and number value, e.g.: '123', '043624'
* // key1: { isId: true },
* key2: parameter.Date, // must match 'YYYY-MM-DD' format date string, e.g.: '2013-06-25'
* // key2: { isDate: true },
* key3: parameter.DateTime, // must match 'YYYY-MM-DD hh:mm:ss' format date string, e.g.: '2013-06-25 12:20:50'
* // key3: { isDateTime: true },
* key4: 'number', // must match `typeof` result, could be 'number', 'string', 'function' or 'object'
* // key4: { type: 'number' },
* key5: { required: false, type: 'number' }, // optional value, if set, must be a number
* key6: { required: false, type: parameter.Date }, // optional value, if set, must be a date string
* key7: { isArray: true, resourceName: 'User', rules: { name: 'string', age: 'number' } }
* // key7: parameter.Array,
* // key7: { type: parameter.Array, resource: 'User', rules: { name: 'string', age: 'number' } }
* key8: { isObject: true, resourceName: 'User', rules: { name: 'string', age: 'number' } }
* // key8: parameter.Object,
* // key8: { type: parameter.Object, resource: 'User', rules: { name: 'string', age: 'number' } }
* key9: /^\d+$/, // must match number or number string.
* key10: ['y', 'm', 'd', 'w'], // must match enums
* }
* rules's keys must exists on `data`. If `data.key1` not exists, will got `missing_field` error.
* @param {String} [resourceName] error resource name, default is 'Param'
*
* @return {null|Array} errors, return null meaning no errors.
* e.g.: [{
* resource: 'Param',
* field: 'id',
* message: 'id required',
* code: 'missing_field'
* }, ... ]
*/
```
### API

## Usage
- `validate(rule, value)` - validate the `value` conforms to `rule`. return an array of errors if break rule.

### Example

```js
var p = require('parameter');
var validate = require('parameter');

var data = {
id: '043624',
nick: '苏千',
date: '2013-06-25',
age: 29,
sid: 123, // or '123'
uid: '456',
unit: 'y',
name: 'foo',
age: 24,
gender: 'male'
};

var wrongData = {
nick: 123,
date: '2013-06-1',
age: '29',
sex: 0,
sid: '123foo',
uid: 'bar',
unit: 'yy',
var rule = {
name: 'string',
age: 'int',
gender: ['male', 'female', 'unknown']
};

var rules = {
id: p.Id,
nick: 'string',
date: p.Date,
age: 'number',
sex: { required: false, type: 'string' },
sid: /^\d+$/,
uid: { type: /^\d+$/, message: 'should be digital' }, // custom error message
unit: ['y', 'm', 'd', 'w'],
};
var errors = p.verify(data, rules);
// errors => null

var errors = p.verify(wrongData, rules);
// errors =>
// [ { resource: 'Param',
// field: 'id',
// message: 'id required',
// code: 'missing_field' },
// { resource: 'Param',
// field: 'nick',
// message: 'expect string, but got number',
// code: 'invalid' },
// { resource: 'Param',
// field: 'date',
// message: 'should be "YYYY-MM-DD" date format string',
// code: 'invalid' },
// { resource: 'Param',
// field: 'age',
// message: 'expect number, but got string',
// code: 'invalid' },
// { resource: 'Param',
// field: 'sex',
// message: 'expect string, but got number',
// code: 'invalid' },
// { resource: 'Param',
// field: 'sid',
// message: 'should match /^\d+$/',
// code: 'invalid' },
// { resource: 'Param',
// field: 'unit',
// message: 'should match one of ["y", "m", "d", "w"]',
// code: 'invalid' },
// { resource: 'Param',
// field: 'uid',
// message: 'should be digital',
// code: 'invalid' } ]
var errors = validate(data, rule);
```

## Performance
#### [complex example](example.js)

```bash
$ node benchmark.js

rules pass: {id: p.Id} x 5,369,593 ops/sec ±3.41% (91 runs sampled)
rules pass: {id: {type: p.Id}} x 3,280,905 ops/sec ±4.18% (92 runs sampled)
rules pass: {id: {idId: true}} x 3,668,805 ops/sec ±1.60% (95 runs sampled)
rules pass: {date: p.Date} x 3,039,691 ops/sec ±1.56% (94 runs sampled)
rules pass: {date: {type: p.Date}} x 3,155,415 ops/sec ±1.39% (96 runs sampled)
rules pass: {date: {isDate: true}} x 3,165,767 ops/sec ±1.21% (96 runs sampled)
rules pass: {time: p.DateTime} x 3,035,140 ops/sec ±1.07% (92 runs sampled)
rules pass: {time: {type: p.DateTime}} x 3,201,006 ops/sec ±1.51% (93 runs sampled)
rules pass: {time: {isDateTime: true}} x 3,249,725 ops/sec ±0.69% (96 runs sampled)
rules pass: {age: "number"} x 5,065,678 ops/sec ±1.03% (92 runs sampled)
rules pass: {age: {type: "number"}} x 4,984,908 ops/sec ±3.26% (90 runs sampled)
rules pass: {nick: "string"} x 4,194,690 ops/sec ±0.73% (97 runs sampled)
rules pass: {nick: {type: "string"}} x 4,124,614 ops/sec ±0.99% (91 runs sampled)
rules pass: {not_exists: "string", required: false} x 986,325 ops/sec ±4.45% (89 runs sampled)
rules pass: {age: /^\d+$/} x 3,076,511 ops/sec ±1.20% (93 runs sampled)
rules pass: {age: {type: /^\d+$/}} x 3,112,817 ops/sec ±4.81% (88 runs sampled)
rules pass: {age: {type: /^\d+$/, message: "should be digital"}} x 3,289,649 ops/sec ±3.47% (93 runs sampled)
rules fail: {age: {type: /^\d+$/}} x 2,229,454 ops/sec ±2.49% (88 runs sampled)
rules fail: {age: {type: /^\d+$/, message: "should be digital"}} x 2,384,240 ops/sec ±3.62% (95 runs sampled)
rules pass: {unit: ["y", "m", "w", "d"]} x 3,782,871 ops/sec ±4.65% (88 runs sampled)
rules pass: {unit: {type: ["y", "m", "w", "d"]}} x 4,322,054 ops/sec ±1.01% (95 runs sampled)
rules fail: {unit: ["y", "m", "w", "d"]} x 2,504,761 ops/sec ±1.56% (90 runs sampled)

Fastest is rules pass: {age: {type: "number"}}
```
### Rule

## Authors
#### common rule

```bash
$ git summary

project : parameter
repo age : 6 weeks
active : 5 days
commits : 16
files : 13
authors :
16 fengmk2 100.0%
- `required` - if `required` is set to false, this property can be empty. default to `true`.
- `type` - The type of property, every type has it's own rule for the validate.

#### int

If type is `int`, there has tow addition rules:

- `max` - The maximum of the value, `value` must <= `max`.
- `min` - The minimum of the value, `value` must >= `min`.

#### integer

Alias to `int`.

#### number

If type is `number`, there has tow addition rules:

- `max` - The maximum of the value, `value` must <= `max`.
- `min` - The minimum of the value, `value` must >= `min`.

#### date

The `date` type want to match `YYYY-MM-DD` type date string.

#### dateTime

The `dateTime` type want to match `YYYY-MM-DD HH:mm:ss` type date string.

#### id

The `id` type want to match `/^\d+$/` type date string.

#### boolean

Match `boolean` type value.

#### bool

Alias to `boolean`

#### string

If type is `string`, there has four addition rules:

- `allowEmpty`(alias to `empty`) - allow empty string, default to false.
- `format` - A `RegExp` to check string's format.
- `max` - The maximum length of the string.
- `min` - The minimum length of the string.

#### enum

If type is `enum`, it requires an addition rule:

- `values` - An array of data, `value` must be one on them. ___this rule is required.___

#### object

If type is `object`, there has one addition rule:

- `rule` - An object that validate the properties ot the object.

#### array

If type is `array`, there has tow addition rule:

- `itemType` - The type of every item in this array.
- `rule` - An object that validate the items of the array. Only work with `itemType`.

#### abbr

- `'int'` => `{type: 'int', required: true}`
- `'integer'` => `{type: 'integer', required: true}`
- `'number'` => `{type: 'number', required: true}`
- `'date'` => `{type: 'date', required: true}`
- `'dateTime'` => `{type: 'dateTime', required: true}`
- `'id'` => `{type: 'id', required: true}`
- `'boolean'` => `{type: 'boolean', required: true}`
- `'bool'` => `{type: 'bool', required: true}`
- `'string'` => `{type: 'string', required: true, allowEmpty: false}`
- `'object'` => `{type: 'object', required: true}`
- `'array'` => `{type: 'array', required: true}`
- `[1, 2]` => `{type: 'enum', values: [1, 2]}`
```
## License
(The MIT License)
Copyright (c) 2013 fengmk2 &lt;fengmk2@gmail.com&gt;
Copyright (c) 2015 fengmk2 &lt;fengmk2@gmail.com&gt;
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down

0 comments on commit 2daa08e

Please sign in to comment.