npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-jsonschema-validation

v0.0.4

Published

Validate your mongoose models through a JSON schema

Downloads

8

Readme

Mongoose JSON Schema Validation

Build Status Coverage Status

Test your mongoose models through JSON schema validator.

Installation

npm install --save mongoose-jsonschema-validation

Is intended to be used as mongoose plugin.

General

This module will let you validate your model through json schema.

To enable validation for your models first you have to create a json schema like this one:

// personschema.json
{
    "$schema": "http://json-schema.org/schema#",
    "title": "Person schema",
    "description": "",
    "type": "object",
    "properties": {
        "_id": {
            "type": "object"
        },
        "name": {
            "type": "string",
            "minLength": 1
        }
    },
    "additionalProperties": false,
    "required": ["name"]
}

Then create a schema like always:

var PersonSchema = new Schema({
    name: String
});

Apply the plugin to your schema:

var JsonSchemaValidation = require('mongoose-jsonschema-validation');

PersonSchema.plugin(JsonSchemaValidation, {
    jsonschema:  __dirname + '/personschema.json'
});

Now everytime you save your model it is checked aginst the schema. If it is invalid, the save callback will be called with the validation error.

To apply the json schema tv4, a quick and performant json-schema-v4 library, is used. Please refer to its documentation to write a valid json schema.

Options

The JsonSchemaValidation plugin accept two options

  • jsonschema: which is mandatory is the absolute path to the json schema or the json schema itself.
  • errorClass: is a constructor used to build a new validation error. t's optional and the default is Error.

Faq

Can I use regular validation?

Yes, you can mix both validations, the regular one from mongoose and the json schema. Because this plugin use the pre-save hook, if the regular validation issue an error the json schema it's not checked at all. The custom error class is also used only for the json schema validation errors, not for the regular ones (which will be a mongoose.Error.ValidationError).

Which capability of json schema are supported?

The same which are supported from tv4.

Tests

To test the library simply

npm test