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

json-data-validator

v2.4.0

Published

Simple framework for dynamic json input validation

Downloads

23

Readme

Data Validator

A simple library to be used to validate JSON file content. The validation can be configured using the JSON file format and different kind of validation can be performed when some constraint is evaluated. Alternatively, the validator can be configured using fluent API.

Suppose we have to validate this data:

const data = {
  num1: 5,
  num2: 6,
  op: 'add',
  result: 11
};

And we want, to ensure that result is 11 if op is add and result is 30 if op is mul.

The configuration for the validator would be:

const validatorConfig = {
  ruleSets: [
    { // We specify a constraint here: this ruleset is to be executed only if `op` is `add`
      constraints: [
        {
          type: 'FIELD_VALUE',
          path: 'op',
          value: 'add',
        },
      ],
      fields: {
        result: [
          {
            type: 'EXACT_VALUE',
            value: 11,
          },
        ],
      },
    },
    { // Here another ruleset. This time, we want this to be executed only if `op` is `mul`
      constraints: [
        {
          type: 'FIELD_VALUE',
          path: 'op',
          value: 'mul',
        },
      ],
      fields: {
        result: [
          {
            type: 'EXACT_VALUE',
            value: 30,
          },
        ],
      },
    },
  ],
};

And the data could be validated by running:

console.log(new Validator(validatorConfig).validate(data));

The exact same result can be obtained using the fluent API:

const validator = validatorBuilder()
    .newRule()
        .withFieldValueConstraint('op', 'add')
        .withField('result')
            .validate(RuleBuilder.exactValue.withValue(11))
    .newRule()
        .withFieldValueConstraint('op', 'mul')
            .validate(RuleBuilder.exactValue.withValue(30))
    .build();

console.log(validator.validate(data));

The validator will stop at the first error. If that is not the desired behaviour, it should be called as:

console.log(new Validator(validatorConfig).validate(data, true));

This way it will return an object like:

{
   valid: false,
   details: [{
      field: 'field1',
      message: 'error message'
   },{
      field: 'field2',
      message: 'error message2'
   }]
}

Configuration

A validator configuration is composed of an array of RuleSet configuration. Each RuleSet configuration is composed of 2 elements:

  • constraints - is a set of constraints that must evaluate to true for the ruleset to be executed
  • fields - a dictionary of all the fields to be validated. For each field, a set of validation rules can be specified

The configuration could be saved into a JSON file or into a JS file as in the example above.

For example, using e JSON file the configuration would be:

{
  "ruleSets": [
    { 
      "constraints": [
        {
          "type": "FIELD_VALUE",
          "path": "op",
          "value": "add",
        }
      ],
      "fields": {
        "result": [
          {
            "type": "EXACT_VALUE",
            "value": 11,
          }
        ]
      }
    },
    {
      "constraints": [
        {
          "type": "FIELD_VALUE",
          "path": "op",
          "value": "mul",
        },
      ],
      "fields": {
        "result": [
          {
            "type": "EXACT_VALUE",
            "value": 30
          }
        ]
      }
    }
  ]
}

RuleSets Constraints

FieldValue Constraints

Executes the RuleSet only if the value at the specified path has the specified value.

Configuration

{
  "type": "FIELD_VALUE",
  "path": "path to the value to be evaluated (keys must be separated with a dot)",
  "value": "the value to be checked"
}

Validation Rules

The list of the supported rules can be found here

Adding new custom constraints

Custom constraints can be added by extending the AbstractConstraint class and implementing the isRespected method. The constraint configuration will be available as this.config.

The new constraint class must be registered into the ConstraintFactoryclass to make it visible to the Validator. To do so, simply call ConstraintFactory.register passing your constraint type string and a factory function than will take the config as parameter and will return an instance of your constraint.

Adding a new custom validation rule

Custom validation rules can be added by implementing the Rule, then you will have to register your new rule into the RuleFactory class by invoking the RuleFactory.register method passing the string type of your new rule and a factory function that will take the rule configuration as parameter.

Examples

All the examples can be found here