Officer
Officer is a powerful schema-based JavaScript object validator and transformer. Keep your objects in line with Officer.
- Operates on arbitrary, multi-level JavaScript objects
- Supports collections and sub-objects
- Specify pre- and post-validation functions
- Supports a large variety of validation and transformation options
- Provides detailed reports about processing and failures
Use
tl;dr
Officer = ;var officer = schema;officer;
- Specify an object schema
- Create a new Officer object, passing in the schema and object to validate
- Validate your object with your shiny new Officer
- Optional: Enjoy access to a large report about what happened to your object as well as detailed error messages
Longer version:
First you need a schema to validate against. For this example, we'll be checking that some incoming cat JSON objects are the correct format, modifying and adding any information that can and should be, and then storing them.
Sample POST data
First, the schema as a single file (not as awesome).
The Full Schema) ./schema/full-cat.js
var { return type: Number min: min max: max // You can provide a function to perform on the value before it's validated with built-in validation an .after() function can also be provided { return Math; } ;}; moduleexports = name: type: String "default": "Kitty" regExp: pattern: /^[a-z\.\s]{1,32}$/i error: "What kind of name is that?" fur: type: color: type: h: s: l: a: type: Number fallback: 1 min: 0 max: 1 coverage: type: Number min: 0 max: 1 collection: true ;
And then the schema broken up into sub-files to define sub-objects (more awesome).
Schema Level 1) ./schema/cat.js
moduleexports = // each top-level key matches the object property to look for and validate name: // the type can be a JavaScript constructor function type: String // If the data is missing, we can specify a default value or function "default": "Kitty" // regular expressions can be specified with an option error message regExp: pattern: /^[a-z\.\s]{1,32}$/i error: "What kind of name is that?" fur: // the type can be an embedded object literal describing the structure (a schema) type: // this tells Officer to also accept an array of matching values collection: true ;
Schema Level 2) ./schema/fur.js
moduleexports = color: // nesting can go as deep as you'd like type: coverage: type: Number // there are a few special built-in validation functions: Numbers->min, max being among them min: 0 max: 1 ;
Schema Level 3) ./schema/color-hsla
var { return type: Number min: min max: max // You can provide a function to perform on the value before it's validated with built-in validation. An .after() function can also be provided { return Math; } ;}; moduleexports = h: s: l: a: type: Number fallback: 1 min: 0 max: 1 ;
Once you have your schema, you can load it up and verify your object.
App) index.js
var Officer = ;var catSchema = ;var catModel = /* require our cat model */var cats = /* pull in our POST data */ cats;
Why did I make this?
I was unhappy with the validation tools available online and wanted something that would work on complex objects with powerful schemas. I developed this tool as a way to work with JSON data going into my MongoDB collections without having to use a tool like Mongoose. It's in no way tied to MongoDB though.
Version
0.2.0
Requirements
Officer requires node.js and npm. It is dependent on the underscore package, but that will be removed in a later release.
Installation
The easiest way to get it into your node project is through npm
npm install officer --save
Contribution and Feedback
If you find an error in the tool, please fork the code, fix it and request a pull.
Feedback and feature requests are welcome.
License
MIT
Free Software, Hell Yeah!