1
0
mirror of synced 2025-02-19 22:03:16 +03:00

chore: Types renames

This commit is contained in:
Zaytsev Kirill 2020-11-06 20:44:04 +03:00
parent 8e3bacab3c
commit 62e65687d1
4 changed files with 41 additions and 37 deletions

View File

@ -4,8 +4,9 @@ import validationMessages from '@/validation/messages'
import { import {
ValidationContext, ValidationContext,
CheckRuleFn, ValidationRuleFn,
CreateMessageFn, ValidationMessageFn,
ValidationMessageI18NFn,
} from '@/validation/validator' } from '@/validation/validator'
export interface FormularioOptions { export interface FormularioOptions {
@ -18,7 +19,7 @@ export interface FormularioOptions {
* The base formulario library. * The base formulario library.
*/ */
export default class Formulario { export default class Formulario {
public validationRules: Record<string, CheckRuleFn> = {} public validationRules: Record<string, ValidationRuleFn> = {}
public validationMessages: Record<string, Function> = {} public validationMessages: Record<string, Function> = {}
constructor (options?: FormularioOptions) { constructor (options?: FormularioOptions) {
@ -43,16 +44,16 @@ 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, ValidationRuleFn> = {}): Record<string, ValidationRuleFn> {
return merge(this.validationRules, 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, ValidationMessageI18NFn|string>): Record<string, ValidationMessageFn> {
const raw = merge(this.validationMessages || {}, extendWith) const raw = merge(this.validationMessages || {}, extendWith)
const messages: Record<string, CreateMessageFn> = {} const messages: Record<string, ValidationMessageFn> = {}
for (const name in raw) { for (const name in raw) {
messages[name] = (context: ValidationContext, ...args: any[]): string => { messages[name] = (context: ValidationContext, ...args: any[]): string => {

View File

@ -15,8 +15,8 @@ import {
} from 'vue-property-decorator' } from 'vue-property-decorator'
import { arrayify, has, shallowEqualObjects, snakeToCamel } from './utils' import { arrayify, has, shallowEqualObjects, snakeToCamel } from './utils'
import { import {
CheckRuleFn, ValidationRuleFn,
CreateMessageFn, ValidationMessageI18NFn,
processConstraints, processConstraints,
validate, validate,
Violation, Violation,
@ -47,8 +47,8 @@ export default class FormularioInput extends Vue {
}) name!: string }) name!: string
@Prop({ default: '' }) validation!: string|any[] @Prop({ default: '' }) validation!: string|any[]
@Prop({ default: () => ({}) }) validationRules!: Record<string, CheckRuleFn> @Prop({ default: () => ({}) }) validationRules!: Record<string, ValidationRuleFn>
@Prop({ default: () => ({}) }) validationMessages!: Record<string, CreateMessageFn|string> @Prop({ default: () => ({}) }) validationMessages!: Record<string, ValidationMessageI18NFn|string>
@Prop({ @Prop({
default: VALIDATION_BEHAVIOR.DEMAND, default: VALIDATION_BEHAVIOR.DEMAND,
validator: behavior => Object.values(VALIDATION_BEHAVIOR).includes(behavior) validator: behavior => Object.values(VALIDATION_BEHAVIOR).includes(behavior)
@ -99,16 +99,16 @@ export default class FormularioInput extends Vue {
}) })
} }
get normalizedValidationRules (): Record<string, CheckRuleFn> { get normalizedValidationRules (): Record<string, ValidationRuleFn> {
const rules: Record<string, CheckRuleFn> = {} const rules: Record<string, ValidationRuleFn> = {}
Object.keys(this.validationRules).forEach(key => { Object.keys(this.validationRules).forEach(key => {
rules[snakeToCamel(key)] = this.validationRules[key] rules[snakeToCamel(key)] = this.validationRules[key]
}) })
return rules return rules
} }
get normalizedValidationMessages (): Record<string, any> { get normalizedValidationMessages (): Record<string, ValidationMessageI18NFn|string> {
const messages: Record<string, any> = {} const messages: Record<string, ValidationMessageI18NFn|string> = {}
Object.keys(this.validationMessages).forEach((key) => { Object.keys(this.validationMessages).forEach((key) => {
messages[snakeToCamel(key)] = this.validationMessages[key] messages[snakeToCamel(key)] = this.validationMessages[key]
}) })

View File

@ -29,7 +29,7 @@ export default {
/** /**
* The value is not a letter. * The value is not a letter.
*/ */
alpha (vm: Vue, context: Record<string, any>): string { alpha (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.alpha', context) return vm.$t('validation.alpha', context)
}, },
@ -175,7 +175,7 @@ export default {
/** /**
* Value is not a url. * Value is not a url.
*/ */
url (vm: Vue, context: Record<string, any>): string { url (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.url.default', context) return vm.$t('validation.url.default', context)
} }
} }

View File

@ -11,14 +11,18 @@ export interface Violation {
message: string; message: string;
} }
export interface CheckRuleFn { export interface ValidationRuleFn {
(context: ValidationContext, ...args: any[]): Promise<boolean>|boolean; (context: ValidationContext, ...args: any[]): Promise<boolean>|boolean;
} }
export interface CreateMessageFn { export interface ValidationMessageFn {
(context: ValidationContext, ...args: any[]): string; (context: ValidationContext, ...args: any[]): string;
} }
export interface ValidationMessageI18NFn {
(vm: Vue, context: ValidationContext, ...args: any[]): string;
}
export interface ValidationContext { export interface ValidationContext {
// The value of the field (do not mutate!), // The value of the field (do not mutate!),
value: any; value: any;
@ -34,21 +38,20 @@ export type ValidatorGroup = {
} }
export function createValidator ( export function createValidator (
ruleFn: CheckRuleFn, ruleFn: ValidationRuleFn,
ruleName: string|null, ruleName: string|null,
ruleArgs: any[], ruleArgs: any[],
messageFn: CreateMessageFn messageFn: ValidationMessageFn
): Validator { ): Validator {
return (context: ValidationContext): Promise<Violation|null> => { return (context: ValidationContext): Promise<Violation|null> => {
return Promise.resolve(ruleFn(context, ...ruleArgs)) return Promise.resolve(ruleFn(context, ...ruleArgs)).then(valid => {
.then(valid => { return !valid ? {
return !valid ? { rule: ruleName,
rule: ruleName, args: ruleArgs,
args: ruleArgs, context,
context, message: messageFn(context, ...ruleArgs),
message: messageFn(context, ...ruleArgs), } : null
} : null })
})
} }
} }
@ -61,8 +64,8 @@ export function parseModifier (ruleName: string): [string, string|null] {
export function processSingleArrayConstraint ( export function processSingleArrayConstraint (
constraint: any[], constraint: any[],
rules: Record<string, CheckRuleFn>, rules: Record<string, ValidationRuleFn>,
messages: Record<string, CreateMessageFn> messages: Record<string, ValidationMessageFn>
): [Validator, string|null, string|null] { ): [Validator, string|null, string|null] {
const args = constraint.slice() const args = constraint.slice()
const first = args.shift() const first = args.shift()
@ -95,8 +98,8 @@ export function processSingleArrayConstraint (
export function processSingleStringConstraint ( export function processSingleStringConstraint (
constraint: string, constraint: string,
rules: Record<string, CheckRuleFn>, rules: Record<string, ValidationRuleFn>,
messages: Record<string, CreateMessageFn> messages: Record<string, ValidationMessageFn>
): [Validator, string|null, string|null] { ): [Validator, string|null, string|null] {
const args = constraint.split(':') const args = constraint.split(':')
const [name, modifier] = parseModifier(args.shift() || '') const [name, modifier] = parseModifier(args.shift() || '')
@ -119,8 +122,8 @@ export function processSingleStringConstraint (
export function processSingleConstraint ( export function processSingleConstraint (
constraint: string|Validator|[Validator|string, ...any[]], constraint: string|Validator|[Validator|string, ...any[]],
rules: Record<string, CheckRuleFn>, rules: Record<string, ValidationRuleFn>,
messages: Record<string, CreateMessageFn> messages: Record<string, ValidationMessageFn>
): [Validator, string|null, string|null] { ): [Validator, string|null, string|null] {
if (typeof constraint === 'function') { if (typeof constraint === 'function') {
return [constraint, null, null] return [constraint, null, null]
@ -139,8 +142,8 @@ export function processSingleConstraint (
export function processConstraints ( export function processConstraints (
constraints: string|any[], constraints: string|any[],
rules: Record<string, CheckRuleFn>, rules: Record<string, ValidationRuleFn>,
messages: Record<string, CreateMessageFn> messages: Record<string, ValidationMessageFn>
): [Validator, string|null, string|null][] { ): [Validator, string|null, string|null][] {
if (typeof constraints === 'string') { if (typeof constraints === 'string') {
return processConstraints(constraints.split('|').filter(f => f.length), rules, messages) return processConstraints(constraints.split('|').filter(f => f.length), rules, messages)