JSPM

object-shape-validator

1.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 21
  • Score
    100M100P100Q57505F
  • License MIT

Object shape validator inspired by React.js prop-types

Package Exports

  • object-shape-validator

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (object-shape-validator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Description

Object shape validator inspired by React.js prop-types

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:

import ObjectShape from 'object-shape-validator';

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

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

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

console.log(errors);

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'`,
});