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

@ennis/valid

v1.0.1

Published

Simple object validation

Downloads

1

Readme

@ennis/valid

Simple object validation. Leverages the javascript global type constructors for easier composition. Expects JSON-like values (doesn't mess with functions, dates, etc).

Install

npm i @ennis/valid

Usage

const Val = require('@ennis/valid');

let schema = {
    shouldBeArray: Array,
    hopeItsANumber: Number,
    nestedObject: {
        inside: String,
    },
    arraysToo: [
        {eachOne: String}
    ],
    stringOrNum: Val.or(String, Number),
    intsAreAnnoying: Number.isInteger,
    upToYou: Val.optional(String)
}

Val.validate(schema, {
    shouldBeArray: ['hi'],
    hopeItsANumber: 13.5,
    nestedObject: {
        inside: 'good so far'
    },
    arraysToo: [
        {eachOne: 'foo'},
        {eachOne: 'bar'}
    ],
    stringOrNum: 'string it is'
    intsAreAnnoying: 12
});
// returns: {valid: true, errors: []}

Val.validate({
    name: String,
    age: val => val > 20
}, {
    name: null,
    age: 14
});
/* returns: {
     valid: false,
     errors: [
         ".name: expected string, got null",
         ".age: integer did not pass validator function"
     ]
}*/

Types

All valid types and definitions are shown here: | type | valid definitions | | --- | --- | | object | 'object', Object | | array | 'array', Array | | string | 'string', String | | boolean | 'boolean', Boolean | | number | 'number', Number | | integer | 'integer', Number.isInteger | | null | 'null', null | | exists (not null/undefined) | 'exists' | | validator function | value => true/false |

API

.validate(schema, inquestion)

Recursively travels through an object or array to ensure all values are present and of the proper type. Accepts type definitions or validator functions. Schemas can include a single definition as the first item of an array. See usage example

Returns an object:

{
    valid: true || false,
    errors: ['human readable invalid features']
}

.isInvalid(typeDefinition, value)

Atomic function of .validate(). Just checks the value against the type definition. Returns false or a string explaining the fault.

For example:

const {isInvalid } = require('@ennis/valid');

isInvalid(String, 13);
// returns 'expected string, got: integer'
isInvalid(Number, 13);
// returns false

Type operators

.optional(type)

Modify a type definition to make it optional. Present values of another type will be invalid.

For example:

const {optional, isInvalid } = require('@ennis/valid');

isInvalid(optional(String), null) 
// false;
isInvalid(optional(String), 13) 
// 'expected optional string, got: integer'

.notEmpty(arrayDefinition)

By default an empty array, even if it has a defined nested structure in the schema, is valid when its empty. This ensures there is atleast one item in the array. Has nonEmpty() alias.

For example:

const {validate, notEmpty} = Val;

let schema = {
    users: notEmpty([
        {id: Number, name: String}
    ])
}
validate(schema, {users: []})
// {valid: false, errors: ['.users: expected non empty array, got: array']}
validate(schema, {users: [
    {id: 1, name: 'John'},
    {id: 2, name: 'Jane'}
]})
// {valid: true, errors: []}

.or(...types)

Accepts a list of type definitions (or a single aray). A present value of any of the types will be valid. Nested objects/arrays won't work.

For example:

const {or, isInvalid } = require('@ennis/valid');

isInvalid(or(String, Object), 15) 
// 'expect string or object, got: integer';
isInvalid(or(String, Number), 13) 
// false