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

object-shape-validator

v1.0.4

Published

Object shape validator inspired by React.js prop-types

Downloads

104

Readme

Object Shape Validator

Object shape validator inspired by React.js prop-types

Installation

Use the npm to install the module

npm install object-shape-validator

Usage

Creating basic shape and validate it:

import ObjectShape from 'object-shape-validator';

const shape = new ObjectShape({
  title: ObjectShape.string,
  tags: ObjectShape.arrayOf(ObjectShape.string),
  published: ObjectShape.bool
});

const object = {
  title: 'Example title',
  tags: ['news', 'advertisement'],
  published: false
};

const errors = shape.validate(object);
console.log(errors);

Validate with static method:

const errors = ObjectShape.validate(shape, object);

console.log(errors);

Options argument

You can also pass additional parameters as the last argument to validation function

const errors = shape.validate(object, {
  suppressOwnPropertiesValidation: true
});

console.log(errors);

List of options

  • suppressOwnPropertiesValidation: Disable validation for own properties. Useful when is needed to validate just few properties of an object to match the shape

Build-in types

ObjectShape.string - validates if value is string

const shape = new ObjectShape({
  title: ObjectShape.string
});

ObjectShape.number - validates if value is number

const shape = new ObjectShape({
  age: ObjectShape.number
});

ObjectShape.func - validates if value is func

const shape = new ObjectShape({
  test: ObjectShape.func
});

ObjectShape.bool - validates if value is boolean

const shape = new ObjectShape({
  published: ObjectShape.bool
});

ObjectShape.array - validates if value is array

const shape = new ObjectShape({
  items: ObjectShape.array
});

ObjectShape.object - validates if value is object

const shape = new ObjectShape({
  user: ObjectShape.object
});

ObjectShape.instanceOf - validates if value is instanceof class

class Animal {
  constuctor(type) {
    this.type = type;
  }
}

class Dog extends Animal {
  constructor() {
    super('dog');
  }
}

const shape = new ObjectShape({
  pet: ObjectShape.instanceOf(Dog)
});

ObjectShape.oneOf - validates if value is equal to one of the values

const shape = new ObjectShape({
  value: ObjectShape.oneOf(['First', 'Second'])
});

ObjectShape.oneOfType - validates if value is one of the types

const shape = new ObjectShape({
  value: ObjectShape.oneOfType([
    ObjectShape.string,
    ObjectShape.bool
  ])
});

ObjectShape.arrayOf - validates if value is array of type

const shape = new ObjectShape({
  items: ObjectShape.arrayOf(ObjectShape.string)
});

ObjectShape.objectOf - validates if value is a valid shape

const shape = new ObjectShape({
  user: {
    fname: ObjectShape.string,
    lname: ObjectShape.string,
    age: ObjectShape.number 
  }
});

ObjectShape.custom - custom validator function

const shape = new ObjectShape({
  value: ObjectShape.custom((value, key) => value === 'test' || `${key} is not equals to 'test'`)
});

or

const shape = new ObjectShape({
  value: (value, key) => value === 'test' || `${key} is not equals to 'test'`,
});

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT