Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

happy-chappy

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

happy-chappy

JSON object validation module

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 02 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc