Code Monkey home page Code Monkey logo

vuex-assert's Introduction

vuex-assert

npm version Build Status

Assertion for Vuex state

Examples

Just add assertions into your Vuex modules.

// modules/users.js
import {
  boolean,
  number,
  string,
  object,
  array
} from 'vuex-assert'

export default {
  state: {
    isLoading: false,
    error: null,
    users: []
  },

  assertions: {
    isLoading: boolean,
    error: object({
      code: number,
      message: string
    }).optional,
    users: array(object({
      id: number.assert(id => id > 0, 'id should be unsigned'),
      name: string
    }))
  },

  // ... module getters, actions and mutations ...
}

Add assertPlugin to plugins option of Vuex.Store with your modules.

import Vuex from 'vuex'
import modules from './modules'
import { assertPlugin } from 'vuex-assert'

const store = new Vuex.Store({
  modules,
  plugins: [
    assertPlugin({ modules })
  ]
})

Then, the store state will be validated for every mutation. Like following message will be printed if the assertion is failed.

[vuex-assert] state.users.error.code == null, number is expected

API

  • class Assertion

    • optional: Assertion

      Get assertion for optional type of this assertion.

    • assert(fn: (value: any) => boolean, message?: string): Assertion

      Include additional assertion for this assertion.

  • assertPlugin(options): VuexPlugin

    Create Vuex plugin with options.

    • options.assertions: { [key: string]: Assertion }
    • options.modules: { [key: string]: VuexModule }
  • assert(fn: (value: any) => boolean, message?: string): Assertion

    Create new assertion.

  • number: Assertion

    Assertion for number.

  • string: Assertion

    Assertion for string.

  • boolean: Assertion

    Assertion for boolean.

  • optional: Assertion

    Assertion for null or undefined.

  • object(assertions?: { [key: string]: Assertion }, message?: string): Assertion

    Create assertion for object with properties assertions.

  • array(assertion?: Assertion, message?: string): Assertion

    Create assertion for array with items assertions.

  • and(assertions: Assertion[]): Assertion

    Intersect given assertions.

  • or(assertions: Assertion[]): Assertion

    Union given assertions.

License

MIT

vuex-assert's People

Contributors

ktsn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.