Created
September 12, 2013 02:47
-
-
Save cbaclig/6532566 to your computer and use it in GitHub Desktop.
Node validation framework idea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input: | |
* name | |
* url | |
* description | |
* categories | |
* tags | |
* pubMonthlyUniqueVisitors | |
* pubMonthlyPageViews | |
output: | |
# property entity | |
class PropertyValidator extend BaseValidator | |
PropertyValidator: (@errorFormat = 'nested') -> | |
schema: | |
first_name: new NameValidator(required: false) | |
first_name: NumberValidator.validate(0, 50) | |
url: new UrlValidator() | |
middleware: (req, res, next) -> | |
req.values = if @validate then @validate(req.body) else req.body | |
next() | |
validate: (params) -> | |
for property, validator of @schema | |
req.body[property] = validator.validate(req.body[property]) | |
if error throw | |
new Property(data) | |
{ | |
first_name: { | |
message: 'human readable' | |
value: 4 | |
}, | |
buyer: { | |
name: | |
}, | |
'buyer.name' : { | |
} | |
'buyer[name]' : { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment