2020-10-10 22:45:28 +03:00
|
|
|
|
import { VueConstructor } from 'vue'
|
2020-10-11 11:41:32 +03:00
|
|
|
|
|
2020-10-22 10:47:53 +03:00
|
|
|
|
import rules from '@/validation/rules'
|
|
|
|
|
import messages from '@/validation/messages'
|
2020-10-14 08:43:17 +03:00
|
|
|
|
import merge from '@/utils/merge'
|
2020-10-09 22:58:28 +03:00
|
|
|
|
|
2020-10-10 22:45:28 +03:00
|
|
|
|
import FormularioForm from '@/FormularioForm.vue'
|
|
|
|
|
import FormularioInput from '@/FormularioInput.vue'
|
2020-10-11 11:41:32 +03:00
|
|
|
|
import FormularioGrouping from '@/FormularioGrouping.vue'
|
2020-10-10 22:45:28 +03:00
|
|
|
|
|
2020-10-22 16:37:57 +03:00
|
|
|
|
import {
|
|
|
|
|
ValidationContext,
|
2020-10-25 22:17:01 +03:00
|
|
|
|
CheckRuleFn,
|
|
|
|
|
CreateMessageFn,
|
|
|
|
|
} from '@/validation/validator'
|
2020-10-10 22:45:28 +03:00
|
|
|
|
|
|
|
|
|
interface FormularioOptions {
|
2020-10-18 16:41:57 +03:00
|
|
|
|
rules?: any;
|
2020-10-25 22:39:38 +03:00
|
|
|
|
messages?: Record<string, Function>;
|
2020-10-10 22:45:28 +03:00
|
|
|
|
}
|
2020-05-22 14:22:56 +03:00
|
|
|
|
|
2020-10-09 22:58:28 +03:00
|
|
|
|
// noinspection JSUnusedGlobalSymbols
|
2020-05-22 14:22:56 +03:00
|
|
|
|
/**
|
|
|
|
|
* The base formulario library.
|
|
|
|
|
*/
|
2020-10-11 11:41:32 +03:00
|
|
|
|
export default class Formulario {
|
2020-10-25 22:17:01 +03:00
|
|
|
|
public rules: Record<string, CheckRuleFn> = {}
|
|
|
|
|
public messages: Record<string, Function> = {}
|
2020-10-10 22:45:28 +03:00
|
|
|
|
|
2020-05-22 14:22:56 +03:00
|
|
|
|
constructor () {
|
2020-10-25 22:17:01 +03:00
|
|
|
|
this.rules = rules
|
|
|
|
|
this.messages = messages
|
2020-05-22 14:22:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Install vue formulario, and register it’s components.
|
|
|
|
|
*/
|
2020-10-22 10:47:53 +03:00
|
|
|
|
install (Vue: VueConstructor, options?: FormularioOptions): void {
|
2020-05-22 14:22:56 +03:00
|
|
|
|
Vue.prototype.$formulario = this
|
2020-10-22 16:37:57 +03:00
|
|
|
|
Vue.component('FormularioForm', FormularioForm)
|
|
|
|
|
Vue.component('FormularioGrouping', FormularioGrouping)
|
|
|
|
|
Vue.component('FormularioInput', FormularioInput)
|
|
|
|
|
|
2020-05-22 14:22:56 +03:00
|
|
|
|
this.extend(options || {})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given a set of options, apply them to the pre-existing options.
|
|
|
|
|
*/
|
2020-10-22 10:47:53 +03:00
|
|
|
|
extend (extendWith: FormularioOptions): Formulario {
|
2020-05-22 14:22:56 +03:00
|
|
|
|
if (typeof extendWith === 'object') {
|
2020-10-25 22:17:01 +03:00
|
|
|
|
this.rules = merge(this.rules, extendWith.rules || {})
|
2020-10-25 22:39:38 +03:00
|
|
|
|
this.messages = merge(this.messages, extendWith.messages || {})
|
2020-05-22 14:22:56 +03:00
|
|
|
|
return this
|
|
|
|
|
}
|
2020-10-25 22:17:01 +03:00
|
|
|
|
throw new Error(`[Formulario]: Formulario.extend() should be passed an object (was ${typeof extendWith})`)
|
2020-05-22 14:22:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get validation rules by merging any passed in with global rules.
|
|
|
|
|
*/
|
2020-10-25 22:17:01 +03:00
|
|
|
|
getRules (extendWith: Record<string, CheckRuleFn> = {}): Record<string, CheckRuleFn> {
|
|
|
|
|
return merge(this.rules, extendWith)
|
2020-05-22 14:22:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 22:39:38 +03:00
|
|
|
|
/**
|
|
|
|
|
* Get validation messages by merging any passed in with global messages.
|
|
|
|
|
*/
|
2020-10-25 22:17:01 +03:00
|
|
|
|
getMessages (vm: Vue, extendWith: Record<string, Function>): Record<string, CreateMessageFn> {
|
|
|
|
|
const raw = merge(this.messages || {}, extendWith)
|
|
|
|
|
const messages: Record<string, CreateMessageFn> = {}
|
|
|
|
|
|
|
|
|
|
for (const name in raw) {
|
|
|
|
|
messages[name] = (context: ValidationContext, ...args: any[]): string => {
|
|
|
|
|
return typeof raw[name] === 'string' ? raw[name] : raw[name](vm, context, ...args)
|
|
|
|
|
}
|
2020-05-25 12:49:49 +03:00
|
|
|
|
}
|
2020-10-25 22:17:01 +03:00
|
|
|
|
|
|
|
|
|
return messages
|
2020-05-22 14:22:56 +03:00
|
|
|
|
}
|
|
|
|
|
}
|