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 🙏

© 2026 – Pkg Stats / Ryan Hefner

js-object-schema

v2.0.5

Published

A tool for validating javascript objects

Readme

js-object-schema

A very small, flexible, easy to use, javascript object validation tool.

Install

$ npm install js-object-schema

Usage

// Import  
import JsObjectSchema from 'js-object-schema';
// Or: const JsObjectSchema = require('js-object-schema');

// Create schema
const schema = new JsObjectSchema('Pokemon', {
    name: ({ name }) => typeof name === 'string', 
    maxLevel: ({ maxLevel }) => typeof maxLevel === 'number' && maxLevel > 0,
    level: ({ level, maxLevel }) => typeof level === 'number' && level <= maxLevel
})

const pokemon = { name: 'Pikachu', level: 99, maxLevel: 99};

const result = schema.validate(object);

// Check result
if (result.error) {
    // Do somthing with the errors if any
}

Api Reference

JsObjectSchema(name, schema, options)

| Argument | Description | Default value | |-----------------------------------|------------------------------------------------------------------------------|-----------------------------------------------------| | name: String | The name of the schema. Providing name will give more accurate error message | 'Object' | | schema: Schema | See Schema reference. | {} | | options: Options | See Options reference | {parseObject: false, rootObjectValidation: null } |

JsObjectSchema.validate(object)

Call this function to validate an object

| Argument | Description | Default value | |--------------------|-------------------------------------|---------------| | object: Object | The object that should be validated | undefined |

| Return Value | Description | Example | |---------------------|------------------------------------------------------|------------------------------------------------------| | result | An object containing the validated object and error | { object: Object, error: null } | | result.object | The validated and optionally parsed object. | { key: 'value' } | | result.error | The error object. When no errors, this is null. | { message: 'Schema validation error', errors: [] } | | result.error.errors | The validation errors for each prop. | [ new Error("'key' is invalid") ] |

Example:

const JsObjectSchema = require('js-object-schema');
const schema = new JsObjectSchema('Pokemon', {
    name: ({ name }) => typeof name === 'string', 
    maxLevel: ({ maxLevel }) => typeof maxLevel === 'number' && maxLevel > 0,
    level: ({ level, maxLevel }) => typeof level === 'number' && level <= maxLevel
}, { parseObject: true });

const pokemon = { name: 'Pikachu', level: 99, maxLevel: 99 };
const { error, object } = schema.validate(object);

Schema

The schema should be an object that reflects the object it should validate. The schema property can be a function, array or JsObjectSchema.

Basic Schema

A schema property function is called with the whole object as argument. If it returns true, the property is interpreted as valid:

const pokemonSchema = new JsObjectSchema('Pokemon', {
    name: ({ name }) => typeof name === 'string', // name must be a string 
    maxLevel: ({ maxLevel }) => typeof maxLevel === 'number' && maxLevel > 0, // maxLevel must be a number grater than 0
    level: ({ level, maxLevel }) => typeof level === 'number' && level <= maxLevel // level must be a number lesser than or equal to maxLevel
});

Nested Schemas

The schema can be nested! Simply create an object within the schema object:

const trainerSchema = new JsObjectSchema('Pokemon', {
    name: ({ name }) => typeof name === 'string',
    badges: ({ badges }) => Array.isArray(badges),
    pokemon: {
        name: ({ name }) => typeof name === 'string',
        maxLevel: ({ maxLevel }) => typeof maxLevel === 'number' && maxLevel > 0,
        level: ({ level, maxLevel }) => typeof level === 'number' && level <= maxLevel
    },
});

Or create a schema and map it to the prop:

const pokemonSchema = new JsObjectSchema('Pokemon', { /* ..props */ });

const trainerSchema = new JsObjectSchema('Pokemon', {
    // ...props
    pokemon: pokemonSchema,
});

If the property is an array, simply put the validation function, object or schema inside an array. Each item in the array will be validated:

const trainerSchema = new JsObjectSchema('Pokemon', {
    pokemon: [ pokemonSchema ],
    bag: [ { /*.. props */ } ],
    badges: [ (badge) => badge !== null ]
});

Options

| Prop | Description | Default value | |---------------------------------------|-------------------------------------------------------------------------------------------------------|---------------| | parseObject: Boolean | When true, the object will pe parsed to match the schema. Not inherited by nested schemas. | false | | rootObjectValidation: function | A function that is invoked with the root object. When returning true, object is interpreted as valid. | null |

License

MIT

Enjoy!