propex-validation is a small framework for validating javascript objects against a propex and a Validator object.
Example validation
var Validator = ;var validate = {}; validatedepartment = { ifvaluelength<3 return "Too Short"; ifvaluelength>30 return "Too long"; } phone: { if!/^\d\d\d-?\d\d\d-?\d\d\d\d$/ return "Bad phone number"; } ; validatelocation = storeid: { if return "Numeric Id needed"; } position: { return datalng+","+datalat } { if/^\d\d,\d\d$/ return "Lng & Lat cannot be over 99"; } //something more complicated... departments: { return "You're going to need to provide these"; } //custom parser parse: validatedepartment { ifvaluelength<2 return "You're going to need to provide more"; } { modeldepartments = value; } ; var posted_data = name:"Walmart of San Francisco" departments: name:"Main" phone:"1231231234" hours:"9-5" name:"Pharmacy" phone:"1235551212" ; //create a propex that defines *what* you want validatedvar px = "{name,storeid?,postition?,departments[0{name,phone,hours},1{name,phone?,hours?}]1:3?}"; //apply the propex and the data to the Validatorvar result = validatelocationpxposted_data; //checkout the resultsconsole;
This will output:
Installation
$ npm install propex-validation
License
The MIT License (MIT) Copyright (c) 2012 William Kapke
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.