Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

Commit

Permalink
Merge pull request #5 from sebastian-software/0.2
Browse files Browse the repository at this point in the history
Milestone 0.2
  • Loading branch information
fastner committed May 13, 2016
2 parents 9539fca + bd93936 commit 56151fd
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 167 deletions.
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ node_modules/
npm-debug.log

test.js
src
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
language: node_js
node_js:
- "4"
- "5"
- "6"
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@
},
"homepage": "https://github.com/sebastian-software/vuex-validator",
"dependencies": {
"lodash": "^4.11.1"
"lodash-es": "^4.12.0"
},
"main": "lib/VuexValidator.js",
"jsnext:main": "src/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",
Expand Down
16 changes: 16 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 3 additions & 1 deletion src/BaseValidator.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
157 changes: 6 additions & 151 deletions src/VuexValidator.js
Original file line number Diff line number Diff line change
@@ -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
})
152 changes: 152 additions & 0 deletions src/VuexValidatorPlugin.js
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 1 addition & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
{
Expand Down

0 comments on commit 56151fd

Please sign in to comment.