https://github.com/rusty1s/mongoose-integer
mongoose plugin to validate integer values within a Mongoose Schema
https://github.com/rusty1s/mongoose-integer
Last synced: 12 months ago
JSON representation
mongoose plugin to validate integer values within a Mongoose Schema
- Host: GitHub
- URL: https://github.com/rusty1s/mongoose-integer
- Owner: rusty1s
- License: mit
- Created: 2015-11-30T13:31:58.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-05-05T09:52:03.000Z (about 6 years ago)
- Last Synced: 2024-12-02T12:36:43.556Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 4
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-integer
mongoose-integer is a plugin to validate integer values within a Mongoose Schema.
I was really tired of casting or validating my `Number` fields to an integer value. So I created this easy-to-use plugin that takes all the work from you.
## Usage
```
npm install mongoose-integer
```
Then, simply apply the plugin to your schema:
```js
var mongoose = require('mongoose');
var integerValidator = require('mongoose-integer');
var mySchema = new mongoose.Schema({
value: {
type: Number,
integer: true
}
});
mySchema.plugin(integerValidator);
```
## Custom Error Messages
You can pass through a custom error message as part of the optional `options` argument:
```js
mySchema.plugin(integerValidator, { message: 'Error, expected {PATH} to be an integer.' });
```
You can also pass a specific error message as a `string` in your field declaration:
```js
var mySchema = new mongoose.Schema({
value: {
type: Number,
integer: 'Value must be an integer.'
}
});
```
You have access to all of the standard Mongoose error message templating:
* `{PATH}`
* `{VALUE}`
* `{TYPE}`
# Tests
To run the tests you need a local MongoDB instance available. Run with:
```
npm test
```
# Issues
Please use the GitHub issue tracker to raise any problems or feature requests.
If you would like to submit a pull request with any changes you make, please feel free!