fraxken / mongo-validator

MongoDB JSON to Validator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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).

mongoValidator function can take an options Object that allow you to redefine the attributes caracter.

const validatorPayload = mongoValidator(payload, {
  attributes_char: '#'
});

About

MongoDB JSON to Validator

License:MIT License


Languages

Language:JavaScript 100.0%