Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ktsn/vuex-assert
Assertion for Vuex state
https://github.com/ktsn/vuex-assert
assert vue vuex
Last synced: about 1 month ago
JSON representation
Assertion for Vuex state
- Host: GitHub
- URL: https://github.com/ktsn/vuex-assert
- Owner: ktsn
- License: mit
- Created: 2016-11-06T02:20:44.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2016-11-12T07:48:32.000Z (about 8 years ago)
- Last Synced: 2024-11-16T01:33:32.114Z (about 2 months ago)
- Topics: assert, vue, vuex
- Language: TypeScript
- Size: 64.5 KB
- Stars: 54
- Watchers: 5
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vuex-assert
[![npm version](https://badge.fury.io/js/vuex-assert.svg)](https://badge.fury.io/js/vuex-assert)
[![Build Status](https://travis-ci.org/ktsn/vuex-assert.svg?branch=master)](https://travis-ci.org/ktsn/vuex-assert)Assertion for Vuex state
## Examples
Just add `assertions` into your Vuex modules.
```js
// 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.
```js
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.
```
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