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:
import ObjectShape from 'object-shape-validator';
const shape = new ObjectShape({
title: ObjectShape.string,
tags: ObjectShape.arrayOf(ObjectShape.string),
published: ObjectShape.bool
});Validate object:
const object = {
title: 'Example title',
tags: ['news', 'advertisement'],
published: false
};
console.log(shape.validate(object));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.custom - custom validator function
const shape = new ObjectShape({
value: ObjectShape.custom((key, value) => value === 'test' || `${key} is not equals to 'test'`)
});