npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fyresite/document_validator

v0.4.12

Published

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

Downloads

26

Readme

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 protected]",
  "phone": "2813308004",
  "dob": new Date("1996-11-09")
};

var userInvalid = {
  "name": {
    "first": "Mike",
    "middle": "T",
    "last": "Jones"
  },
  "email": "[email protected]",
  "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 protected]",
  "phone": "2813308004",
  "dob": new Date("1996-11-09"),
  "_v" : 1
};

var userInvalid = {
  "name": {
    "first": "Mike",
    "middle": "T",
    "last": "Jones"
  },
  "email": "[email protected]",
  "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.

| Param | Type | Default | Description | | --- | --- | --- | --- | | schemaName | string | | Name of the schema to validate. | | [config] | Object | | Custom configuration options. | | config.schemaPath | string | "APP_ROOT/schemas" | the path to the schemas folder | | config.isS3 | boolean | true | Define if schema location is a s3 bucket | | config.s3Bucket | string | | Name of the s3 bucket to connect the schemas are located | | config.awsProfile | string | | The 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

| Param | Type | Description | | --- | --- | --- | | document | Object | Document to be validated | | version | string | version to be validated. |

validator.validateKeys(fields, version) ⇒ Object

Validates keys for document against specific version of the schema

Kind: instance method of Validator

| Param | Type | Description | | --- | --- | --- | | fields | Object | Document to be validated | | version | string | version 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

| Param | Type | Description | | --- | --- | --- | | document | Object | Document to be validated | | version | string | version 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

| Param | Type | Description | | --- | --- | --- | | conditions | array | Array 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

| Param | Type | Description | | --- | --- | --- | | opFunction | function | valid 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

| Param | Type | | --- | --- | | value | number | string | boolean |

Op.gt(value) ⇒ function

Checks if fields value is greater than the provided value

Kind: static method of Op

| Param | Type | | --- | --- | | value | number |

Op.gte(value) ⇒ function

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

Kind: static method of Op

| Param | Type | | --- | --- | | value | number |

Op.lt(value) ⇒ function

Checks if fields value is less than the provided value

Kind: static method of Op

| Param | Type | | --- | --- | | value | number |

Op.lte(value) ⇒ function

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

Kind: static method of Op

| Param | Type | | --- | --- | | value | number |