mongo-validator

1.1.0 • Public • Published

mongo-validator

MongoDB JSON to Validator

Usage example

const mongoValidator = require('mongo-validator');
 
const payload = {
    __size: 10,
    foo: '!String',
    bar: 'Number',
    customObject: {
        __exists: true,
        propTest: 'Bool'
    }
}; 
const validatorPayload = mongoValidator(payload);
console.log(validatorPayload);

It will log (stdout) the following result :

{
  "validator": {
    "$and": [
      {
        "foo": {
          "$type": "string",
          "$exists": true
        }
      },
      {
        "bar": {
          "$type": "number"
        }
      },
      {
        "customObject": {
          "$type": "object",
          "$exists": true
        }
      },
      {
        "customObject.propTest": {
          "$type": "bool"
        }
      }
    ]
  },
  "size": 10
}

Use this object as a validator for MongoDB (when you create a new collection for example).

/mongo-validator/

    Package Sidebar

    Install

    npm i mongo-validator

    Weekly Downloads

    2

    Version

    1.1.0

    License

    MIT

    Last publish

    Collaborators

    • fraxken