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

orange-dragonfly-validator

v0.8.0

Published

Library for input parameters validation

Downloads

51

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)
  • transform (function): transforms value for validation
  • apply_transformed (boolean): if true, replaces original value with value returned by function provided as transform parameter
  • per_type (object): custom rules for the specific type (makes sense if multiple types are allowed). Acceptable parameters: in, in:public, min, max, pattern, special, transform, apply_transformed, children