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

@amphibian/validate

v1.6.1

Published

simple schema validation

Downloads

49

Readme

validate

build status

simple schema validation

npm install @amphibian/validate
var {validate} = require('@amphibian/validate');

Building a schema

Set up schemas as objects that have a type property with a string describing the expected type.

// String schema
{type: 'string'}

type can be any of the following: any, undefined, boolean, number, string, regexp, array, object, date, function, promise.

schema.optional

Provide boolean optional if validate shouldn't validate input when it is undefined.

// NOTE Will not throw
validate(undefined, {type: 'string', optional: true});

schema.test

Provide function test to do your own validation after validate has succeeded. It is given three arguments: input, objectPath, and objectPathArray. objectPath is a readable string showing the location of the error in the provided schema. objectPathArray is the array representation of that string that can be passed onto other validate functions.

// NOTE Will throw custom error
validate('password', {
    type: 'string',
    test: (input, objectPath) => {
        if (input.length < 10) {
            throw new Error(`Password too short at "${objectPath}"!`);
        }
    }
});

Object schema

Special property properties that describe children of the object.

{
    type: 'object',
    properties: {
        someChild: {type: 'string'},
        otherChild: {type: 'number', optional: true}
    }
}

Array schema

Special property indices that describe children of the array.

{
    type: 'array',
    indices: {type: 'string'}
}

Usage

The validate function takes two arguments: The variable you would like to validate, and the schema it should match. validate throws on error.

var {validate} = require('@amphibian/validate');

try {
    validate({}, {type: 'string'});
} catch (error) {
    console.log(error.code); // > type_error
}

Errors

If the schema validation fails, an @amphibian/error will be thrown. The error object is an Error instance with a few extra properties, and looks somewhat like this:

{
    message: 'Invalid Input (unknown_schema_type): schema.type undefned',
    status: 400,
    type: 'invalid_input',
    code: 'unknown_schema_type',
    data: ['schema.type', 'undefned']
}

The first index of error.data is the path to where the error was found.

Advanced schemas

To build more advanced and complex schemas, utilize the any type with custom test parameters.

var {validate, expectOneToSucceed} = require('@amphibian/validate');
var schema = {
    type: 'any',
    test: (input, objectPath, objectPathArray) => {
        try {
            expectOneToSucceed([
                () => validate(input, {type: 'object'}, objectPathArray),
                () => validate(input, {type: 'array'}, objectPathArray)
            ]);
        } catch (error) {
            throw new Error(`Use Object or Array at "${objectPath}"!`);
        }
    }
};

validate({}, schema);