aldendaniels / zan

test object types (similar to React.PropTypes)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

zan

NPM version Build status Test coverage Downloads

Drop in replacement for React.PropTypes:

import { types } from 'zan';
const { string, number } = types;
React.createClass({
  propTypes: {
    name: string,
    age: number.isOptional
  },
  render() {
    // ...
  },
});

The primary differences this has with React.PropTypes is that

  1. zan exposes an exactShape type.
  2. checks by default are isRequired already and they each expose an isOptional method
  3. zan exposes a createCustomChecker method which can be used as follows:
const urlString = createCustomChecker(value => /^https?:/.test(value) );

This module also exposes a recursive checker so you don't need to use things like arrayOf and shape:

import { types, recursive } from 'zan';
const { number, string } = types;

const propTypes = recursive({
  myAge: number,
  myFavoriteNumbers: [number],
  treeNode: {
    value: number
  },
  buddies: [{
    name: string,
    age: number,
  }],
});

// which is equivalent to:

const propTypes = {
  myAge: number,
  myFavoriteNumbers: arrayOf(number),
  treeNode: shape({
    value: number,
  }),
  buddies: arrayOf(shape({
    name: string,
    age: number,
  })),
};

This module also exposes an a checker so you can check types manually without React

Usage

import { check, types, createCustomChecker } from 'zan';

check(types.bool, true); // null
check(types.bool, 123); // returns Error object

check(types.shape({name: types.string}), {name: 'Me'}); // null
check(types.shape({name: types.string}), {}); // returns Error object
check(types.shape({name: types.string}), {name: 'Me', age: 22}); // null
check(types.exactShape({name: types.string}), {name: 'Me', age: 22}); // returns Error object

check is curry-able so const numberChecker = check(types.number); numberChecker(22); works

see test.js from more usage

About

test object types (similar to React.PropTypes)


Languages

Language:JavaScript 100.0%