🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@fyresite/document_validator

Package Overview
Dependencies
Maintainers
2
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fyresite/document_validator

Validate and translate JSON objects for Document Databases like MongoDB or Amazon's DynamoDB.

0.4.12
latest
Source
npm
Version published
Weekly downloads
14
366.67%
Maintainers
2
Weekly downloads
 
Created
Source

Document Validator

Validate and translate JSON objects for Document Databases like MongoDB or Amazon's DynamoDB.

Installation

npm install --save @fyresite/document_validator

Usage

Writing Schema

Validations are handled using @fyresite/object-validator and utilizes the validator node module.

module.exports = function (Types, Op) {
  return {
    "name": {
      "first": {
        "type": Types.STRING,
        "required": true
      },
      "middle": Types.STRING,
      "last": {
        "type": Types.STRING,
        "required": true
      }
    },
    validateField: {
      type: Types.STRING,
      validate: {
        "method": "matches",
        "pattern": "^([a-z ])+$"
      }
    },
    "myInt":{
      type: Types.INTEGER,
      "required": {
        myFloat:Op.eq(4.5)
      }
    },
    "myFloat": Types.FLOAT
    "email": {
      "type": Types.STRING,
      "required": true
    },
    "phone": {
      "type": Types.STRING,
      "required": true
    },
    "dob": {
      "type": Types.DATE,
      "required": true
    }
  }
}

Synchronous Validation

var { Validator } = require('@fyresite/document_validator');

var userValidator = new Validator('user');

var userValid = {
  "name": {
    "first": "Mike",
    "middle": "T",
    "last": "Jones"
  },
  "email": "email@example.com",
  "phone": "2813308004",
  "dob": new Date("1996-11-09")
};

var userInvalid = {
  "name": {
    "first": "Mike",
    "middle": "T",
    "last": "Jones"
  },
  "email": "email@example.com",
  "phone": 344,
  "dob": new Date("1996-11-09"),
  "address": {
    "street": "2212 S Banner St",
    "street2":"Strong",
    "city": "Gilbert",
    "state": "AZ",
    "zip": 85296
  }
};

// Returns emptyObject {} signifying the document was valid
var invalidFields = userValidator.validate(userValid, 'v1');

// Returns object containing all of the validation errors that occurred
var invalidFields = userValidator.validate(userInvalid, 'v1');

Asynchronous Validation

var userValidator = new Validator('user', { schemaPath: 'schemas', isS3: true, bucket: 'model-schemas', profile: 'test-profile' });

var userValid = {
  "name": {
    "first": "Mike",
    "middle": "T",
    "last": "Jones"
  },
  "email": "email@example.com",
  "phone": "2813308004",
  "dob": new Date("1996-11-09"),
  "_v" : 1
};

var userInvalid = {
  "name": {
    "first": "Mike",
    "middle": "T",
    "last": "Jones"
  },
  "email": "email@example.com",
  "phone": 344,
  "dob": new Date("1996-11-09"),
  "address": {
    "street": "2212 S Banner St",
    "street2":"Strong",
    "city": "Gilbert",
    "state": "AZ",
    "zip": 85296
  },
  "_v" : 1
};

userValidator.init()
  .then(() => {
    var invalidFields;

    // Returns emptyObject {} signifying the document was valid
    invalidFields = userValidator.validate(userValid, 'v1');

    // Returns object containing all of the validation errors that occurred
    invalidFields = userValidator.validate(userInvalid, 'v1');
  })
  .catch(err => {
    console.error(err);
  });

API

Validator

Class used validate and translate documents

Kind: global class

new Validator(schemaName, [config])

Create a validator.

ParamTypeDefaultDescription
schemaNamestringName of the schema to validate.
[config]ObjectCustom configuration options.
config.schemaPathstring"APP_ROOT/schemas"the path to the schemas folder
config.isS3booleantrueDefine if schema location is a s3 bucket
config.s3BucketstringName of the s3 bucket to connect the schemas are located
config.awsProfilestringThe AWS Profile you want to use to connect to the bucket

validator.init() ⇒ Promise

Used to fetch schema from s3, the instance validate function must be used inside .then() to guarantee that schemas are loaded before validating

Kind: instance method of Validator

validator.validate(document, version) ⇒ Object

Validates document against specific version of the schema

Kind: instance method of Validator

ParamTypeDescription
documentObjectDocument to be validated
versionstringversion to be validated.

validator.validateKeys(fields, version) ⇒ Object

Validates keys for document against specific version of the schema

Kind: instance method of Validator

ParamTypeDescription
fieldsObjectDocument to be validated
versionstringversion to be validated.

validator.translate(document, version) ⇒ Object

Translates document to version specified the documents version is automatically read through the _v property on the document

Kind: instance method of Validator

ParamTypeDescription
documentObjectDocument to be validated
versionstringversion to be translated to.

Schema

Defines the validation rules that will be applied to the document using the Validator

// Export a function (Types, Op) that returns a json
module.exports = function(Types, Op) {
  return {
    /*
    Fields can be defined by setting the ket to the field
    name and the type as the value
    */
    fieldName: Types.STRING

    // Or pass an object for more advanced options

    advancedField: {
      type: Types.INTEGER, //Type of field *required*

      /*
      flag that tells validator to return error if field is not provided.
      The value can be
      */
      required: true,

      //Default value to use if field is not provided
      default: 0,

      /*
        Validation object that is used for advanced validation. Methods are based on the validator npm package by cohara87.

        For further documentation refer to @fyresite/object_validator
      */
      validate: {
        "method": "validator method", /** String **/
        "param1": value,
        "param2": value
      }
    }
  }
}

Using Conditional require statements in schema

An function can be passed

module.exports = function(Types, Op) {
  return {
    field1 : Types.NUMBER
  }
}

Types

Op

Class used to provide conditions to require flags

Kind: global class

Op.any(conditions) ⇒ function

Checks if the documents value matches any of the conditions

Kind: static method of Op

ParamTypeDescription
conditionsarrayArray of conditions to match on the Op functions are valid conditions

Op.not(opFunction) ⇒ function

Accepts Op Function and returns opposite value

Kind: static method of Op

ParamTypeDescription
opFunctionfunctionvalid Op function

Op.set() ⇒ function

Checks if field is set

Kind: static method of Op

Op.eq(value) ⇒ function

Checks if field equals value

Kind: static method of Op

ParamType
valuenumber | string | boolean

Op.gt(value) ⇒ function

Checks if fields value is greater than the provided value

Kind: static method of Op

ParamType
valuenumber

Op.gte(value) ⇒ function

Checks if fields value is greater than or equal to provided value

Kind: static method of Op

ParamType
valuenumber

Op.lt(value) ⇒ function

Checks if fields value is less than the provided value

Kind: static method of Op

ParamType
valuenumber

Op.lte(value) ⇒ function

Checks if fields value is less than or equal to provided value

Kind: static method of Op

ParamType
valuenumber

Keywords

document

FAQs

Package last updated on 22 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts