mongo-schemer

1.1.2 • Public • Published

mongo-schemer

Provides clear Mongo schema validation error messages by running all validation errors through ajv. Does this by wrapping your Mongo client db in order to catch validation errors and compare against your schema.

Requirements

Installation

npm install mongo-schemer

Usage

Wrap your db with MongoSchemer.explainSchemaErrors():

const { MongoClient } = require('mongodb');
const MongoSchemer = require('mongo-schemer');

MongoClient.connect(dbUrl).then((client) => {
  const db = client.db(dbName);
  if (process.env.DEV) {
    db = MongoSchemer.explainSchemaErrors(db, {
      onError: (errors) => { console.log(errors); },
    });
  }
});

Running in prod is not recommended due to the overhead of validating documents against the schema.

Caveats

MongoDB does not currently support definitions in the JSON Schema. However, there is an easy workaround using json-schema-ref-parser. See https://brianschiller.com/blog/2018/01/30/pokemon-mongo-schemer for more information.

/mongo-schemer/

    Package Sidebar

    Install

    npm i mongo-schemer

    Weekly Downloads

    44

    Version

    1.1.2

    License

    MIT

    Unpacked Size

    23.4 kB

    Total Files

    7

    Last publish

    Collaborators

    • wintersieck
    • bgschiller