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 地支持同步和异步校验。