huskies-strict

strict method , is huskies middleware.

strict middleware

strict method , is huskies middleware.

example

 
var hus = require("huskies"),
    strict = require("./");
    
    function test(name,age){
        console.log(arguments)
    }
    
    var wrap = hus(test)
                .use(strict)
                .set({type:String,params:{min:5}})
                .set({type:Number})
                .seal();
    
    wrap("brighthas",25);
    
    wrap("leo",false); // throw error. 
 

npm install

npm install huskies-strict

core strict type

Date
Object
String
Boolean
RegExp
Array
Number
Function
 
custom converter and validater
==============================
 
 
var huskies = require("huskies"),
    strict = require("./");
 
// set converter for Type.
strict.converter(Type,function converter(value){
    console.log(value)
    if(value.name){
        return new Type(value.name);
    }else{
        throw new Error("convert Type error!");
    }
})
 
// set validater for Type.
strict.validater(Type,function validater(value,params){
     if(params.len && value.name.length === params.len){
     }else{
        throw new Error;
     }
});
 
function Type(name){
     this.name = name;
}
 
function Article(){
    this._name = null;
    this._type = null;
}
 
Article.prototype = {
 
    setName:
        huskies(function(name){
            this._name = name;
        })
        .use(strict)
        .set({type:String}),
    
    setType:
        huskies(function(type){
            this._type = type;
        })
        .use(strict)
        .set({type:Type,params:{len:5}})
    
}
 
var article = new Article();
 
article.setType({name:"ttttt"});
 
console.log(article._type instanceof Type)  // true
console.log(article._type.name) // ttttt
 
article.setType(new Type("aaaaa"));
console.log(article._type.name) // ttttt

default value

   // if set default value, the arg no validat. 
   set({type:String,params:{default:"brighthas"})

dumb

 
    .use(...)
    .set(...)
    .set(true) // set dumb is true. 
    .seal();
 
    // set dumb=true , then if convert or validate fail , no throw error , return ParamError object . 
    // and default dumb = false , then if convert or validate fail , then throw error. 
 

Number type validater

 
   Number validater
     set({type:Number,params:{min:3,max:12})
 

String type validater

 
   String validater
   
     set({type:String,params:{min:3,max:12}})
     
     set({type:String,params:{regexp:/abc/}})
     
     set({type:String,params:{len:10}})
     
     set({type:String,params:{type:"mail"}})
         // type : mail | url | ip | ipv4 | ipv6 | int | float 
 

LICENSE

MIT