0.1.0 • Published 6 years ago

mst-validatejs v0.1.0

Weekly downloads
53
License
MIT
Repository
github
Last release
6 years ago

mst-validatejs

Brings validation support to mobx-state-tree powered by validate.js.

Requirements

  • mobx-state-tree 2.x or 3.x
  • validate.js >= 0.11

Installing

yarn add mst-validatejs

How To Use

This 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 "../src/with-validations"

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.

Contributors

  • Fork it
  • Clone it
  • Branch it
  • Yarn it
  • Build it
  • Test it
  • Push it
  • PR it
  • Merge it

To run the tests, I like to open two shells yarn test:compile:watch and yarn ava --watch.