diff --git a/.gitignore b/.gitignore index ef3312f..d311984 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ coverage node_modules .DS_Store *.log +*.gz *.swp *~ diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d98378..d02fbff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,31 @@ + +# [2.0.0-alpha.7](https://github.com/vuejs/vue-validator/compare/v2.0.0-alpha.6...v2.0.0-alpha.7) (2015-12-10) + + +### Features + +* **syntax:** support array syntax on v-validate expression ([bf33bb4](https://github.com/vuejs/vue-validator/commit/bf33bb4)) + + + + +# [2.0.0-alpha.6](https://github.com/vuejs/vue-validator/compare/v2.0.0-alpha.6...v2.0.0-alpha.6) (2015-12-07) + + +### Performances + +* **bundle**: more compact the vue-validator + about 20% smaller build with rollupjs. + + - before + - vue-validator.min.js 11701 + - vue-validator.js 26180 + - after + - vue-validator.min.js 9309 + - vue-validator.js 20713 + + + # [2.0.0-alpha.5](https://github.com/vuejs/vue-validator/compare/v2.0.0-alpha.4...v2.0.0-alpha.5) (2015-11-24) diff --git a/README.md b/README.md index 55b48b9..ccc80df 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,8 @@ We can use `validator` element directive and `v-validate` directive. The followi
- - + +
Required your name. Your comment is too long. @@ -139,40 +139,44 @@ The various top-level properties has been defined in the validation scope, and t - `modified`: if modified field exist even **one** in validate fields, return `true`, else `false`. - `dirty`: if dirty field exist even **one** in validate fields, return `true`, else `false`. - `pristine`: whether **all** fields is pristine, if so, return `true`, else `false`. -- `errors`: if invalid even one exist, return all field error message wrapped with object, else `undefined`. +- `messages`: if invalid even one exist, return all field error message wrapped with object, else `undefined`. # Validator syntax `v-validate` directive syntax the below: ``` - v-validate:field.[validator]+[="primitive literal | object literal | binding"] + v-validate:field="array literal | object literal | binding" ``` ## Literal -### Primitive -The below is example that using literal of string value: +### Array +The below is example that using array literal: ```html - Zip:
+ Zip:
- Invalid format of your zip code. + Required zip code.
``` +Like the `required`, if you don't need to specify the rule, you should use it. + + ### Object The below is example that using object literal: ```html
- ID:
+ ID:
+ Required Your ID. Your ID is too short. Your ID is too long.
@@ -180,6 +184,21 @@ The below is example that using object literal: ``` +You can specify the rule value on the object literal. Like the `required`, you can specify the **dummy rule** value on the literal object. + +And also, you can specify strict object as the below: + +```html + + + ID:
+
+ Your ID is too short. + Your ID is too long. +
+ +``` + ## Binding The below is example that using binding: @@ -198,7 +217,7 @@ new Vue({
- ID:
+ ID:
Your ID is too short. Your ID is too long. @@ -215,9 +234,9 @@ You can grouping validation results. the below example: ```html - username:
- password:
- password (confirm): + username:
+ password:
+ password (confirm):
Required your name.
@@ -232,10 +251,10 @@ You can specify error message that can get the validation scope. ```html - username:
- password:
@@ -270,7 +289,7 @@ new Vue({ ```html
- comment: + comment:
``` @@ -295,7 +314,7 @@ new Vue({ ```html
- address:
+ address:
invalid your email address format.
@@ -308,7 +327,6 @@ new Vue({ # TODO - async validation -- errors properties - validate timing customize with options - local asset registration (`compontents` asset-like) - server-side validation error applying diff --git a/config/karma.conf.js b/config/karma.conf.js index 08ab418..624f8ab 100644 --- a/config/karma.conf.js +++ b/config/karma.conf.js @@ -29,7 +29,7 @@ module.exports = function (config) { devtool: 'source-map', module: { noParse: [ - /node_modules\/sinon\//, + /node_modules\/sinon\// ], loaders: [{ test: /\.js$/, diff --git a/config/webpack.e2e.conf.js b/config/webpack.e2e.conf.js index 4c5cf4c..3022f5d 100644 --- a/config/webpack.e2e.conf.js +++ b/config/webpack.e2e.conf.js @@ -1,17 +1,29 @@ +var webpack = require('webpack') + module.exports = { - entry: { - app: ['webpack/hot/dev-server', './test/e2e/index.js'] - }, + entry: './test/e2e/index.js', output: { path: './test/e2e', - filename: 'index.build.js' + filename: 'e2e.js' }, module: { loaders: [{ test: /\.js$/, - exclude: /node_modules/, - loader: 'babel' + exclude: /node_modules|vue\/dist/, + loader: 'babel', + query: { + presets: ['es2015'] + } }] }, - devtool: 'inline-source-map' + devtool: 'source-map', + devServer: { + contentBase: './test/e2e', + port: 8080, + hot: true, + inline: true + }, + plugins: [ + new webpack.HotModuleReplacementPlugin() + ] } diff --git a/config/webpack.test.conf.js b/config/webpack.test.conf.js index 2017d73..af1e301 100644 --- a/config/webpack.test.conf.js +++ b/config/webpack.test.conf.js @@ -10,7 +10,7 @@ module.exports = { devtool: 'source-map', module: { noParse: [ - /node_modules\/sinon\//, + /node_modules\/sinon\// ], preLoaders: [{ test: /\.js$/, diff --git a/dist/vue-validator.common.js b/dist/vue-validator.common.js index cee31c5..1a58893 100644 --- a/dist/vue-validator.common.js +++ b/dist/vue-validator.common.js @@ -1,5 +1,5 @@ /*! - * vue-validator v2.0.0-alpha.6 + * vue-validator v2.0.0-alpha.7 * (c) 2015 kazuya kawaguchi * Released under the MIT License. */ @@ -334,40 +334,27 @@ var Validation = (function () { this.touched = false; this.dirty = false; this.modified = false; - this.validates = this._buildValidates(dir); + this.validators = Object.create(null); } babelHelpers.createClass(Validation, [{ - key: '_buildValidates', - value: function _buildValidates(dir) { - var arg = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; - + key: 'setValidation', + value: function setValidation(name, arg, msg, fn) { var resolveAsset = exports$1.Vue.util.resolveAsset; - var camelize = exports$1.Vue.util.camelize; - var ret = Object.create(null); - var validates = dir.modifiers; + var validator = this.validators[name]; + if (!validator) { + validator = this.validators[name] = {}; + validator.fn = resolveAsset(this.dir.vm.$options, 'validators', name); + } - for (var validate in validates) { - var fn = resolveAsset(dir.vm.$options, 'validators', camelize(validate)); - if (fn) { - ret[validate] = { arg: arg, fn: fn }; - } + validator.arg = arg; + if (msg) { + validator.msg = msg; } - return ret; - } - }, { - key: 'updateValidate', - value: function updateValidate(name, arg, msg, fn) { - if (this.validates[name]) { - this.validates[name].arg = arg; - if (msg) { - this.validates[name].msg = msg; - } - if (fn) { - this.validates[name].fn = fn; - } + if (fn) { + validator.fn = fn; } } }, { @@ -399,8 +386,8 @@ var Validation = (function () { var messages = Object.create(null); var valid = true; - each(this.validates, function (descriptor, name) { - var res = descriptor.fn(_this.el.value, descriptor.arg); + each(this.validators, function (descriptor, name) { + var res = descriptor.fn.call(_this.dir.vm, _this.el.value, descriptor.arg); if (!res) { valid = false; var msg = descriptor.msg; @@ -467,26 +454,30 @@ function Validate (Vue) { if (_.isPlainObject(value)) { this.handleObject(value); - } else { - this.handleSingle(value); + } else if (Array.isArray(value)) { + this.handleArray(value); } this.validator.validate(this.validation); }, - handleSingle: function handleSingle(value) { - var validateKey = Object.keys(this.validation.validates)[0]; - this.validation.updateValidate(validateKey, value); + handleArray: function handleArray(value) { + var _this = this; + + each(value, function (val) { + _this.validation.setValidation(val); + }, this); }, handleObject: function handleObject(value) { - var _this = this; + var _this2 = this; each(value, function (val, key) { if (_.isPlainObject(val)) { if ('rule' in val) { - _this.validation.updateValidate(key, val.rule, 'message' in val ? val.message : null); + var msg = 'message' in val ? val.message : null; + _this2.validation.setValidation(key, val.rule, msg); } } else { - _this.validation.updateValidate(key, val); + _this2.validation.setValidation(key, val); } }, this); }, @@ -779,7 +770,7 @@ function plugin(Vue) { Validate(Vue); } -plugin.version = '2.0.0-alpha.5'; +plugin.version = '2.0.0-alpha.7'; if (typeof window !== 'undefined' && window.Vue) { window.Vue.use(plugin); diff --git a/dist/vue-validator.js b/dist/vue-validator.js index 4f0e72d..646a454 100644 --- a/dist/vue-validator.js +++ b/dist/vue-validator.js @@ -1,5 +1,5 @@ /*! - * vue-validator v2.0.0-alpha.6 + * vue-validator v2.0.0-alpha.7 * (c) 2015 kazuya kawaguchi * Released under the MIT License. */ @@ -338,40 +338,27 @@ this.touched = false; this.dirty = false; this.modified = false; - this.validates = this._buildValidates(dir); + this.validators = Object.create(null); } babelHelpers.createClass(Validation, [{ - key: '_buildValidates', - value: function _buildValidates(dir) { - var arg = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; - + key: 'setValidation', + value: function setValidation(name, arg, msg, fn) { var resolveAsset = exports$1.Vue.util.resolveAsset; - var camelize = exports$1.Vue.util.camelize; - var ret = Object.create(null); - var validates = dir.modifiers; + var validator = this.validators[name]; + if (!validator) { + validator = this.validators[name] = {}; + validator.fn = resolveAsset(this.dir.vm.$options, 'validators', name); + } - for (var validate in validates) { - var fn = resolveAsset(dir.vm.$options, 'validators', camelize(validate)); - if (fn) { - ret[validate] = { arg: arg, fn: fn }; - } + validator.arg = arg; + if (msg) { + validator.msg = msg; } - return ret; - } - }, { - key: 'updateValidate', - value: function updateValidate(name, arg, msg, fn) { - if (this.validates[name]) { - this.validates[name].arg = arg; - if (msg) { - this.validates[name].msg = msg; - } - if (fn) { - this.validates[name].fn = fn; - } + if (fn) { + validator.fn = fn; } } }, { @@ -403,8 +390,8 @@ var messages = Object.create(null); var valid = true; - each(this.validates, function (descriptor, name) { - var res = descriptor.fn(_this.el.value, descriptor.arg); + each(this.validators, function (descriptor, name) { + var res = descriptor.fn.call(_this.dir.vm, _this.el.value, descriptor.arg); if (!res) { valid = false; var msg = descriptor.msg; @@ -471,26 +458,30 @@ if (_.isPlainObject(value)) { this.handleObject(value); - } else { - this.handleSingle(value); + } else if (Array.isArray(value)) { + this.handleArray(value); } this.validator.validate(this.validation); }, - handleSingle: function handleSingle(value) { - var validateKey = Object.keys(this.validation.validates)[0]; - this.validation.updateValidate(validateKey, value); + handleArray: function handleArray(value) { + var _this = this; + + each(value, function (val) { + _this.validation.setValidation(val); + }, this); }, handleObject: function handleObject(value) { - var _this = this; + var _this2 = this; each(value, function (val, key) { if (_.isPlainObject(val)) { if ('rule' in val) { - _this.validation.updateValidate(key, val.rule, 'message' in val ? val.message : null); + var msg = 'message' in val ? val.message : null; + _this2.validation.setValidation(key, val.rule, msg); } } else { - _this.validation.updateValidate(key, val); + _this2.validation.setValidation(key, val); } }, this); }, @@ -783,7 +774,7 @@ Validate(Vue); } - plugin.version = '2.0.0-alpha.5'; + plugin.version = '2.0.0-alpha.7'; if (typeof window !== 'undefined' && window.Vue) { window.Vue.use(plugin); diff --git a/dist/vue-validator.min.js b/dist/vue-validator.min.js index 7207075..dfd8357 100644 --- a/dist/vue-validator.min.js +++ b/dist/vue-validator.min.js @@ -1,6 +1,6 @@ /*! - * vue-validator v2.0.0-alpha.6 + * vue-validator v2.0.0-alpha.7 * (c) 2015 kazuya kawaguchi * Released under the MIT License. */ -!function(i,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):i.VueValidator=t()}(this,function(){"use strict";function i(i,t){window.console&&(console.warn("[vue-validator] "+i),t&&console.warn(t.stack))}function t(i){if(null===i)return!0;if(Array.isArray(i)){if(i.length>0)return!1;if(0===i.length)return!0}else if(y.Vue.util.isPlainObject(i))for(var t in i)if(y.Vue.util.hasOwn(i,t))return!1;return!0}function e(i,t,e){if(Array.isArray(i))for(var a=0;a0:"number"==typeof i||"function"==typeof i?!0:"boolean"==typeof i?i:"string"==typeof i?i.length>0:null!==i&&"object"===("undefined"==typeof i?"undefined":g["typeof"](i))?Object.keys(i).length>0:null===i||void 0===i?!1:void 0}function r(i,t){if("string"!=typeof t)return!1;var e=t.match(new RegExp("^/(.*?)/([gimy]*)$"));return e?new RegExp(e[1],e[2]).test(i):!1}function o(i,t){return"string"==typeof i&&h(t,10)&&i.length>=parseInt(t,10)}function l(i,t){return"string"==typeof i&&h(t,10)&&i.length<=parseInt(t,10)}function u(i,t){return!isNaN(+i)&&!isNaN(+t)&&+i>=+t}function d(i,t){return!isNaN(+i)&&!isNaN(+t)&&+t>=+i}function h(i){return/^(-?[1-9]\d*|0)$/.test(i)}function v(i){i.config._assetTypes.push("validator"),i.options.validators=_;var t=i.config.optionMergeStrategies;t&&(t.validators=t.methods),i.validator=function(t,e){return e?void(i.options.validators[t]=e):i.options.validators[t]}}function c(i){var t=i.prototype._init;i.prototype._init=function(i){this._validatorMaps||(this._validatorMaps=Object.create(null)),t.call(this,i)};var e=i.prototype._destroy;i.prototype._destroy=function(){e.apply(this,arguments),this._validatorMaps=null}}function f(t){var a=t.util;t.directive("validate",{params:["group"],bind:function(){var t=this.vm,e=t.$options._validator;if(!e)return void i("TODO: should be implemented error message");var n=this.validator=this.vm._validatorMaps[e],s=this.validation=new b(this);n.addValidation(s),this.params.group&&n.addGroupValidation(this.params.group,s),this.on("blur",a.bind(this.validation.listener,this.validation)),this.on("input",a.bind(this.validation.listener,this.validation))},update:function(i,t){i&&(a.isPlainObject(i)?this.handleObject(i):this.handleSingle(i),this.validator.validate(this.validation))},handleSingle:function(i){var t=Object.keys(this.validation.validates)[0];this.validation.updateValidate(t,i)},handleObject:function(i){var t=this;e(i,function(i,e){a.isPlainObject(i)?"rule"in i&&t.validation.updateValidate(e,i.rule,"message"in i?i.message:null):t.validation.updateValidate(e,i)},this)},unbind:function(){this.validator&&this.validation&&(this.params.group&&this.validator.removeGroupValidation(this.params.group,this.validation),this.validator.removeValidation(this.validation),this.validator=null,this.validation=null)}})}function p(t){var e=t.util,a=t.FragmentFactory,n=t.directive("if");t.elementDirective("validator",{params:["name","groups"],bind:function(){if(!this.params.name)return void i("TODO: should be implemented validator:bind name params nothing error");var t=this.validatorName="$"+this.params.name;if(!this.vm._validatorMaps)return void i("TODO: should be implemented error message");var a=[];this.params.groups&&(e.isArray(this.params.groups)?a=this.params.groups:e.isPlainObject(this.params.groups)||"string"!=typeof this.params.groups||a.push(this.params.groups));var n=this.validator=new V(t,this,a);n.enableReactive(),n.setupScope(),this.anchor=e.createAnchor("vue-validator"),e.replace(this.el,this.anchor),this.insert(t),this.vm.$on("hook:compiled",function(){n.validate()})},insert:function(i){e.extend(this.vm.$options,{_validator:i}),this.factory=new a(this.vm,this.el.innerHTML),n.insert.call(this)},unbind:function(){n.unbind.call(this),this.validator.disableReactive(),this.validatorName&&(this.validatorName=null,this.validator=null)}})}function m(t){arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return m.installed?void i("already installed."):(y.Vue=t,v(t),c(t),p(t),void f(t))}var g={};g["typeof"]=function(i){return i&&"undefined"!=typeof Symbol&&i.constructor===Symbol?"symbol":typeof i},g.classCallCheck=function(i,t){if(!(i instanceof t))throw new TypeError("Cannot call a class as a function")},g.createClass=function(){function i(i,t){for(var e=0;e0)return!1;if(0===i.length)return!0}else if(g.Vue.util.isPlainObject(i))for(var t in i)if(g.Vue.util.hasOwn(i,t))return!1;return!0}function e(i,t,e){if(Array.isArray(i))for(var a=0;a0:"number"==typeof i||"function"==typeof i?!0:"boolean"==typeof i?i:"string"==typeof i?i.length>0:null!==i&&"object"===("undefined"==typeof i?"undefined":y["typeof"](i))?Object.keys(i).length>0:null===i||void 0===i?!1:void 0}function s(i,t){if("string"!=typeof t)return!1;var e=t.match(new RegExp("^/(.*?)/([gimy]*)$"));return e?new RegExp(e[1],e[2]).test(i):!1}function o(i,t){return"string"==typeof i&&h(t,10)&&i.length>=parseInt(t,10)}function l(i,t){return"string"==typeof i&&h(t,10)&&i.length<=parseInt(t,10)}function u(i,t){return!isNaN(+i)&&!isNaN(+t)&&+i>=+t}function d(i,t){return!isNaN(+i)&&!isNaN(+t)&&+t>=+i}function h(i){return/^(-?[1-9]\d*|0)$/.test(i)}function c(i){i.config._assetTypes.push("validator"),i.options.validators=_;var t=i.config.optionMergeStrategies;t&&(t.validators=t.methods),i.validator=function(t,e){return e?void(i.options.validators[t]=e):i.options.validators[t]}}function v(i){var t=i.prototype._init;i.prototype._init=function(i){this._validatorMaps||(this._validatorMaps=Object.create(null)),t.call(this,i)};var e=i.prototype._destroy;i.prototype._destroy=function(){e.apply(this,arguments),this._validatorMaps=null}}function f(t){var a=t.util;t.directive("validate",{params:["group"],bind:function(){var t=this.vm,e=t.$options._validator;if(!e)return void i("TODO: should be implemented error message");var n=this.validator=this.vm._validatorMaps[e],r=this.validation=new b(this);n.addValidation(r),this.params.group&&n.addGroupValidation(this.params.group,r),this.on("blur",a.bind(this.validation.listener,this.validation)),this.on("input",a.bind(this.validation.listener,this.validation))},update:function(i,t){i&&(a.isPlainObject(i)?this.handleObject(i):Array.isArray(i)&&this.handleArray(i),this.validator.validate(this.validation))},handleArray:function(i){var t=this;e(i,function(i){t.validation.setValidation(i)},this)},handleObject:function(i){var t=this;e(i,function(i,e){if(a.isPlainObject(i)){if("rule"in i){var n="message"in i?i.message:null;t.validation.setValidation(e,i.rule,n)}}else t.validation.setValidation(e,i)},this)},unbind:function(){this.validator&&this.validation&&(this.params.group&&this.validator.removeGroupValidation(this.params.group,this.validation),this.validator.removeValidation(this.validation),this.validator=null,this.validation=null)}})}function p(t){var e=t.util,a=t.FragmentFactory,n=t.directive("if");t.elementDirective("validator",{params:["name","groups"],bind:function(){if(!this.params.name)return void i("TODO: should be implemented validator:bind name params nothing error");var t=this.validatorName="$"+this.params.name;if(!this.vm._validatorMaps)return void i("TODO: should be implemented error message");var a=[];this.params.groups&&(e.isArray(this.params.groups)?a=this.params.groups:e.isPlainObject(this.params.groups)||"string"!=typeof this.params.groups||a.push(this.params.groups));var n=this.validator=new V(t,this,a);n.enableReactive(),n.setupScope(),this.anchor=e.createAnchor("vue-validator"),e.replace(this.el,this.anchor),this.insert(t),this.vm.$on("hook:compiled",function(){n.validate()})},insert:function(i){e.extend(this.vm.$options,{_validator:i}),this.factory=new a(this.vm,this.el.innerHTML),n.insert.call(this)},unbind:function(){n.unbind.call(this),this.validator.disableReactive(),this.validatorName&&(this.validatorName=null,this.validator=null)}})}function m(t){arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return m.installed?void i("already installed."):(g.Vue=t,c(t),v(t),p(t),void f(t))}var y={};y["typeof"]=function(i){return i&&"undefined"!=typeof Symbol&&i.constructor===Symbol?"symbol":typeof i},y.classCallCheck=function(i,t){if(!(i instanceof t))throw new TypeError("Cannot call a class as a function")},y.createClass=function(){function i(i,t){for(var e=0;e { + this.validation.setValidation(val) + }, this) }, handleObject (value) { each(value, (val, key) => { if (_.isPlainObject(val)) { if ('rule' in val) { - this.validation.updateValidate(key, val.rule, ('message' in val ? val.message : null)) + let msg = 'message' in val ? val.message : null + this.validation.setValidation(key, val.rule, msg) } } else { - this.validation.updateValidate(key, val) + this.validation.setValidation(key, val) } }, this) }, diff --git a/src/index.js b/src/index.js index 8834c66..ec20ca9 100644 --- a/src/index.js +++ b/src/index.js @@ -26,7 +26,7 @@ function plugin (Vue, options = {}) { Validate(Vue) } -plugin.version = '2.0.0-alpha.5' +plugin.version = '2.0.0-alpha.7' export default plugin diff --git a/src/validation.js b/src/validation.js index 147b69b..8ba1a06 100644 --- a/src/validation.js +++ b/src/validation.js @@ -15,35 +15,25 @@ export default class Validation { this.touched = false this.dirty = false this.modified = false - this.validates = this._buildValidates(dir) + this.validators = Object.create(null) } - _buildValidates (dir, arg = null) { + setValidation (name, arg, msg, fn) { const resolveAsset = util.Vue.util.resolveAsset - const camelize = util.Vue.util.camelize - let ret = Object.create(null) - let validates = dir.modifiers - - for (let validate in validates) { - let fn = resolveAsset(dir.vm.$options, 'validators', camelize(validate)) - if (fn) { - ret[validate] = { arg: arg, fn: fn } - } + let validator = this.validators[name] + if (!validator) { + validator = this.validators[name] = {} + validator.fn = resolveAsset(this.dir.vm.$options, 'validators', name) + } + + validator.arg = arg + if (msg) { + validator.msg = msg } - return ret - } - - updateValidate (name, arg, msg, fn) { - if (this.validates[name]) { - this.validates[name].arg = arg - if (msg) { - this.validates[name].msg = msg - } - if (fn) { - this.validates[name].fn = fn - } + if (fn) { + validator.fn = fn } } @@ -72,8 +62,8 @@ export default class Validation { let messages = Object.create(null) let valid = true - each(this.validates, (descriptor, name) => { - let res = descriptor.fn(this.el.value, descriptor.arg) + each(this.validators, (descriptor, name) => { + let res = descriptor.fn.call(this.dir.vm, this.el.value, descriptor.arg) if (!res) { valid = false let msg = descriptor.msg diff --git a/test/e2e/.babelrc b/test/e2e/.babelrc new file mode 100644 index 0000000..6f20dd0 --- /dev/null +++ b/test/e2e/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": ["es2015"], + "plugins": ["transform-es2015-modules-commonjs"] +} diff --git a/test/e2e/helper.js b/test/e2e/helper.js deleted file mode 100644 index a44907e..0000000 --- a/test/e2e/helper.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Import(s) - */ - -var pathResolve = require('path').resolve; -var urlFormat = require('url').format; - - -/** - * Exports(s) - */ - -module.exports = { - resolve: resolve -} - - -function resolve (path) { - return urlFormat({ - protocol: 'file', - slashes: true, - pathname: pathResolve(__dirname, path) - }) -} diff --git a/test/e2e/index.html b/test/e2e/index.html new file mode 100644 index 0000000..4e9861a --- /dev/null +++ b/test/e2e/index.html @@ -0,0 +1,113 @@ + + + + registration + + + + + + + +
+ + +
+ +
+ + + + {{ $validation.username.valid ? '(success)' : '(error)' }} + +
+
+
+ +
+ + + + {{ $validation.email.valid ? '(success)' : '(error)' }} + +
+
+
+ +
+ + + + {{ $validation.confirmEmail.valid ? '(success)' : '(error)' }} + +
+
+
+ +
+ + + + {{ $validation.password.valid ? '(success)' : '(error)' }} + +
+
+
+
+ +
+
+ +
+
+ + + diff --git a/test/e2e/index.js b/test/e2e/index.js new file mode 100644 index 0000000..c870173 --- /dev/null +++ b/test/e2e/index.js @@ -0,0 +1,22 @@ +import Vue from 'vue' +import plugin from '../../src/index' + +Vue.use(plugin) + +// register custom validator +Vue.validator('email', (val) => { + return /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(val) +}) + +Vue.validator('same', (val, prop) => { + return val == this.$get(prop) +}) + +new Vue({ + data: { + username: '', + email: '', + confirmEmail: '', + password: '' + } +}).$mount('#registration') diff --git a/test/e2e/registration.html b/test/e2e/registration.html deleted file mode 100644 index 5cafd53..0000000 --- a/test/e2e/registration.html +++ /dev/null @@ -1,111 +0,0 @@ - - - - registration - - - - - - - - - -
-
-
- -
- - - -
-
-
- -
- - - -
-
-
- -
- - - -
-
-
- -
- - - -
-
-
-
- -
-
-
-
- - - diff --git a/test/e2e/registration.js b/test/e2e/test.js similarity index 86% rename from test/e2e/registration.js rename to test/e2e/test.js index e3afa58..c20524d 100644 --- a/test/e2e/registration.js +++ b/test/e2e/test.js @@ -1,46 +1,38 @@ -/** - * Import(s) - */ +import assert from 'power-assert' +import Nightmare from 'nightmare' +const url = 'http://localhost:' + (process.env.PORT || 8080) -var Nightmare = require('nightmare') -var assert = require('power-assert') -var resolve = require('./helper').resolve +describe('registration', () => { -/** - * Test(s) - */ - -describe('registration', function () { - - var expectOK = function (ret) { + let expectOK = (ret) => { assert(ret) } - var expectTextError = function (text) { + let expectTextError = (text) => { assert(text === '(error)') } - var expectTextSuccess = function (text) { + let expectTextSuccess = (text) => { assert(text === '(success)') } - var getUsernameStatus = function () { + let getUsernameStatus = () => { return document.querySelector('#inputUsernameStatus').innerText } - var getEmailStatus = function () { + let getEmailStatus = () => { return document.querySelector('#inputEmailStatus').innerText } - var getConfirmEmailStatus = function () { + let getConfirmEmailStatus = () => { return document.querySelector('#inputConfirmEmailStatus').innerText } - var getPasswordStatus = function () { + let getPasswordStatus = () => { return document.querySelector('#inputPasswordStatus').innerText } describe('first loaded page', function () { this.timeout(20000) - it('should be invalid', function (done) { + it('should be invalid', (done) => { new Nightmare() - .goto(resolve('./registration.html')) + .goto(url) .exists('#username .has-error', expectOK) .exists('#inputUsernameIcon .glyphicon-remove', expectOK) .evaluate(getUsernameStatus, expectTextError) @@ -62,9 +54,9 @@ describe('registration', function () { describe('input valid username', function () { this.timeout(20000) - it('should be valid', function (done) { + it('should be valid', (done) => { new Nightmare() - .goto(resolve('./registration.html')) + .goto(url) .type('#inputUsername', 'kazupon') .wait('#inputUsername') .exists('#username .has-success', expectOK) // success @@ -88,9 +80,9 @@ describe('registration', function () { describe('input valid email', function () { this.timeout(20000) - it('should be valid', function (done) { + it('should be valid', (done) => { new Nightmare() - .goto(resolve('./registration.html')) + .goto(url) .type('#inputEmail', 'foo@domain.com') .wait('#inputEmail') .exists('#username .has-error', expectOK) @@ -114,9 +106,9 @@ describe('registration', function () { describe('input valid confirm email', function () { this.timeout(20000) - it('should be valid', function (done) { + it('should be valid', (done) => { new Nightmare() - .goto(resolve('./registration.html')) + .goto(url) .type('#inputEmail', 'foo@domain.com') .wait('#inputEmail') .type('#inputConfirmEmail', 'foo@domain.com') @@ -142,7 +134,7 @@ describe('registration', function () { describe('input valid password', function () { this.timeout(20000) - it('should be valid', function (done) { + it('should be valid', (done) => { new Nightmare() .goto(resolve('./registration.html')) .type('#inputPassword', 'xxxxxxxxx') @@ -168,9 +160,9 @@ describe('registration', function () { describe('input valid fileds', function () { this.timeout(20000) - it('join button should be enabled', function (done) { + it('join button should be enabled', (done) => { new Nightmare() - .goto(resolve('./registration.html')) + .goto(url) .type('#inputUsername', 'kazupon') .wait('#inputUsername') .type('#inputEmail', 'foo@domain.com') diff --git a/test/specs/custom.js b/test/specs/custom.js index 60a63e0..04565df 100644 --- a/test/specs/custom.js +++ b/test/specs/custom.js @@ -20,7 +20,7 @@ describe('custom', () => { el.innerHTML = '' + '
' + - '' + + '' + '
' + '
' vm = new Vue({ diff --git a/test/specs/directives/validate.js b/test/specs/directives/validate.js index 61b4c1b..f9d9678 100644 --- a/test/specs/directives/validate.js +++ b/test/specs/directives/validate.js @@ -16,7 +16,7 @@ describe('validate directive', () => { el: el, template: '' + '
' + - '' + + '' + '
' + '
' }) @@ -46,7 +46,7 @@ describe('validate directive', () => { }, template: '' + '
' + - '' + + '' + '
' + '
' }) @@ -123,7 +123,7 @@ describe('validate directive', () => { }, template: '' + '
' + - '' + + '' + '
' + '
' }) diff --git a/test/specs/dirty.js b/test/specs/dirty.js index a1a96f8..a5e6bfa 100644 --- a/test/specs/dirty.js +++ b/test/specs/dirty.js @@ -11,8 +11,8 @@ describe('dirty', () => { el.innerHTML = '' + '
' + - '' + - '' + + '' + + '' + '
' + '
' vm = new Vue({ diff --git a/test/specs/event.js b/test/specs/event.js index ba76654..fad2e23 100644 --- a/test/specs/event.js +++ b/test/specs/event.js @@ -15,7 +15,7 @@ describe('event', () => { it('should be occured event', (done) => { el.innerHTML = '' + - '' + + '' + '' vm = new Vue({ el: el, @@ -38,7 +38,7 @@ describe('event', () => { it('should be occured event', (done) => { el.innerHTML = '' + - '' + + '' + '' vm = new Vue({ el: el, diff --git a/test/specs/group.js b/test/specs/group.js index bdd6a92..39e5b06 100644 --- a/test/specs/group.js +++ b/test/specs/group.js @@ -9,11 +9,11 @@ describe('group', () => { beforeEach((done) => { el = document.createElement('div') el.innerHTML = - '' + - '' + - '' + - '' + - '' + + '' + + '' + + '' + + '' + + '' + '' vm = new Vue({ el: el diff --git a/test/specs/invalid.js b/test/specs/invalid.js index 7f45f4d..e9f5b28 100644 --- a/test/specs/invalid.js +++ b/test/specs/invalid.js @@ -11,8 +11,8 @@ describe('invalid', () => { el.innerHTML = '' + '
' + - '' + - '' + + '' + + '' + '
' + '
' vm = new Vue({ diff --git a/test/specs/messages.js b/test/specs/messages.js index 46247fb..44b6f60 100644 --- a/test/specs/messages.js +++ b/test/specs/messages.js @@ -9,13 +9,13 @@ describe('messages', () => { beforeEach((done) => { el = document.createElement('div') el.innerHTML = - '' + - '' + - '' + - '' + - '' + - '' + - '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + '
  • ' + '

    {{$key}}:{{val}}

    ' + '
' + diff --git a/test/specs/modified.js b/test/specs/modified.js index 65114f8..fc2b788 100644 --- a/test/specs/modified.js +++ b/test/specs/modified.js @@ -11,8 +11,8 @@ describe('modified', () => { el.innerHTML = '' + '
' + - '' + - '' + + '' + + '' + '
' + '
' vm = new Vue({ diff --git a/test/specs/multiple.js b/test/specs/multiple.js index 22ec381..928497a 100644 --- a/test/specs/multiple.js +++ b/test/specs/multiple.js @@ -10,10 +10,10 @@ describe('multiple', () => { el = document.createElement('div') el.innerHTML = '' + - '' + + '' + '' + '' + - '' + + '' + '' vm = new Vue({ el: el diff --git a/test/specs/pristine.js b/test/specs/pristine.js index fcb89bd..27b197b 100644 --- a/test/specs/pristine.js +++ b/test/specs/pristine.js @@ -11,8 +11,8 @@ describe('pristine', () => { el.innerHTML = '' + '
' + - '' + - '' + + '' + + '' + '
' + '
' vm = new Vue({ diff --git a/test/specs/syntax.js b/test/specs/syntax.js index 9062f0b..5578422 100644 --- a/test/specs/syntax.js +++ b/test/specs/syntax.js @@ -11,200 +11,187 @@ describe('syntax', () => { }) - describe('simple', () => { - context('static', () => { + context('simple', () => { + beforeEach((done) => { + el.innerHTML = + '' + + '
' + + '' + + '
' + + '
' + vm = new Vue({ + el: el + }) + vm.$nextTick(done) + }) + + it('should be validated', (done) => { + // default + assert(vm.$validator1.field1.minlength === true) + + // change input value + let input = el.getElementsByTagName('input')[0] + input.value = 'foo' + trigger(input, 'input') + vm.$nextTick(() => { + assert(vm.$validator1.field1.minlength === false) + done() + }) + }) + }) + + + context('array', () => { + beforeEach((done) => { + el.innerHTML = + '' + + '
' + + '' + + '
' + + '
' + vm = new Vue({ + el: el + }) + vm.$nextTick(done) + }) + + it('should be validated', (done) => { + // default + assert(vm.$validator1.field1.required === true) + + // change input value + let input = el.getElementsByTagName('input')[0] + input.value = 'foo' + trigger(input, 'input') + vm.$nextTick(() => { + assert(vm.$validator1.field1.required === false) + done() + }) + }) + }) + + + context('strict', () => { + beforeEach((done) => { + el.innerHTML = + '' + + '
' + + '' + + '
' + + '
' + vm = new Vue({ + el: el + }) + vm.$nextTick(done) + }) + + it('should be validated', (done) => { + // default + assert(vm.$validator1.field1.minlength === false) + assert(vm.$validator1.field1.maxlength === false) + + // change input value + let input = el.getElementsByTagName('input')[0] + input.value = 'h' + trigger(input, 'input') + vm.$nextTick(() => { + assert(vm.$validator1.field1.minlength === true) + assert(vm.$validator1.field1.maxlength === false) + + input.value = 'hi kazupon' + trigger(input, 'input') + vm.$nextTick(() => { + assert(vm.$validator1.field1.minlength === false) + assert(vm.$validator1.field1.maxlength === true) + done() + }) + }) + }) + }) + + + context('binding', () => { + context('primitive', () => { beforeEach((done) => { el.innerHTML = '' + '
' + - '' + + '' + '
' + '
' vm = new Vue({ - el: el + el: el, + data: { number: 10 } }) vm.$nextTick(done) }) it('should be validated', (done) => { // default - assert(vm.$validator1.field1.minlength === true) + assert(vm.$validator1.field1.max === false) // change input value let input = el.getElementsByTagName('input')[0] - input.value = 'foo' + input.value = '11' trigger(input, 'input') vm.$nextTick(() => { - assert(vm.$validator1.field1.minlength === false) + assert(vm.$validator1.field1.max === true) done() }) }) }) - context('binding', () => { - context('primitive', () => { - beforeEach((done) => { - el.innerHTML = - '' + - '
' + - '' + - '
' + - '
' - vm = new Vue({ - el: el, - data: { number: 10 } - }) - vm.$nextTick(done) - }) - - it('should be validated', (done) => { - // default - assert(vm.$validator1.field1.max === false) - - // change input value - let input = el.getElementsByTagName('input')[0] - input.value = '11' - trigger(input, 'input') - vm.$nextTick(() => { - assert(vm.$validator1.field1.max === true) - done() - }) - }) - }) - - context('function', () => { - beforeEach((done) => { - el.innerHTML = - '' + - '
' + - '' + - '
' + - '
' - vm = new Vue({ - el: el, - data: { condition: '' }, - methods: { - getMax (condition) { - let ret = 0 - switch (condition) { - case 'condition1': - ret = 5 - break - default: - ret = 10 - break - } - return ret + context('function', () => { + beforeEach((done) => { + el.innerHTML = + '' + + '
' + + '' + + '
' + + '
' + vm = new Vue({ + el: el, + data: { condition: '' }, + methods: { + getMax (condition) { + let ret = 0 + switch (condition) { + case 'condition1': + ret = 5 + break + default: + ret = 10 + break } + return ret } - }) - vm.$nextTick(done) - }) - - it('should be validated', (done) => { - // default - assert(vm.$validator1.field1.max === false) - - // change input value - let input = el.getElementsByTagName('input')[0] - vm.condition = 'condition1' - input.value = '11' - trigger(input, 'input') - vm.$nextTick(() => { - assert(vm.$validator1.field1.max === true) - done() - }) + } }) + vm.$nextTick(done) }) - }) - }) - - - describe('object', () => { - context('static', () => { - context('loose', () => { - beforeEach((done) => { - el.innerHTML = - '' + - '
' + - '' + - '
' + - '
' - vm = new Vue({ - el: el - }) - vm.$nextTick(done) - }) - it('should be validated', (done) => { - // default - assert(vm.$validator1.field1.min === false) - assert(vm.$validator1.field1.max === false) - - // change input value - let input = el.getElementsByTagName('input')[0] - input.value = '0' - trigger(input, 'input') - vm.$nextTick(() => { - assert(vm.$validator1.field1.min === true) - assert(vm.$validator1.field1.max === false) - - input.value = '6' - trigger(input, 'input') - vm.$nextTick(() => { - assert(vm.$validator1.field1.min === false) - assert(vm.$validator1.field1.max === true) - done() - }) - }) - }) - }) + it('should be validated', (done) => { + // default + assert(vm.$validator1.field1.max === false) - context('strict', () => { - beforeEach((done) => { - el.innerHTML = - '' + - '
' + - '' + - '
' + - '
' - vm = new Vue({ - el: el - }) - vm.$nextTick(done) - }) - - it('should be validated', (done) => { - // default - assert(vm.$validator1.field1.minlength === false) - assert(vm.$validator1.field1.maxlength === false) - - // change input value - let input = el.getElementsByTagName('input')[0] - input.value = 'h' - trigger(input, 'input') - vm.$nextTick(() => { - assert(vm.$validator1.field1.minlength === true) - assert(vm.$validator1.field1.maxlength === false) - - input.value = 'hi kazupon' - trigger(input, 'input') - vm.$nextTick(() => { - assert(vm.$validator1.field1.minlength === false) - assert(vm.$validator1.field1.maxlength === true) - done() - }) - }) + // change input value + let input = el.getElementsByTagName('input')[0] + vm.condition = 'condition1' + input.value = '11' + trigger(input, 'input') + vm.$nextTick(() => { + assert(vm.$validator1.field1.max === true) + done() }) }) }) - context('binding', () => { + context('object', () => { beforeEach((done) => { el.innerHTML = '' + '
' + - '' + + '' + '
' + '
' vm = new Vue({ diff --git a/test/specs/touched.js b/test/specs/touched.js index f90461b..c7f03cf 100644 --- a/test/specs/touched.js +++ b/test/specs/touched.js @@ -11,8 +11,8 @@ describe('touched', () => { el.innerHTML = '' + '
' + - '' + - '' + + '' + + '' + '
' + '
' vm = new Vue({ diff --git a/test/specs/untouched.js b/test/specs/untouched.js index a0ea789..646ca55 100644 --- a/test/specs/untouched.js +++ b/test/specs/untouched.js @@ -11,8 +11,8 @@ describe('ununtouched', () => { el.innerHTML = '' + '
' + - '' + - '' + + '' + + '' + '
' + '
' vm = new Vue({ diff --git a/test/specs/valid.js b/test/specs/valid.js index af039be..afc255e 100644 --- a/test/specs/valid.js +++ b/test/specs/valid.js @@ -11,8 +11,8 @@ describe('valid', () => { el.innerHTML = '' + '
' + - '' + - '' + + '' + + '' + '
' + '
' vm = new Vue({