# mongodb-schema-document-validator

> Makes `Document failed validation` less dreadful.

Latest version **1.0.4** (published 2020-11-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongodb-schema-document-validator
pnpm add mongodb-schema-document-validator
yarn add mongodb-schema-document-validator
bun add mongodb-schema-document-validator
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2020-11-02 |
| First published | 2020-09-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 3.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Krystian Jarmicki |
| Maintainers | kjarmicki |
| Keywords | MongoDB, Document, Schema, Validation, Error |

## Links

- npm: https://www.npmjs.com/package/mongodb-schema-document-validator
- Repository: https://github.com/kjarmicki/mongodb-schema-document-validator
- Homepage: https://github.com/kjarmicki/mongodb-schema-document-validator#readme
- Issues: https://github.com/kjarmicki/mongodb-schema-document-validator/issues
- npm.io page: https://npm.io/package/mongodb-schema-document-validator

## Dependencies (2)

- [ajv](https://npm.io/package/ajv.md) 6.12.4
- [ajv-bsontype](https://npm.io/package/ajv-bsontype.md) 1.0.7

## Recent versions

- 1.0.4 (latest) — 2020-11-02
- 1.0.3 — 2020-09-15
- 1.0.2 — 2020-09-14
- 1.0.1 — 2020-09-14
- 1.0.0 — 2020-09-14

## README

# MongoDB Schema Document Validator

Have you ever been losing your mind over that annoyingly vague `Document failed validation` error
while trying to figure out which of the document properties are acutally causing it?

If so, despair no more because this module is designed to help exactly with that.

## Usage

You first need to create and initialize the validator object with db object *after* the validation rules
have been set up. Then you can use the validator giving it a collection name and a document you'd like to validate.

Example:
```js
const {MongoClient} = require('mongodb');
const {MongodbSchemaDocumentValidator, isDocumentFailedValidationError} = require('mongodb-schema-document-validator');

const mongoClient = new MongoClient('mongodb://company');
await mongoClient.connect();

const db = mongoClient.db('staff');
await db.createCollection('developers', {
  validationLevel: 'strict',
  validationAction: 'error',
  validator: {
    $jsonSchema: {
      bsonType: 'object',
      required: ['name'],
      properties: {
        name: {
          bsonType: 'string',
          description: 'must be a string and is required'
        }
      }
    }
  }
});

const validator = new MongodbSchemaDocumentValidator(db);
await validator.initialize(); // remember that initalize has to be called after the schema is attached to the collection

const collection = db.collection('developers');
const newDeveloper = {
  naem: 'this property has a typo'
};
try {
  await collection.insert(newDeveloper);
} catch (error) {
  if (isDocumentFailedValidationError(error)) {
    const validation = validator.validate('developers', newDeveloper);
    console.log(validation);
  }
}
```
This will output
```js
{
  isValid: false,
  errors: [{
    keyword: 'required',
    dataPath: '',
    schemaPath: '#/required',
    params: {
      missingProperty: 'name'
    },
    message: "should have required property 'name'"
  }],
  errorsText: "data should have required property 'name'"
}
```
which should point you in the right direction towards solving your problem.

## Custom validation options

This module uses [Ajv](https://www.npmjs.com/package/ajv) under the hood and you can configure it using
[its options](https://www.npmjs.com/package/ajv#options). In order to use your custom configured Ajv instead
of the default one, pass it as a second argument to the constructor:

```js
const validator = new MongodbSchemaDocumentValidator(db, new Ajv({
  allErrors: true
}));
```

At the moment of writing this module is known to work well with Ajv version `6.12.4`.

---
_Source: https://npm.io/package/mongodb-schema-document-validator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
