Skip to content

Commit

Permalink
add vee-validate integration + change exported API
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorLuizC committed Aug 21, 2017
1 parent 7b75257 commit e1af172
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 13 deletions.
70 changes: 66 additions & 4 deletions dist/vue-convenia-util.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ var isEmail = function (value) {
};


var $validate = Object.freeze({
var validate = Object.freeze({
is: is,
isCPF: isCPF,
isDate: isDate,
Expand Down Expand Up @@ -383,6 +383,41 @@ var $format = Object.freeze({
toCEP: toCEP
});

/**
* Integra automaticamente as funções de validação ao vee-validade.
* @param {vee-validate.Validator} Validator
* @param {Object.<String, { name: String, getMessage: Function }>} options
*/
var VeeValidateIntegration = function (Validator, options) {
var defaultOptions = {
isCPF: {
name: 'cpf',
getMessage: function () { return 'CPF inválido.'; }
},
isCNPJ: {
name: 'cnpj',
getMessage: function () { return 'CNPJ inválido.'; }
},
isDate: {
name: 'date',
getMessage: function () { return 'Data inválida.'; }
}
};

var rules = Object.assign({}, defaultOptions, options);

Object.keys(rules)
.map(function (key) { return Object.assign({}, rules[key], { validate: validate[key] }); })
.filter(function (rule) { return is(rules, 'Object'); })
.forEach(function (rule) { return Validator.extend(rule.name, rule); });

return true
};

var integrations = {
'vee-validate': VeeValidateIntegration
};

/**
* Opções do plugin.
* @typedef {Object} Options
Expand Down Expand Up @@ -412,10 +447,37 @@ var install = function (Vue, options) {
}

if (options.validators) {
Vue.prototype.$validate = $validate;
Vue.prototype.$validate = validate;
}
};

/**
* Integra-se a lib definida usando o object/função de integração e as opções da
* integração.
* @example ```
* import { Validator } from 'vee-validate'
* import Util from 'vue-convenia-util'
*
* Util.integrate('vee-validate', Validator)
* ```
* @param {String} lib
* @param {(Object|Function)} integrator
* @param {Object} options
* @returns {Boolean}
*/
var integrate = function (lib, integrator, options) {
if ( options === void 0 ) options = {};

var integration = integrations.hasOwnProperty(lib) ? integrations[lib] : null;
var success = integration ? integration(integrator, options) : false;
return success
};

var index = {
install: install,
integrate: integrate
};

exports.format = $format;
exports.validate = $validate;
exports['default'] = install;
exports.validate = validate;
exports['default'] = index;
68 changes: 65 additions & 3 deletions dist/vue-convenia-util.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var isEmail = function (value) {
};


var $validate = Object.freeze({
var validate = Object.freeze({
is: is,
isCPF: isCPF,
isDate: isDate,
Expand Down Expand Up @@ -377,6 +377,41 @@ var $format = Object.freeze({
toCEP: toCEP
});

/**
* Integra automaticamente as funções de validação ao vee-validade.
* @param {vee-validate.Validator} Validator
* @param {Object.<String, { name: String, getMessage: Function }>} options
*/
var VeeValidateIntegration = function (Validator, options) {
var defaultOptions = {
isCPF: {
name: 'cpf',
getMessage: function () { return 'CPF inválido.'; }
},
isCNPJ: {
name: 'cnpj',
getMessage: function () { return 'CNPJ inválido.'; }
},
isDate: {
name: 'date',
getMessage: function () { return 'Data inválida.'; }
}
};

var rules = Object.assign({}, defaultOptions, options);

Object.keys(rules)
.map(function (key) { return Object.assign({}, rules[key], { validate: validate[key] }); })
.filter(function (rule) { return is(rules, 'Object'); })
.forEach(function (rule) { return Validator.extend(rule.name, rule); });

return true
};

var integrations = {
'vee-validate': VeeValidateIntegration
};

/**
* Opções do plugin.
* @typedef {Object} Options
Expand Down Expand Up @@ -406,8 +441,35 @@ var install = function (Vue, options) {
}

if (options.validators) {
Vue.prototype.$validate = $validate;
Vue.prototype.$validate = validate;
}
};

export { $format as format, $validate as validate };export default install;
/**
* Integra-se a lib definida usando o object/função de integração e as opções da
* integração.
* @example ```
* import { Validator } from 'vee-validate'
* import Util from 'vue-convenia-util'
*
* Util.integrate('vee-validate', Validator)
* ```
* @param {String} lib
* @param {(Object|Function)} integrator
* @param {Object} options
* @returns {Boolean}
*/
var integrate = function (lib, integrator, options) {
if ( options === void 0 ) options = {};

var integration = integrations.hasOwnProperty(lib) ? integrations[lib] : null;
var success = integration ? integration(integrator, options) : false;
return success
};

var index = {
install: install,
integrate: integrate
};

export { $format as format, validate };export default index;
26 changes: 25 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as $format from './formatters'
import * as $validate from './validators'
import integrations from './integrations'

export { $format as format }

Expand Down Expand Up @@ -36,4 +37,27 @@ const install = (Vue, options = {}) => {
}
}

export default install
/**
* Integra-se a lib definida usando o object/função de integração e as opções da
* integração.
* @example ```
* import { Validator } from 'vee-validate'
* import Util from 'vue-convenia-util'
*
* Util.integrate('vee-validate', Validator)
* ```
* @param {String} lib
* @param {(Object|Function)} integrator
* @param {Object} options
* @returns {Boolean}
*/
const integrate = (lib, integrator, options = {}) => {
const integration = integrations.hasOwnProperty(lib) ? integrations[lib] : null
const success = integration ? integration(integrator, options) : false
return success
}

export default {
install,
integrate
}
36 changes: 36 additions & 0 deletions src/integrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as validate from './validators'

/**
* Integra automaticamente as funções de validação ao vee-validade.
* @param {vee-validate.Validator} Validator
* @param {Object.<String, { name: String, getMessage: Function }>} options
*/
const VeeValidateIntegration = (Validator, options) => {
const defaultOptions = {
isCPF: {
name: 'cpf',
getMessage: () => 'CPF inválido.'
},
isCNPJ: {
name: 'cnpj',
getMessage: () => 'CNPJ inválido.'
},
isDate: {
name: 'date',
getMessage: () => 'Data inválida.'
}
}

const rules = Object.assign({}, defaultOptions, options)

Object.keys(rules)
.map(key => Object.assign({}, rules[key], { validate: validate[key] }))
.filter(rule => validate.is(rules, 'Object'))
.forEach(rule => Validator.extend(rule.name, rule))

return true
}

export default {
'vee-validate': VeeValidateIntegration
}
34 changes: 29 additions & 5 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava'
import sinon from 'sinon'
import install, { format, validate } from '../'
import Util, { format, validate } from '../'

const getVue = () => {
const Vue = class {
Expand All @@ -15,8 +15,8 @@ const getVue = () => {
return Vue
}

test('A exportação padrão é uma função', (context) => {
context.is(typeof install, 'function')
test('A exportação padrão é um objeto', (context) => {
context.is(typeof Util, 'object')
})

test('Exporta o namespace "format"', (context) => {
Expand All @@ -34,7 +34,7 @@ test('Implanta os plugins selecionados', (context) => {
const spy = sinon.spy(Vue, 'filter')
const vm = new Vue()

install(Vue, {
Util.install(Vue, {
formatters: true,
formatFilters: true,
validators: true
Expand All @@ -50,7 +50,7 @@ test('Implanta apenas os plugins selecionados', (context) => {
const spy = sinon.spy(Vue, 'filter')
const vm = new Vue()

install(Vue, {
Util.install(Vue, {
formatters: false,
formatFilters: false,
validators: false
Expand All @@ -60,3 +60,27 @@ test('Implanta apenas os plugins selecionados', (context) => {
context.is(!!vm.$format, false)
context.is(!!vm.$validate, false)
})

test('integrate: Integra os validadores ao "vee-validate"', context => {
const Validator = {
validations: {},
extend (name, handler) {
this.validations[name] = handler
}
}

const spy = sinon.spy(Validator, 'extend')

Util.integrate('vee-validate', Validator, {
isEmail: {
name: 'email',
getMessage: () => 'Email inválido.'
}
})

context.is(spy.called, true)
context.is(Validator.validations.cpf.validate === validate.isCPF, true)
context.is(Validator.validations.cnpj.validate === validate.isCNPJ, true)
context.is(Validator.validations.date.validate === validate.isDate, true)
context.is(Validator.validations.email.validate === validate.isEmail, true)
})

0 comments on commit e1af172

Please sign in to comment.