mongoose-schema-model
Small model definitions (MongooseDB style) for composable use in other environments.
- Share the same model definitions from MongooseDB on the server with your client
- Use to apply transformations getters/setters between client and REST API (casting form strings as numbers, etc.)
- Shared validations in server/client
Install
Currently only in npm as commonjs.
npm install mongoose-schema-model
Usage
Using a Mongoose Schema definition, apply getter and setter transformations and validation.
// Declare a schema definition var Model = ; { return ~~x; } { return x; } var definitions = year: type: Number min: 1900 max: 2014 set: numberCast items: type: String lowercase: true get: split ; var model = ; // `get` applies the `get` transformationmodel; // "hello, world" // `set` applies all implicit and `set` transformations, as well as subsequently validating// the model, post-transform.var response = model;responsevalue; // `["hello", "world"]`, the implicit `lowercase` transform was applied to each elementresponseerror; // null response = model; responsevalue; // `2000`, transforms applied via `set`responseerror ; // null response = model; responsevalue; // `2000`, transforms applied, via `set`responseerror; // "Path `year` (1550) is less than the minimum allowed value (1900)."
Supported Types
Number
String
[Number]
[String]
Supported Properties
Transforms
Validations
License
MIT License