1
0
mirror of synced 2024-11-22 13:26:06 +03:00

refactor!: rules & messages Formulario options renamed to validationRules & validationMessages

This commit is contained in:
Zaytsev Kirill 2020-10-26 17:03:24 +03:00
parent 1caff9c089
commit 4251d46591
3 changed files with 16 additions and 16 deletions

View File

@ -1,8 +1,8 @@
import { VueConstructor } from 'vue' import { VueConstructor } from 'vue'
import rules from '@/validation/rules'
import messages from '@/validation/messages'
import merge from '@/utils/merge' import merge from '@/utils/merge'
import validationRules from '@/validation/rules'
import validationMessages from '@/validation/messages'
import FormularioForm from '@/FormularioForm.vue' import FormularioForm from '@/FormularioForm.vue'
import FormularioGrouping from '@/FormularioGrouping.vue' import FormularioGrouping from '@/FormularioGrouping.vue'
@ -15,8 +15,8 @@ import {
} from '@/validation/validator' } from '@/validation/validator'
interface FormularioOptions { interface FormularioOptions {
rules?: any; validationRules?: any;
messages?: Record<string, Function>; validationMessages?: Record<string, Function>;
} }
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
@ -24,12 +24,12 @@ interface FormularioOptions {
* The base formulario library. * The base formulario library.
*/ */
export default class Formulario { export default class Formulario {
public rules: Record<string, CheckRuleFn> = {} public validationRules: Record<string, CheckRuleFn> = {}
public messages: Record<string, Function> = {} public validationMessages: Record<string, Function> = {}
constructor () { constructor () {
this.rules = rules this.validationRules = validationRules
this.messages = messages this.validationMessages = validationMessages
} }
/** /**
@ -49,8 +49,8 @@ export default class Formulario {
*/ */
extend (extendWith: FormularioOptions): Formulario { extend (extendWith: FormularioOptions): Formulario {
if (typeof extendWith === 'object') { if (typeof extendWith === 'object') {
this.rules = merge(this.rules, extendWith.rules || {}) this.validationRules = merge(this.validationRules, extendWith.validationRules || {})
this.messages = merge(this.messages, extendWith.messages || {}) this.validationMessages = merge(this.validationMessages, extendWith.validationMessages || {})
return this return this
} }
throw new Error(`[Formulario]: Formulario.extend() should be passed an object (was ${typeof extendWith})`) throw new Error(`[Formulario]: Formulario.extend() should be passed an object (was ${typeof extendWith})`)
@ -60,14 +60,14 @@ export default class Formulario {
* Get validation rules by merging any passed in with global rules. * Get validation rules by merging any passed in with global rules.
*/ */
getRules (extendWith: Record<string, CheckRuleFn> = {}): Record<string, CheckRuleFn> { getRules (extendWith: Record<string, CheckRuleFn> = {}): Record<string, CheckRuleFn> {
return merge(this.rules, extendWith) return merge(this.validationRules, extendWith)
} }
/** /**
* Get validation messages by merging any passed in with global messages. * Get validation messages by merging any passed in with global messages.
*/ */
getMessages (vm: Vue, extendWith: Record<string, Function>): Record<string, CreateMessageFn> { getMessages (vm: Vue, extendWith: Record<string, Function>): Record<string, CreateMessageFn> {
const raw = merge(this.messages || {}, extendWith) const raw = merge(this.validationMessages || {}, extendWith)
const messages: Record<string, CreateMessageFn> = {} const messages: Record<string, CreateMessageFn> = {}
for (const name in raw) { for (const name in raw) {

View File

@ -5,7 +5,7 @@ import Formulario from '@/index.ts'
import FormularioForm from '@/FormularioForm.vue' import FormularioForm from '@/FormularioForm.vue'
Vue.use(Formulario, { Vue.use(Formulario, {
messages: { validationMessages: {
required: () => 'required', required: () => 'required',
'in': () => 'in', 'in': () => 'in',
min: () => 'min', min: () => 'min',

View File

@ -9,13 +9,13 @@ import FormularioInput from '@/FormularioInput.vue'
const globalRule = jest.fn(() => { return false }) const globalRule = jest.fn(() => { return false })
Vue.use(Formulario, { Vue.use(Formulario, {
rules: { globalRule }, validationRules: { globalRule },
messages: { validationMessages: {
required: () => 'required', required: () => 'required',
'in': () => 'in', 'in': () => 'in',
min: () => 'min', min: () => 'min',
globalRule: () => 'globalRule', globalRule: () => 'globalRule',
} },
}) })
describe('FormularioInput', () => { describe('FormularioInput', () => {