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

guard-object

v1.0.2

Published

Quickly validate your object scheme without breaking a sweat

Downloads

147

Readme

guard-object

Quickly validate your object scheme without breaking a sweat

Key Advantages

  • Superfast (an error is thrown when a testCase doesn't match the footprint instead of scanning through the entire object)
  • Zero Dependency
  • Flexible
  • Validate deep nested objects and arrays

Installation

npm install guard-object

or using yarn

yarn add guard-object

Keywords

  • testCase: this is the data you are validating
  • footprint: this is the schema used for validating your data

Basic Usage

import { guardObject, GuardSignal } from 'guard-object';

const schema = guardObject({
    // provide custom validation
    username: value => typeof value === 'string' && value.length < 30,
    // must be a string
    password: GuardSignal.STRING,
    // must match a valid email pattern
    email: GuardSignal.EMAIL,
    // must be an epoch integar
    joinedOn: GuardSignal.DATES.OF_EPOCH
    // must be a valid https link
    photo: GuardSignal.HTTPS,
    location: {
        address: GuardSignal.STRING,
        // must be a valid coordinate in the format [lat, lng]
        coordinate: GuardSignal.COORDINATE.LAT_LNG_INT
    },
    static_field: 7
});

// validation will pass as the testCase matches the footprint 
schema.validate({
    username: 'Jackie Chan',
    password: 'strong_password',
    email: '[email protected]',
    joinedOn: 1713634539425,
    photo: 'https://brainbehindx.com/jackie_chan/photo.png',
    location: {
        address: 'Sango ota, ogun state Nigeria',
        coordinate: [6.707728, 3.256003]
    },
    static_field: 7
});

// this will throw an error as the testCase does not match the footprint 
schema.validate({
    username: 'Jackie Chan',
    password: 'strong_password',
    email: '[email protected]',
    // should be an integer epoch time
    joinedOn: 'Sat Apr 20 2024 18:43:55',
    photo: 'https://brainbehindx.com/jackie_chan/photo.png',
    // this field shouldn't be here
    unknown_field: 'this is an unknown field',
    location: {
        address: 'Sango ota, ogun state Nigeria'
        // coordinate not provided
    },
    // should be 7
    static_field: 5
});

Validator

all validators are expose so you can reuse them on the fly

import { Validator } from 'guard-object';

Validator.EMAIL('[email protected]'); // true

Validator.CARDS.VISA('3748 8282 9283 2939'); // false

dynamic array

to retain flexibility in validating data, guard-object validate it testCase in a literal way. so the following applies

import { guardObject, GuardSignal } from 'guard-object';

const scheme = guardObject({
    history: [
        { profit: GuardSignal.NUMBER, date: GuardSignal.DATES['DD-MM-YYYY'] },
        { profit: GuardSignal.NUMBER, date: GuardSignal.DATES['DD-MM-YYYY'] },
        { profit: GuardSignal.NUMBER, date: GuardSignal.DATES['DD-MM-YYYY'] } 
    ]
});

// validation will pass as each item matches the footprint
scheme.validate({
    history: [
        { profit: 259, date: '14-04-2024' },
        { profit: 345, date: '15-04-2024' },
        { profit: 134, date: '16-04-2024' } 
    ]
});

// validation will fail as the numbers of item in "history" field doesn't match the footprint
scheme.validate({
    history: [
        { profit: 259, date: '14-04-2024' },
        { profit: 345, date: '15-04-2024' }
    ]
});

to validate array dynamically when the length of an array is unknown

import { guardObject, GuardSignal, guardArray } from 'guard-object';

const scheme = guardObject({
    history: guardArray({
        profit: GuardSignal.NUMBER,
        date: GuardSignal.DATES['DD-MM-YYYY']
    })
});

// validation will pass
scheme.validate({
    history: [
        { profit: 259, date: '14-04-2024' },
        { profit: 345, date: '15-04-2024' },
        { profit: 134, date: '16-04-2024' } 
    ]
});

// validation will pass
scheme.validate({
    history: [
        { profit: 259, date: '14-04-2024' },
        { profit: 345, date: '15-04-2024' }
    ]
});

// validation will fail as balance wasn't provided in the footprint
scheme.validate({
    history: [
        { profit: 259, balance: 345, date: '14-04-2024' }
    ]
});

TODO

  • Option to enable throwing more refined error