# mongo-json-schema

> A schema validator for objects going into mongo

Latest version **0.2.8** (published 2014-08-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongo-json-schema
pnpm add mongo-json-schema
yarn add mongo-json-schema
bun add mongo-json-schema
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.8 |
| Published | 2014-08-05 |
| First published | 2014-02-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 (+3 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Gregg Caines |
| Maintainers | cainus, peterkhayes |
| Keywords | mongo, schema, jsonschema |

## Links

- npm: https://www.npmjs.com/package/mongo-json-schema
- Repository: https://github.com/cainus/mongoJsonSchema
- Issues: https://github.com/cainus/mongoJsonSchema/issues
- npm.io page: https://npm.io/package/mongo-json-schema

## Dependencies (5)

- [JSV](https://npm.io/package/JSV.md) 4.0.2
- [difflet](https://npm.io/package/difflet.md) 0.2.6
- [mongodb](https://npm.io/package/mongodb.md) 1.3.23
- [traverse](https://npm.io/package/traverse.md) 0.6.6
- [underscore](https://npm.io/package/underscore.md) 1.5.2

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.2.8 (latest) — 2014-08-05
- 0.2.7 — 2014-07-23
- 0.2.6 — 2014-05-21
- 0.2.5 — 2014-04-18
- 0.2.4 — 2014-04-18
- 0.2.2 — 2014-02-26
- 0.2.1 — 2014-02-20
- 0.2.0 — 2014-02-13
- 0.1.0 — 2014-02-12
- 0.0.8 — 2014-02-12
- 0.0.7 — 2014-02-12
- 0.0.4 — 2014-02-07
- 0.0.3 — 2014-02-07
- 0.0.2 — 2014-02-06
- 0.0.1 — 2014-02-04

## README

#mongo-json-schema

This is a library for validating objects against a jsonSchema before inserting into (and after retrieving from) mongo.
### Creating a schema object
```javascript  
var Schema = require('mongo-json-schema');
var schema = Schema({
  nested: {
    type: 'object',
    properties: {
      sub: {
        type: "objectid"
      }
    }
  },
  count: {
    type: "number",
    required: false
  },
  participants: {
    type: "array",
    items: {
      type: "objectid",
    }
  }
});
  
```

### Validating an input object
```javascript
schema.validate({
  _id '52f044dee2896a8264d7ec2f',
  nested: {
      sub: '52f044dee2896a8264d7ec2f'
  }
})
// throws errors if its not valid!
```

### Getting a standard jsonSchema
```javascript
schema.toJsonSchema();
```

returns:
```javascript
{
  {
    type: 'object',
    properties: {
      _id: {
        type: "string",
        pattern: "^[a-fA-F0-9]{24}$",
        required: true
      },
      nested: {
        type: 'object',
        properties: {
          sub: {
            type: "string",
            pattern: "^[a-fA-F0-9]{24}$",
          }
        }
      },
      count: {
        type: "number",
        required: false
      },
      participants: {
        type: "array",
        items: {
          type: "string",
          pattern: "^[a-fA-F0-9]{24}$"
        }
      }
    }
  }
}
```
###getting an object with strings for ids
```javascript
  schema.idsToStrings({
    _id: ObjectID('52f044dee2896a8264d7ec2f'),
    nested: {
      sub: ObjectID('52f044dee2896a8264d7ec2f'),
    },
    count: 42,
    participants: [ObjectID('52f044dee2896a8264d7ec2f'),ObjectID('52f044dee2896a8264d7ec2f')]
  });
```
returns:
```javascript
{
  _id: '52f044dee2896a8264d7ec2f',
  nested: {
    sub: '52f044dee2896a8264d7ec2f',
  },
  count: 42,
  participants: ['52f044dee2896a8264d7ec2f','52f044dee2896a8264d7ec2f']
}
```

###getting an object with ObjectIDs for ids
```javascript
schema.stringsToIds({
  _id: '52f044dee2896a8264d7ec2f',
  nested: {
    sub: '52f044dee2896a8264d7ec2f',
  },
  count: 42,
  participants: ['52f044dee2896a8264d7ec2f','52f044dee2896a8264d7ec2f']
});
```
returns:
```javascript
{
  _id: ObjectID('52f044dee2896a8264d7ec2f'),
  nested: {
    sub: ObjectID('52f044dee2896a8264d7ec2f'),
  },
  count: 42,
  participants: [ObjectID('52f044dee2896a8264d7ec2f'),ObjectID('52f044dee2896a8264d7ec2f')]
}
```

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