Brings validation support to mobx-state-tree powered by validate.js.
mobx-state-tree2.x or 3.xvalidate.js>= 0.11
yarn add mst-validatejsThis library offers a withValidations extension that you can use to extend your models giving it 3 new views:
.errors.isValid.isInvalid
Using the .extend function on an mst model, you can add your validations in there. Check out the test directory here for a bigger example using credit cards.
import { types } from "mobx-state-tree"
import { withValidations } from "mst-validatejs"
export const UserModel = types
.model("User")
.props({
name: "",
age: 69,
})
.extend(
// here we go!
withValidations({
name: {
length: { minimum: 1, message: "name is required" },
},
age: {
numericality: {
onlyInteger: true,
greaterThanOrEqualTo: 0,
lessThan: 110,
message: "liar",
},
},
}),
)Find more about validate.js, it's a pretty great library.
Yes plz!
Fork it, Clone it, Branch it, Yarn it
Build it, Test it, Push it, PR it
To run the tests, I like to open two shells yarn test:compile:watch and yarn ava --watch.