ag-types - arbitrary data structure validation for Javascript
You've got incoming JSON data that you'd like to access using Javascript, but you can't be sure its structure matches what you expect. ag-types
allows you to validate the structure of the input graph before you crash and burn on a TypeError: 'undefined' is not an object
.
Installation
npm install ag-types
Usage
Examples in Coffeescript.
Describe your type
require 'ag-types'RequestType = Object url: String method: String params: Optional Map Any
Construct a type validator by simply mirroring the structure of what you want.
Validate input data against the type
input = url: 'http://example.com'RequestTypeinput
Your validator is a function that returns a data.validation.
Continue off the validation result
RequestTypeinputfold consolelog "This doesn't look like a valid request: "errors doRequest request
If an error occurs, the errors object will hold details of what went wrong. Otherwise you're good to go with the data.
Development
npm install
grunt test watch:test --force