mongoose-jojen

1.0.0 • Public • Published

mongoose-jojen

Extends a Mongoose model to do extended validation using Jojen.

Validation runs prior to Mongooses validation. For each field in the model you defile a joRule which will be combined and used for validation.

Installation

npm install mongoose-jojen --save

Setup

const jojenPlugin = require('mongoose-jojen');
const mongoose = require('mongoose');

// Apply to all Schema's
mongoose.plugin(scopesPlugin);

// Apply to one schema
schema.plugin(scopesPlugin);

Usage

const UserSchema = new mongoose.Schema({
    username: {
        type: String,
        joRule: Jo.string().min(3).max(12),
    },
    password: {
        type: String,
        joRule: Jo.string().min(8),
    },
    email: {
        type: String,
        joRule: Jo.string().email(),
    },
    balance: {
        type: Number,
        joRule: Jo.number().min(0),
    },
});

const User = mongoose.model('User', UserSchema);

// Validate an instance
user.validate()
.then(() => {
    // Success
}).catch(err => {
    // Validation failed
    // err instanceof ValidationError
    // err.details == array with details
});

License

MIT

Dependents (0)

Package Sidebar

Install

npm i mongoose-jojen

Weekly Downloads

0

Version

1.0.0

License

MIT

Last publish

Collaborators

  • jamy