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-ajv-plugin

v2.0.0

Published

AJV plugin for Mongoose

Downloads

12

Readme

The Problem

You love Mongoose for all it's convenience methods and valiate-before-saving logic, but but you store complex objects using Schema.Types.Mixed which lacks validation in Mongoose, or you just wish you could validate objects, strings, etc. using a richer JSON-schema vocabulary than is included with Mongoose.

The Solution

The mongoose-ajv-plugin lets you use the awesome AJV JSON-Schema validation library, to validate individual attributes or entire documents, giving you access to it's rich extensible schema vocabulary and convenience formats like email, Date, hostname, ect.

Getting Started

Import mongoose and add in the mongoose-ajv-plugin:

var mongoose = require("mongoose");
mongoose.plugin(require("mongoose-ajv-plugin"))

Now use your favorite AJV Schema, such as the ajv_contact_schema defined below, to validate entire documents using the "ajv-schema" keyword, like so:

var Contact_schema = new mongoose.Schema({
    "name": String ,
    "email": String,
    "birthday": String,
    // let AJV validate this entire document
    "ajv-schema": ajv_contact_schema 
});

Or use AJV to validate one or more attributes of a document using the "ajv-schema" option:

// use AJV to validate fields within a document
var Player_schema = new Schema({
    "user_name": String,
    "rank": Number,
    "ip_address": { 
        "type": String, 
        // let AJV validate this string attribute
        "ajv-schema": { 
            "type": 'string',
            "format": 'ipv4'  /
        } 
    },
    "contact-info": {
        "type": Schema.Types.Mixed ,
        // let AJV validate this nested object
        "ajv-schema": contact_json_schema 
    },
});

Using AJV Extensions

If you wish to extend the Ajv instance used for validation with additional schemata, formats, or keywords, you can pass your own (extended) ajv instance to the plugin, like so:

// create an Ajv instance
var Ajv = require("ajv");
var ajv = new Ajv();

// add custom schema, keywords, or formats
ajv.addSchema(...);
// or 
ajv.addKewword(...)
// or 
ajv.addFormat(...)
// or 
require("my-ajv-plugin")(ajv)

// use this ajv instance to compile every new validator
mongoose.plugin(require("mongoose-ajv-plugin",{"ajv":ajv})

// or use this ajv instance to compile validators for an individual
// mongoose schema
var my_schema = new mongoose.Schema({...});
my_schema.plugin(require("mongoose-ajv-plugin",{"ajv":ajv})

Contact JSON schema

And finally, here's the definition of ajv_contact_schema used in the above examples:

var ajv_contact_schema = {
    "type":"object",
    "properties":{
        "name": {
            "type":"string"
        },
        "email": {
            "type":"string",
            "fomrat":"email"
        },
        "birthday": {
            "oneOf":[
                {"$ref":"#/definitions/date"},
                {"$ref":"#/definitions/date-time"}
            ]
        }
    },
    "required":[
        "name",
        "email"
    ],
    "definitions":{
        "date":{
            "type":"string",
            "format":"date"
        },
        "date-time":{
            "type":"string",
            "format":"date-time"
        }
    }
};