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

happy-chappy

v2.0.0

Published

JSON object validation module

Downloads

23

Readme

happy-chappy

Happy-chappy is a compact, dependency-free JSON object validator package for JavaScript and TypeScript.

Usage

import { Schema, createValidator, validate } from "happy-chappy";

const MY_OBJECT_SCHEMA: Schema = {
    object: {
        members: {
            firstField: "number",

            secondField: {
                number: {},
                optional: true
            },

            thirdField: "string",

            fourthField: {
                enum: [1, 2, 3, "four", 5]
            }
        }
    }
};

const validateMyObject = createValidator(MY_OBJECT_SCHEMA);
const myObject = { firstField: 5, thirdField: "hey", fourthField: "four" };

validateMyObject(myObject) === true;

// Or alternatively
validate(myObject, MY_OBJECT_SCHEMA) === true;

Many more examples available in the test folder.

Data types

Supported data types are:

  • String
  • Number
  • Boolean
  • Array
  • Object
  • Enumeration

Each of these can be further restricted using matcher functions or specific configuration.

String options

Text string validation is supported using multiple types of matcher. A string can be matched by:

  • An exact value
  • A regular expression
  • A matcher function

Number options

Number validation options allow to restrict the available range as follows:

  • Exact value matching (checked with the strict equality operator or matcher)
  • Minimum threshold
  • Maximum threshold
  • Integer vs floating point
  • Epsilon (custom floating point comparison constant)

Array options

Arrays can be further scoped by setting the following:

  • Exact length
  • Minimum length
  • Maximum length
  • Matcher function

Object options

Object specification can be customised by allowing extra memebrs not to be taken into account during validation or by specifying a matcher function.

Enumerations

Enumerated values can be any string or number, matched with the strict equality operator.

Matchers

Matchers allow more complex logic to be included in the validation model. For example:

const isPersonWithAgeTuple = (v: any[]) => (
    v.length === 2
    && typeof v[0] === "string"
    && typeof v[1] === "number"
    && Number.isInteger(v[1])
); // Enforces [Name: string, Age: integer]

const schema: Schema = {
    array: { matcher: isPersonWithAgeTuple }
};

validate(["Dummy", 5], schema) === true;
validate([5, "Dummy"], schema) === false;

Typing Aids

Version 2 brings in typing aids for objects and enumerations to streamline the schema ceration process. When you define a schema for an object or enumeration you can provide a type that will be used to assist in constraining object members and enumerated values.

interface MyRequest {
    a: number
    b: string
}

const MY_REQUEST_SCHEMA: Schema<MyRequest> = {
    object: {
        members: {
            a: { ... },
            b: { ... },
            c: { ... }, // TypeScript error!
        }
    }
};
enum MyEnum {
    first = 1,
    second = 2
}

const MY_REQUEST_SCHEMA: Schema<MyEnum> = {
    enum: [
        MyEnum.first,
        2,
        3 // TypeScript error!
    ]
};
type MyEnum = "first" | "second";

const MY_REQUEST_SCHEMA: Schema<MyEnum> = {
    enum: [
        "first",
        "second",
        "third" // TypeScript error!
    ]
};

Changelog

Read the changelog here.

License

This package is licensed under the ISC license.

Bugs and feedback

You can find the repostiory for this package on GitHub.