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

@hlorenzi/validate

v2.0.0

Published

Object validation and normalization using a MongoDB-query-like schema.

Downloads

3

Readme

validate

Performs validation and normalization on JavaScript objects, through schemata also defined as JavaScript objects, using a MongoDB-query-like structure.

Install with: npm install @hlorenzi/validate

There's a battery of tests at npm test, and test coverage checks at npm run test-coverage.

Example

import { Validator } from "@hlorenzi/validate"

const schema =
{
    name:   { $type: "string", $maxLen: 30 },
    age:    { $type: "int", $min: 0 },
    height: { $type: "float" },
    
    address:
    {
        number: { $type: "int" },
        street: { $type: "string" }
    },
    
    info: { $optional: true, $type: "string" }
}

const object =
{
    name: "Bob",
    age: 25,
    height: 1.79,
    address:
    {
        number: 10,
        street: "One Street"
    }
}

const normalizedObject = Validator.validateOrThrow(schema, object)

Validator API

Validator.validateOrThrow(schema, object, options = {})
Validator.validateOrNull(schema, object, options = {})

These are the main entry points for validation. They all return a normalized object containing only the relevant fields:

  • undefined or null values, where accepted, are removed from the object.
  • By using { discardUnknownFields: true } for options, extraneous fields not defined in the schema will also be simply removed, instead of being considered an error.

Schema Structure

{
    field1: { $type: "..." },
    field2: { $type: "..." },
    field3:
    {
        nestedField1: { $type: "..." },
        nestedField2: { $type: "..." },
    }
}

Fields not starting with a $ are treated as being expected to exist in the object under validation.

The fields on the object have their values validated by using the directives contained in the schema's corresponding fields, such as the { $type: "..." } declarations above.

A basic directive can only specify the type, as in { $type: "string" }, but depending on the type, they can also have extra constraints. These are independent from one another, so you can specify only the ones that are relevant:

{ $type: "string",
    $minLen: 0,
    $maxLen: 30,
    $in: [ "apple", "orange", "banana" ] }
    
{ $type: "int",
    $min: 0,
    $max: 10,
    $minExclusive: 0,
    $maxExclusive: 10 }
    
{ $type: "float",
    $min: 0,
    $max: 10,
    $minExclusive: 0,
    $maxExclusive: 10,
    $acceptInfinity: true,
    $acceptNaN: true }
    
{ $type: "bool" }

{ $type: "array", $of: { /* nested directive */ },
    $minLen: 1,
    $maxLen: 5 }

Fields can also be declared with the $optional directive, in which case the validator will accept missing, undefined, and null values. For example:

{
    field1: { $type: "string", $optional: true }
}

For multiple possible types, you can use the $either directive:

{
    field1:
    {
        $either:
        [
            { $type: "string", $maxLen: 30 },
            { $type: "float", $max: 10 },
            { $type: "float", $min: 20 }
        ]
    }
}

Multiple errors can be detected in a single validation pass, and the validation functions will provide an array of errors upon unsuccessful validation.

  • validateOrThrow(schema, object, options = {}) will return the normalized object or throw the array of errors.
  • validateOrNull(schema, object, options = {}) will return the normalized object or return null on error, never throwing.

When the validation throws, you can check whether the error is coming from an actual validation error against the schema, as opposed to some other unexpected exception, by using:

  • isValidatorErrorArray(err) will return true if the given exception is an array of validation errors.

Each object in the array of validation errors contains two fields:

  • path is a string describing the name of the field that is invalid, possibly using dot syntax for nested fields, such as "address.street".
  • failure is a string describing the internal reason for the field's value being rejected.