非常强大的表单验证库 OverAssert

#1
import { of, maxLength, large, imageMatchP, itShould, always } from 'overassert';

of({name, avatarFile, age})
  .map(itShouldProp('name', maxLength(10), always({ nameError: 'the length should be large 10' })))
  .map(imageMatchP(
    itShouldProp('width', large(20), always({ avatarError: 'avatar width should large 20'})),
    itShouldProp('height', large(20), always({ avatarError: 'avatar height should large 20'}))
  ))
  .validate((success, reason) => {
    if (!success) {
       // for controlled error message component
        this.setState(reason);
    }
  });

直接将错误绑定到组件状态, 和受控组件配合使用。虽然框架无关,但是与 React 结合的非常好。

同时 OverAssert 非常 easy 地支持同步和异步校验。

#2

补充demo

import { of, itShould, always } from 'overassert';

of(x)
  .map(itShould(large(10), always('应该大于10')))
  .map(itShould(large(20), always('应该大于20')))
  .validate((success, value) => {
    if (success) {
       // value === x
    } else {
      // value === reason
    }
  })