flyber-types
v0.1.38
Published
Type System for Javascript Objects
Downloads
7
Readme
#FT Runtime Type Check System For Javascript
Install
npm install flyber-types
Type Definition Example ('./examples/System.ft')
#Simple Types
String : /.?/
Integer : Global.Integer
Int : Integer
Boolean : Global.Boolean
Bool : Boolean
Double : Global.Double
Numeric : Double | Integer
Null : Global.Null
Undefined : Global.Undefined
Email : /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i
Strings : [String]
#Type Extensions
String...
Min min : /^.{#{min},}$/
Max max : /^.{,#{max}}$/
Range min max : /^.{#{min},#{max}}$/
Integer...
Min min : @ >= min
Max max : @ <= max
#Enums
Status : "active" | "inactive"
Missing : Null | Undefined
#Complex Types
User
------------
email : Email
picture : String
firstname : String Range(5,20)
lastname : String Min(5) Max(20)
status : Status
bio : String | Missing
tags : [String]
Use
var types = require("flyber-types");
var fs = require("fs");
var validate = types({
System: fs.readFileSync("./examples/System.ft").toString("utf8")
});
var user = {
email: '[email protected]',
picture: 'http://some-website.com/picture.png',
firstname: 'Andrey',
lastname: 'Test',
status: 'active',
bio: 'Ho',
tags: ["user"]
};
validate("System.User", user); //true