orange-dragonfly-validator

Library for input parameters validation

Usage no npm install needed!

<script type="module">
  import orangeDragonflyValidator from 'https://cdn.skypack.dev/orange-dragonfly-validator';
</script>

README

Orange Dragonfly Validator

One day Orange Dragonfly will become a NodeJS framework. For now I'm starting to publish its components.

This library is created for input parameters' validation.

How it works?

You have input in some object (Input). You have another object with schema of allowed input (Schema).

Schema describes available Input and there are any issues throwing an exception which contains information about the errors in the Input.

Simple example which explains the idea

const validate = require("orange-dragonfly-validator")

const rules = {
  "name": {
    "type": "string",
    "pattern": /^[A-Z]([a-z]+)$/
  },
  "position": {
    "type": "string"
  },
  "term_ends": {
    "type": "integer",
    "min": 2025
  }
}

function f(input) {
  try {
    validate(rules, input)
    console.log(`${input.name}'s job as ${input.position} ends in ${input.term_ends}`)
  } catch (e) {
    console.error(e.message, e.info)
  }
}

f({
  "name": "Donald",
  "position": "President of the United States",
  "term_ends": 2021
})

// Output: "Validation failed { term_ends: [ 'Minimal value (length) is 2025. 2021 provided' ] }"

f({
  "name": "Donald",
  "position": "President of the United States",
  "term_ends": 2025
})

// Output: "Donald's job as President of the United States ends in 2025"

Configuration

Rule definition

There is no required params in rule's definition.

  • type (string or array): describes allowed types of the parameter. Allowed values: string, number, integer, array, object, boolean, null.
  • in (array): describes allowed values.
  • in:public (boolean or array): if true error message of in property will have list of available values. If array is provided if will be provided in error message of in instead of in values. For example it may be used if some of available values is deprecated and should not be exposed to users.
  • min (integer): minimal (length of) value (applicable for integer, number, string and array values).
  • max (integer): maximal (length of) value (applicable for integer, number, string and array values).
  • required (boolean): show is value required or not.
  • pattern (RegExp): RegExp object.
  • default (any type): default value. It will ve added to the Input if it is not provided.
  • children (object): description of children value (applicable for array and object).
    • # (rule object): rule for object key validation. Applicable for root of the schema as well.
    • * (rule object): rule for all values of object or array. Applicable for root of the schema as well.
    • %key% (rule object): rule for specific object key's value. Applicable for root of the schema as well (it is how you define rules).
    • @ (object): options. Applicable for root of the schema as well.
      • strict (boolean): in strict mode validator does not allow in Input keys not defined in Rules (default is true, but it can also be overridden in options argument of validate function)