2020-10-14 08:43:17 +03:00
|
|
|
import merge from '@/utils/merge'
|
2020-10-26 17:03:24 +03:00
|
|
|
import validationRules from '@/validation/rules'
|
|
|
|
import validationMessages from '@/validation/messages'
|
2020-10-09 22:58:28 +03:00
|
|
|
|
2020-10-22 16:37:57 +03:00
|
|
|
import {
|
|
|
|
ValidationContext,
|
2020-11-06 20:44:04 +03:00
|
|
|
ValidationRuleFn,
|
|
|
|
ValidationMessageFn,
|
|
|
|
ValidationMessageI18NFn,
|
2021-05-30 17:40:33 +03:00
|
|
|
ViolationsRecord,
|
2020-10-25 22:17:01 +03:00
|
|
|
} from '@/validation/validator'
|
2020-10-10 22:45:28 +03:00
|
|
|
|
2021-05-30 17:40:33 +03:00
|
|
|
import { FormularioFormInterface } from '@/types'
|
|
|
|
|
2020-11-02 20:34:09 +03:00
|
|
|
export interface FormularioOptions {
|
2020-11-10 09:53:13 +03:00
|
|
|
validationRules?: Record<string, ValidationRuleFn>;
|
|
|
|
validationMessages?: Record<string, ValidationMessageI18NFn|string>;
|
2020-10-10 22:45:28 +03:00
|
|
|
}
|
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-11-06 20:44:04 +03:00
|
|
|
public validationRules: Record<string, ValidationRuleFn> = {}
|
2020-11-10 09:47:40 +03:00
|
|
|
public validationMessages: Record<string, ValidationMessageI18NFn|string> = {}
|
2020-10-10 22:45:28 +03:00
|
|
|
|
2021-05-30 17:40:33 +03:00
|
|
|
private readonly registry: Map<string, FormularioFormInterface>
|
|
|
|
|
2020-11-02 20:34:09 +03:00
|
|
|
constructor (options?: FormularioOptions) {
|
2021-05-30 17:40:33 +03:00
|
|
|
this.registry = new Map()
|
|
|
|
|
2020-10-26 17:03:24 +03:00
|
|
|
this.validationRules = validationRules
|
|
|
|
this.validationMessages = validationMessages
|
2020-10-22 16:37:57 +03:00
|
|
|
|
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-26 17:03:24 +03:00
|
|
|
this.validationRules = merge(this.validationRules, extendWith.validationRules || {})
|
|
|
|
this.validationMessages = merge(this.validationMessages, extendWith.validationMessages || {})
|
2020-05-22 14:22:56 +03:00
|
|
|
return this
|
|
|
|
}
|
2021-05-30 17:40:33 +03:00
|
|
|
throw new Error(`[Formulario]: Formulario.extend(): should be passed an object (was ${typeof extendWith})`)
|
|
|
|
}
|
|
|
|
|
|
|
|
runValidation (id: string): Promise<ViolationsRecord> {
|
|
|
|
if (!this.registry.has(id)) {
|
|
|
|
throw new Error(`[Formulario]: Formulario.runValidation(): no forms with id "${id}"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const form = this.registry.get(id) as FormularioFormInterface
|
|
|
|
|
|
|
|
return form.runValidation()
|
|
|
|
}
|
|
|
|
|
|
|
|
resetValidation (id: string): void {
|
|
|
|
if (!this.registry.has(id)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const form = this.registry.get(id) as FormularioFormInterface
|
|
|
|
|
|
|
|
form.resetValidation()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by forms instances to add themselves into a registry
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
register (id: string, form: FormularioFormInterface): void {
|
|
|
|
if (this.registry.has(id)) {
|
|
|
|
throw new Error(`[Formulario]: Formulario.register(): id "${id}" is already in use`)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.registry.set(id, form)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by forms instances to remove themselves from a registry
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
unregister (id: string): void {
|
|
|
|
if (this.registry.has(id)) {
|
|
|
|
this.registry.delete(id)
|
|
|
|
}
|
2020-05-22 14:22:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get validation rules by merging any passed in with global rules.
|
2021-05-30 17:40:33 +03:00
|
|
|
* @internal
|
2020-05-22 14:22:56 +03:00
|
|
|
*/
|
2020-11-06 20:44:04 +03:00
|
|
|
getRules (extendWith: Record<string, ValidationRuleFn> = {}): Record<string, ValidationRuleFn> {
|
2020-10-26 17:03:24 +03:00
|
|
|
return merge(this.validationRules, 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.
|
2021-05-30 17:40:33 +03:00
|
|
|
* @internal
|
2020-10-25 22:39:38 +03:00
|
|
|
*/
|
2020-11-06 20:44:04 +03:00
|
|
|
getMessages (vm: Vue, extendWith: Record<string, ValidationMessageI18NFn|string>): Record<string, ValidationMessageFn> {
|
2020-10-26 17:03:24 +03:00
|
|
|
const raw = merge(this.validationMessages || {}, extendWith)
|
2020-11-06 20:44:04 +03:00
|
|
|
const messages: Record<string, ValidationMessageFn> = {}
|
2020-10-25 22:17:01 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|