@fibjs/enforce
Advanced tools
Comparing version 0.0.8 to 0.1.0
@@ -10,17 +10,18 @@ declare namespace FibjsEnforce { | ||
export interface IEnforce { | ||
add(property: string, validator: ValidationCallback): IEnforce; | ||
add(property: string, validator: IValidator): IEnforce; | ||
interface IEnforce { | ||
add(property: string, validator: ValidationCallback): this; | ||
add(property: string, validator: IValidator): this; | ||
context(): any; | ||
context(name: string): any; | ||
context(name: string, value: any): IEnforce; | ||
context(name: string, value: any): this; | ||
clear(): void; | ||
check(data: any, cb: (error: Error | Error[]) => void): any; | ||
check(data: any, cb: (error: Error | Error[]) => void): void; | ||
checkSync(data: any): Error[] | ||
} | ||
export interface Options { | ||
interface Options { | ||
returnAllErrors: boolean; | ||
} | ||
export interface ContextMap { | ||
interface ContextMap { | ||
property?: string; | ||
@@ -30,3 +31,6 @@ [name: string]: any; | ||
export interface IValidator { | ||
interface IValidator { | ||
/** | ||
* @bad_design | ||
*/ | ||
validate: ValidationCallback | ||
@@ -40,11 +44,11 @@ | ||
export interface ValidationCallback { | ||
(value: any, next: (errorMessage?: string) => boolean, thisArg?: any, contexts?: ContextMap): void; | ||
interface ValidationCallback { | ||
(value: any, next: (errorMessage?: string) => any, thisArg?: any, contexts?: ContextMap): void; | ||
} | ||
export interface ValidatorMap { | ||
interface ValidatorListDict { | ||
[property: string]: IValidator[]; | ||
} | ||
export interface ValidationError extends Error { | ||
interface ValidationError extends Error { | ||
property?: string; | ||
@@ -51,0 +55,0 @@ value?: any; |
v0.0.8 / 2019-03-06 | ||
v0.1.0 / 2019-07-06 | ||
================== | ||
* add IEnforce.checkSync and finish it. | ||
* robust change. | ||
* update repo info. | ||
* update ci settings. | ||
v0.0.8 / 2019-03-06 | ||
=================== | ||
* Release v0.0.8 | ||
* upgrade dependencies, typo fix. | ||
@@ -6,0 +15,0 @@ |
@@ -5,2 +5,3 @@ /// <reference path="../@types/index.d.ts" /> | ||
constructor(options) { | ||
this.options = options; | ||
this.validations = {}; | ||
@@ -17,5 +18,5 @@ this.contexts = {}; | ||
if (validator.validate === undefined) { | ||
throw new Error('Missing validator (function) in FibjsEnforce.add(property, validator)'); | ||
throw new Error('[FibjsEnforce.add(property, validator)]Missing validate function validator'); | ||
} | ||
if (!this.validations.hasOwnProperty(property)) | ||
if (!this.validations.hasOwnProperty(property) || !Array.isArray(this.validations[property])) | ||
this.validations[property] = []; | ||
@@ -38,39 +39,43 @@ this.validations[property].push(validator); | ||
} | ||
check(data, cb) { | ||
var validations = []; | ||
var errors = []; | ||
var next = () => { | ||
if (validations.length === 0) { | ||
if (errors.length > 0) | ||
return cb(errors); | ||
else | ||
return cb(null); | ||
checkSync(data) { | ||
const errors = []; | ||
if (!Object.keys(this.validations)) | ||
return errors; | ||
const contexts = Object.assign({}, this.contexts); | ||
const { returnAllErrors } = this.options; | ||
let _err = null; | ||
for (let property in this.validations) { | ||
const validators = this.validations[property]; | ||
for (let i = 0; i < validators.length; i++) { | ||
const validator = validators[i]; | ||
validator.validate(data[property], (message) => { | ||
if (!message) | ||
return; | ||
_err = new Error(message); | ||
_err.property = property; | ||
_err.value = data[property]; | ||
_err.msg = message; | ||
_err.type = "validation"; | ||
}, data, contexts); | ||
if (!_err) | ||
continue; | ||
errors.push(_err); | ||
_err = null; | ||
if (!returnAllErrors) | ||
break; | ||
} | ||
var validation = validations.shift(); | ||
this.contexts.property = validation.property; | ||
validation.validator.validate(data[validation.property], function (message) { | ||
if (message) { | ||
var err = new Error(message); | ||
err.property = validation.property; | ||
err.value = data[validation.property]; | ||
err.msg = message; | ||
err.type = "validation"; | ||
if (!this.options.returnAllErrors) | ||
return cb(err); | ||
errors.push(err); | ||
} | ||
return next(); | ||
}.bind(this), data, this.contexts); | ||
}; | ||
for (var k in this.validations) { | ||
for (var i = 0; i < this.validations[k].length; i++) { | ||
validations.push({ | ||
property: k, | ||
validator: this.validations[k][i] | ||
}); | ||
} | ||
if (errors.length && !returnAllErrors) | ||
break; | ||
} | ||
return next(); | ||
return errors; | ||
} | ||
check(data, cb) { | ||
const errs = this.checkSync(data); | ||
if (!errs.length) | ||
cb(null); | ||
else { | ||
cb(this.options.returnAllErrors ? errs : errs[0]); | ||
} | ||
} | ||
} | ||
module.exports = Enforce; |
{ | ||
"name": "@fibjs/enforce", | ||
"version": "0.0.8", | ||
"version": "0.1.0", | ||
"description": "", | ||
"main": "lib/index.js", | ||
"types": "@types/index.d.ts", | ||
"repository": "git://github.com/richardo2016/fibjs-enforce.git", | ||
"repository": "git://github.com/fibjs-modules/enforce.git", | ||
"author": "richardo2016@gmail.com", | ||
"homepage": "https://github.com/richardo2016/fibjs-enforce", | ||
"homepage": "https://github.com/fibjs-modules/enforce", | ||
"license": "MIT", | ||
@@ -26,3 +26,3 @@ "keywords": [ | ||
"ci": { | ||
"type": "travis", | ||
"type": "travis, appveyor", | ||
"version": [ | ||
@@ -35,5 +35,6 @@ "0.21.0", | ||
"0.26.0", | ||
"0.26.1" | ||
"0.26.1", | ||
"0.27.0" | ||
] | ||
} | ||
} |
@@ -7,3 +7,4 @@ # @fibjs/enforce | ||
[![Build Status](https://secure.travis-ci.org/richardo2016/fibjs-enforce.png?branch=master)](http://travis-ci.org/richardo2016/fibjs-enforce) | ||
[![Build Status](https://secure.travis-ci.org/fibjs-modules/enforce.png?branch=master)](https://travis-ci.org/fibjs-modules/enforce) | ||
[![Build status](https://ci.appveyor.com/api/projects/status/38egey3758071m4k?svg=true)](https://ci.appveyor.com/project/richardo2016/enforce) | ||
[![NPM version](https://img.shields.io/npm/v/@fibjs/enforce.svg)](https://www.npmjs.org/package/@fibjs/enforce) | ||
@@ -10,0 +11,0 @@ |
@@ -176,2 +176,67 @@ | ||
}); | ||
describe(".checkSync (default options)", function () { | ||
it("should return no error if it's ok", function () { | ||
var checks = new enforce.Enforce(); | ||
checks.add("prop", enforce.lists.inside([ 1, 2, 3 ])); | ||
checks.add("prop", enforce.lists.inside([ 3, 4, 5 ])); | ||
const [ err ] = checks.checkSync({ | ||
prop : 3 | ||
}); | ||
assert.notExist(err); | ||
}); | ||
it("should return after first error", function () { | ||
var checks = new enforce.Enforce(); | ||
checks.add("prop", enforce.lists.inside([ 1, 2, 3 ], "first-error")); | ||
checks.add("prop", enforce.lists.inside([ 3, 4, 5 ], "last-error")); | ||
const [ err ] = checks.checkSync({ | ||
prop : 6 | ||
}); | ||
assert.exist(err); | ||
assert.equal(err.msg, "first-error"); | ||
}); | ||
}); | ||
describe(".checkSync (returnAllErrors = true)", function () { | ||
it("should return no error if it's ok", function () { | ||
var checks = new enforce.Enforce({ | ||
returnAllErrors : true | ||
}); | ||
checks.add("prop", enforce.lists.inside([ 1, 2, 3 ])); | ||
checks.add("prop", enforce.lists.inside([ 3, 4, 5 ])); | ||
const [ err ] = checks.checkSync({ | ||
prop : 3 | ||
}); | ||
assert.notExist(err); | ||
}); | ||
it("should return after all validations", function () { | ||
var checks = new enforce.Enforce({ | ||
returnAllErrors : true | ||
}); | ||
checks.add("prop", enforce.lists.inside([ 1, 2, 3 ], "first-error")); | ||
checks.add("prop", enforce.lists.inside([ 3, 4, 5 ], "last-error")); | ||
const errs = checks.checkSync({ | ||
prop : 6 | ||
}); | ||
assert.exist(errs); | ||
assert.isArray(errs); | ||
assert.equal(errs[0].msg, "first-error"); | ||
assert.equal(errs[1].msg, "last-error"); | ||
}); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
// var mocha = require('./_mocha') | ||
var path = require('path') | ||
var test = require('test') | ||
@@ -10,11 +10,9 @@ | ||
fs.readdirSync(location).filter(function (file) { | ||
return file.substr(-3) === '.js'; | ||
}).forEach(function (file) { | ||
var fpath = path.join(location, file) | ||
fs.readdirSync(location) | ||
.filter(file => path.extname(file) === '.js') | ||
.forEach(function (file) { | ||
var fpath = path.join(location, file) | ||
run(fpath) | ||
}); | ||
console.log('fpath', fpath) | ||
run(fpath) | ||
}); | ||
test.run(console.DEBUG); |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
111572
25
1656
1
228