From 745eb2e8b561b7d92b727beb5d07ea01f984629a Mon Sep 17 00:00:00 2001 From: Sebastian Fastner Date: Fri, 13 May 2016 16:21:39 +0200 Subject: [PATCH 1/8] Fix #2: Move VuexValidator code to own file; Use lodash-es --- src/BaseValidator.js | 4 +- src/VuexValidator.js | 157 ++----------------------------------- src/VuexValidatorPlugin.js | 152 +++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+), 152 deletions(-) create mode 100644 src/VuexValidatorPlugin.js diff --git a/src/BaseValidator.js b/src/BaseValidator.js index a9b14fb..e1eadc9 100644 --- a/src/BaseValidator.js +++ b/src/BaseValidator.js @@ -1,4 +1,6 @@ -import { reduce, isFinite, isString } from "lodash" +import reduce from "lodash-es/reduce" +import isFinite from "lodash-es/isFinite" +import isString from "lodash-es/isString" class ValidatorAssertions { invalid(fields, error) diff --git a/src/VuexValidator.js b/src/VuexValidator.js index bca3f08..24b32d7 100644 --- a/src/VuexValidator.js +++ b/src/VuexValidator.js @@ -1,152 +1,7 @@ -import { reduce } from "lodash" +import VuexValidatorPlugin from "./VuexValidatorPlugin" +import BaseValidator from "./BaseValidator" -const validators = [] -const validatorsMap = {} -const propertyToValidator = {} - -class GlobalValidator { - isValid(module = null) - { - if (module) - return validatorsMap[module].isValid() - - return reduce( - validators.map((validator) => validator.isValid()), - (all, self) => - { - if (all === true && self === true) - return true - - if (all !== true && self === true) - return all - - if (all === true && self !== true) - return self - - return all.concat(self) - } - ) - } -} - -const validator = new GlobalValidator() - -function propertyValidator(state) -{ - return { - isInvalid: (property) => - { - const vals = propertyToValidator[property] - if (!vals) - { - const moduleValidator = validatorsMap[property] - if (moduleValidator) - { - const isValid = moduleValidator.isValid() - return isValid === true ? null : isValid - } - - return null - } - - return reduce(vals.map((val) => val.validatorFunction(state)), (all, self) => - { - if (!self) - return all - - // It is possible, that a validation fails without being this property as reason - if (self.fields.indexOf(property) < 0) - return all - - if (all) - return all.concat(self) - - return [ self ] - }, null) - } - } -} - -function install(Vue, { validators: _validators } = { validators: [] }) -{ - /* eslint no-invalid-this: 0, no-console:0 */ - if (_validators.length === 0) - console.warn("[Vuex Validator] No validators found. Maybe you want to do Vue.use(validator, { validators: [myValidator] })") - - _validators.forEach((item) => - { - validators.push(item) - if (item.module) - validatorsMap[item.module] = item - }) - Vue.prototype.$validator = validator - - function validatorInit() - { - if (!this.$store) - throw Error("[Vuex Validator] Vuex store not injected. Please execute Vue.use(store) before executing Vue.use(validator)") - - if (!this.$store.$validator) - { - this.$store.$validator = validator - validators.forEach((item) => item.injectStore(this.$store)) - } - - const options = this.$options - const getters = options.computed = options.computed || {} - const state = this.$store.state - const statedPropertyValidator = propertyValidator(state) - const self = this - - validators.forEach((item) => - { - item.getProperties().forEach((prop) => - { - if (!propertyToValidator[prop]) - propertyToValidator[prop] = [] - - const rules = item.getRulesByProperty(prop) - if (rules) - rules.forEach((rule) => - { - if (propertyToValidator[prop].indexOf(rule) < 0) - propertyToValidator[prop].push(rule) - }) - }) - }) - - if (options && options.vuex && options.vuex.validators) - { - const vals = options.vuex.validators - Object.keys(vals).forEach((prop) => - { - const currentPropertyFnt = vals[prop] - getters[prop] = () => currentPropertyFnt.call(self, statedPropertyValidator) - }) - } - } - - const _init = Vue.prototype._init - Vue.prototype._init = function(options = {}) - { - options.init = options.init ? - [ validatorInit ].concat(options.init) : - [ validatorInit ] - _init.call(this, options) - } - - // option merging (found in vuex) - const merge = Vue.config.optionMergeStrategies.computed - Vue.config.optionMergeStrategies.vuexValidator = (toVal, fromVal) => - { - if (!toVal) return fromVal - if (!fromVal) return toVal - return { - getters: merge(toVal.getters, fromVal.getters) - } - } -} - -export default { - install -} +export default ({ + VuexValidator: VuexValidatorPlugin, + BaseValidator +}) diff --git a/src/VuexValidatorPlugin.js b/src/VuexValidatorPlugin.js new file mode 100644 index 0000000..c72cc83 --- /dev/null +++ b/src/VuexValidatorPlugin.js @@ -0,0 +1,152 @@ +import reduce from "lodash-es/reduce" + +const validators = [] +const validatorsMap = {} +const propertyToValidator = {} + +class GlobalValidator { + isValid(module = null) + { + if (module) + return validatorsMap[module].isValid() + + return reduce( + validators.map((validator) => validator.isValid()), + (all, self) => + { + if (all === true && self === true) + return true + + if (all !== true && self === true) + return all + + if (all === true && self !== true) + return self + + return all.concat(self) + } + ) + } +} + +const validator = new GlobalValidator() + +function propertyValidator(state) +{ + return { + isInvalid: (property) => + { + const vals = propertyToValidator[property] + if (!vals) + { + const moduleValidator = validatorsMap[property] + if (moduleValidator) + { + const isValid = moduleValidator.isValid() + return isValid === true ? null : isValid + } + + return null + } + + return reduce(vals.map((val) => val.validatorFunction(state)), (all, self) => + { + if (!self) + return all + + // It is possible, that a validation fails without being this property as reason + if (self.fields.indexOf(property) < 0) + return all + + if (all) + return all.concat(self) + + return [ self ] + }, null) + } + } +} + +function install(Vue, { validators: _validators } = { validators: [] }) +{ + /* eslint no-invalid-this: 0, no-console:0 */ + if (_validators.length === 0) + console.warn("[Vuex Validator] No validators found. Maybe you want to do Vue.use(validator, { validators: [myValidator] })") + + _validators.forEach((item) => + { + validators.push(item) + if (item.module) + validatorsMap[item.module] = item + }) + Vue.prototype.$validator = validator + + function validatorInit() + { + if (!this.$store) + throw Error("[Vuex Validator] Vuex store not injected. Please execute Vue.use(store) before executing Vue.use(validator)") + + if (!this.$store.$validator) + { + this.$store.$validator = validator + validators.forEach((item) => item.injectStore(this.$store)) + } + + const options = this.$options + const getters = options.computed = options.computed || {} + const state = this.$store.state + const statedPropertyValidator = propertyValidator(state) + const self = this + + validators.forEach((item) => + { + item.getProperties().forEach((prop) => + { + if (!propertyToValidator[prop]) + propertyToValidator[prop] = [] + + const rules = item.getRulesByProperty(prop) + if (rules) + rules.forEach((rule) => + { + if (propertyToValidator[prop].indexOf(rule) < 0) + propertyToValidator[prop].push(rule) + }) + }) + }) + + if (options && options.vuex && options.vuex.validators) + { + const vals = options.vuex.validators + Object.keys(vals).forEach((prop) => + { + const currentPropertyFnt = vals[prop] + getters[prop] = () => currentPropertyFnt.call(self, statedPropertyValidator) + }) + } + } + + const _init = Vue.prototype._init + Vue.prototype._init = function(options = {}) + { + options.init = options.init ? + [ validatorInit ].concat(options.init) : + [ validatorInit ] + _init.call(this, options) + } + + // option merging (found in vuex) + const merge = Vue.config.optionMergeStrategies.computed + Vue.config.optionMergeStrategies.vuexValidator = (toVal, fromVal) => + { + if (!toVal) return fromVal + if (!fromVal) return toVal + return { + getters: merge(toVal.getters, fromVal.getters) + } + } +} + +export default { + install +} From eda4966054ce6d97b7e45edd7917ec18f44f45d6 Mon Sep 17 00:00:00 2001 From: Sebastian Fastner Date: Fri, 13 May 2016 16:22:08 +0200 Subject: [PATCH 2/8] Generate lib as UMD module via buble --- rollup.config.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 rollup.config.js diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..7d081b6 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,16 @@ +import nodeResolve from "rollup-plugin-node-resolve" +import buble from "rollup-plugin-buble" + +export default { + entry: "src/VuexValidator.js", + format: "umd", + moduleName: "VuexValidator", + plugins: [ + nodeResolve({ + jsnext: true + }), + buble() + ], + dest: "lib/VuexValidator.js", + sourceMap: true +} From d7c415dce52b9bc0ef3f3dbd41a3a8b6f7d03418 Mon Sep 17 00:00:00 2001 From: Sebastian Fastner Date: Fri, 13 May 2016 16:22:36 +0200 Subject: [PATCH 3/8] Switch from babel to rollup + buble --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index e923d62..fd2afe0 100644 --- a/package.json +++ b/package.json @@ -20,18 +20,18 @@ "main": "lib/VuexValidator.js", "devDependencies": { "ava": "^0.14.0", - "babel-cli": "^6.7.7", - "babel-preset-es2015": "^6.6.0", - "babel-register": "^6.7.2", "eslint": "^2.8.0", "release-it": "^2.3.1", + "rollup": "^0.26.3", + "rollup-plugin-buble": "^0.7.0", + "rollup-plugin-node-resolve": "^1.5.0", "s15e-javascript": "^0.1.17", "updtr": "^0.1.10" }, "scripts": { "lint": "eslint --cache *.js src/*.js", "autofix": "eslint --cache --fix *.js src/*.js", - "build": "babel --source-maps true --out-dir ./lib/ ./src/", + "build": "rollup -c", "pretest": "npm run build", "test": "ava", "test:watch": "ava --watch", From 3d51ff0165fd2c50d460adba98d99ffe3324f8d6 Mon Sep 17 00:00:00 2001 From: Sebastian Fastner Date: Fri, 13 May 2016 16:22:50 +0200 Subject: [PATCH 4/8] Use lodash-es instead of lodash --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fd2afe0..fa6aea8 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ }, "homepage": "https://github.com/sebastian-software/vuex-validator", "dependencies": { - "lodash": "^4.11.1" + "lodash-es": "^4.12.0" }, "main": "lib/VuexValidator.js", "devDependencies": { From f8de53db339298d5d1b28724a480f04106119e79 Mon Sep 17 00:00:00 2001 From: Sebastian Fastner Date: Fri, 13 May 2016 16:23:13 +0200 Subject: [PATCH 5/8] Also distribute src folder for ES6 --- .npmignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.npmignore b/.npmignore index 4c221ca..07b4535 100644 --- a/.npmignore +++ b/.npmignore @@ -10,4 +10,3 @@ node_modules/ npm-debug.log test.js -src From fdf427c12aab704a4c788bc369860faf7e17c9e5 Mon Sep 17 00:00:00 2001 From: Sebastian Fastner Date: Fri, 13 May 2016 16:27:26 +0200 Subject: [PATCH 6/8] Fix #3: Add jsnext for ES6 modules --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index fa6aea8..6441239 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "lodash-es": "^4.12.0" }, "main": "lib/VuexValidator.js", + "jsnext:main": "src/VuexValidator.js", "devDependencies": { "ava": "^0.14.0", "eslint": "^2.8.0", From 92a3996119c04a2b05b576248435c70941c0a5db Mon Sep 17 00:00:00 2001 From: Sebastian Fastner Date: Fri, 13 May 2016 17:57:51 +0200 Subject: [PATCH 7/8] Remove babel test dependency and use es5 from lib --- .babelrc | 4 ---- test.js | 4 +--- 2 files changed, 1 insertion(+), 7 deletions(-) delete mode 100644 .babelrc diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 4042b67..0000000 --- a/.babelrc +++ /dev/null @@ -1,4 +0,0 @@ -{ - "presets": ["es2015"], - "compact": "auto" -} diff --git a/test.js b/test.js index 52b279a..417f3ae 100644 --- a/test.js +++ b/test.js @@ -7,10 +7,8 @@ */ import test from "ava" -import "babel-register" -import VuexValidator from "./src/VuexValidator" -import BaseValidator from "./src/BaseValidator" +import { VuexValidator, BaseValidator } from "./lib/VuexValidator" test("VuexValidator Plugin is valid", (api) => { From bd9393666f85876006e57a3e4391da4d32a6fc08 Mon Sep 17 00:00:00 2001 From: Sebastian Fastner Date: Fri, 13 May 2016 18:02:33 +0200 Subject: [PATCH 8/8] Remove old nodejs environment for tests --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3f308e8..4909f83 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,3 @@ language: node_js node_js: - - "4" - - "5" - "6"