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

@mbokinala/data-validator

v0.2.0

Published

A data validation library designed to validate JSON data for REST APIs and provide useful validation error output

Downloads

1

Readme

Data Validation

A (work in progress) data validation library designed to validate JSON data for REST APIs and provide useful validation error output

Installation

npm install @mbokinala/data-validator

or

yarn add @mbokinala/data-validator

Example

const {validate, isDefined, isNotEmpty, isType, isGreaterThan, validateArray, includes} = require('@mbokinala/data-validator');

let data = {
    age: 18,
    profession: false,
    favoriteLanguages: [
        'java',
        'go',
        12,
        'python'
    ]
}

let [valid, errors] = validate(data, {
    name: [isDefined, isType('string'), isNotEmpty],
    age: [isDefined, isType('number'), isGreaterThan(21)],
    profession: [isDefined, isType('string')],
    favoriteLanguages: [isDefined, validateArray([isDefined, isType('string')]), includes('typescript')]
})

console.log('Validation result is', valid);

if (!valid) {
    console.log(JSON.stringify(errors, null, 4));
} else {
    console.log('valid');
}

Output:

Validation result is false
{
    "name": [
        "is null or undefined",
        "is not of type `string`",
        "is empty"
    ],
    "age": [
        "is not greater than 21"
    ],
    "profession": [
        "is not of type `string`"
    ],
    "favoriteLanguages": [
        {
            "2": [
                "is not of type `string`"
            ]
        },
        "does not include `typescript`"
    ]
}

Usage

validate(data, validators) => [valid: boolean, errors? ValidationErrors] The library is designed so that you can return errors as a JSON object in something like a REST API.

validators should match the structure of the data you want to validate. For each key, the value should be an array of validator functions to be run on the field. There are some built-in validator functions (see below), but you can create custom validators using the format:

// valid: true if input passes the validation test, false if otherwise
// message: an error message to be displayed if validation fails
(input: any) => [valid: boolean, message?: string];

Built-in validators:

| Function | Description | |-------------------------------------------------- |----------------------------------------------------------------------------------------------------------------------------------------------------- | | isDefined | Passes if input is not null or undefined | | isType(type: string) | Passes if input matches provided type | | isArray | Passes if input is an array | | isNotEmpty | Passes if input string or array is not empty | | equals(val: any) | Passes if input matches provided value | | includes(element: any) | Passes if input array contains provided element | | isGreaterThan(val: number) | Passes if input is greater than provided value | | isGreaterThanOrEqualTo(val: number) | Passes if input is greater than or equal to provided value | | isLessThan(val: number) | Passes if input is less than provided value | | isLessThanOrEqualTo(val: number) | Passes if input is less than or equal to provided value | | validateArray(validators: ValidatorFunction[]) | Passes if all elements in input array pass all provided validators | | ifDefined(validators: ValidatorFunction[]) | Passes if input is defined or if input passes all provided validators. Useful for when a validator is only meant to be run when a value is defined. |