https://github.com/polidog/vuex-jsonschema-validation
A vuex state validation plugin on change state.
https://github.com/polidog/vuex-jsonschema-validation
vue vuex vuex-plugin
Last synced: about 2 months ago
JSON representation
A vuex state validation plugin on change state.
- Host: GitHub
- URL: https://github.com/polidog/vuex-jsonschema-validation
- Owner: polidog
- License: mit
- Created: 2019-11-10T12:27:44.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-02-12T17:44:38.000Z (over 3 years ago)
- Last Synced: 2025-03-18T19:55:02.651Z (2 months ago)
- Topics: vue, vuex, vuex-plugin
- Language: JavaScript
- Homepage:
- Size: 321 KB
- Stars: 3
- Watchers: 1
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vuex-jsonschema-validation
A vuex state validation plugin on change state.
## usage
```shell
npm install --save vuex-jsonschema-validation
```create vuex store and json schema.
```js
// schema.json{
"type": "object",
"properties": {
"todos": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"text": {
"type": "string"
},
"done": {
"type": "boolean"
}
}
}
}
},
"additionalProperties": false
}
``````js
// store.jsimport Vue from 'vue'
import Vuex from 'vuex'
import VuexJsonSceham from 'vuex-jsonschema-validation'
import schema from './schema.json'
const plugin = VuexJsonSceham(schema)Vue.use(Vuex)
const state = {
todos: [
{ id: 1, text: 'taskA', done: true },
{ id: 2, text: 'taskB', done: false }
]
}const actions = {}
const mutations = {
change(state, { id, done }) {
const target = state.todos.find(todo => todo.id === id)
if (target) {
target.done = done
}
},
add(state, { text }) {
const lastId = state.todos.slice(-1)[0].id
state.todos.push({
id: lastId + 1,
text,
done: false
})
},
other() {
state.abc = 'd'
}
}const store = new Vuex.Store({
strict: process.env.NODE_ENV !== 'production',
plugins: [plugin],
state,
actions,
mutations
})export default store
```