1
0
mirror of synced 2024-11-26 07:16:02 +03:00

chore: Typehints, rewritten comments in @/validation/messages

This commit is contained in:
Zaytsev Kirill 2020-11-06 20:59:49 +03:00
parent 62e65687d1
commit c82c8de5db
3 changed files with 22 additions and 64 deletions

View File

@ -1,23 +1,25 @@
import { ValidationContext } from '@/validation/validator' import {
ValidationContext,
ValidationMessageI18NFn,
} from '@/validation/validator'
export default { /**
* Message builders, names match rules names, see @/validation/rules
*/
const messages: Record<string, ValidationMessageI18NFn> = {
/** /**
* The default render method for error messages. * Fallback for rules without message builder
* @param vm
* @param context
*/ */
default (vm: Vue, context: ValidationContext): string { default (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.default', context) return vm.$t('validation.default', context)
}, },
/**
* Valid accepted value.
*/
accepted (vm: Vue, context: ValidationContext): string { accepted (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.accepted', context) return vm.$t('validation.accepted', context)
}, },
/**
* The date is not after.
*/
after (vm: Vue, context: ValidationContext, compare: string | false = false): string { after (vm: Vue, context: ValidationContext, compare: string | false = false): string {
if (typeof compare === 'string' && compare.length) { if (typeof compare === 'string' && compare.length) {
return vm.$t('validation.after.compare', context) return vm.$t('validation.after.compare', context)
@ -26,23 +28,14 @@ export default {
return vm.$t('validation.after.default', context) return vm.$t('validation.after.default', context)
}, },
/**
* The value is not a letter.
*/
alpha (vm: Vue, context: ValidationContext): string { alpha (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.alpha', context) return vm.$t('validation.alpha', context)
}, },
/**
* Rule: checks if the value is alpha numeric
*/
alphanumeric (vm: Vue, context: ValidationContext): string { alphanumeric (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.alphanumeric', context) return vm.$t('validation.alphanumeric', context)
}, },
/**
* The date is not before.
*/
before (vm: Vue, context: ValidationContext, compare: string|false = false): string { before (vm: Vue, context: ValidationContext, compare: string|false = false): string {
if (typeof compare === 'string' && compare.length) { if (typeof compare === 'string' && compare.length) {
return vm.$t('validation.before.compare', context) return vm.$t('validation.before.compare', context)
@ -51,9 +44,6 @@ export default {
return vm.$t('validation.before.default', context) return vm.$t('validation.before.default', context)
}, },
/**
* The value is not between two numbers or lengths
*/
between (vm: Vue, context: ValidationContext, from: number|any = 0, to: number|any = 10, force?: string): string { between (vm: Vue, context: ValidationContext, from: number|any = 0, to: number|any = 10, force?: string): string {
const data = { ...context, from, to } const data = { ...context, from, to }
@ -64,16 +54,10 @@ export default {
return vm.$t('validation.between.default', data) return vm.$t('validation.between.default', data)
}, },
/**
* The confirmation field does not match
*/
confirm (vm: Vue, context: ValidationContext): string { confirm (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.confirm', context) return vm.$t('validation.confirm', context)
}, },
/**
* Is not a valid date.
*/
date (vm: Vue, context: ValidationContext, format: string | false = false): string { date (vm: Vue, context: ValidationContext, format: string | false = false): string {
if (typeof format === 'string' && format.length) { if (typeof format === 'string' && format.length) {
return vm.$t('validation.date.format', context) return vm.$t('validation.date.format', context)
@ -82,24 +66,15 @@ export default {
return vm.$t('validation.date.default', context) return vm.$t('validation.date.default', context)
}, },
/**
* Is not a valid email address.
*/
email (vm: Vue, context: ValidationContext): string { email (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.email.default', context) return vm.$t('validation.email.default', context)
}, },
/**
* Ends with specified value
*/
endsWith (vm: Vue, context: ValidationContext): string { endsWith (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.endsWith.default', context) return vm.$t('validation.endsWith.default', context)
}, },
/** in (vm: Vue, context: ValidationContext): string {
* Value is an allowed value.
*/
in: function (vm: Vue, context: ValidationContext): string {
if (typeof context.value === 'string' && context.value) { if (typeof context.value === 'string' && context.value) {
return vm.$t('validation.in.string', context) return vm.$t('validation.in.string', context)
} }
@ -107,16 +82,10 @@ export default {
return vm.$t('validation.in.default', context) return vm.$t('validation.in.default', context)
}, },
/**
* Value is not a match.
*/
matches (vm: Vue, context: ValidationContext): string { matches (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.matches.default', context) return vm.$t('validation.matches.default', context)
}, },
/**
* The maximum value allowed.
*/
max (vm: Vue, context: ValidationContext, maximum: string | number = 10, force?: string): string { max (vm: Vue, context: ValidationContext, maximum: string | number = 10, force?: string): string {
if (Array.isArray(context.value)) { if (Array.isArray(context.value)) {
return vm.$tc('validation.max.array', maximum, context) return vm.$tc('validation.max.array', maximum, context)
@ -129,9 +98,6 @@ export default {
return vm.$tc('validation.max.default', maximum, context) return vm.$tc('validation.max.default', maximum, context)
}, },
/**
* The maximum value allowed.
*/
min (vm: Vue, context: ValidationContext, minimum: number | any = 1, force?: string): string { min (vm: Vue, context: ValidationContext, minimum: number | any = 1, force?: string): string {
if (Array.isArray(context.value)) { if (Array.isArray(context.value)) {
return vm.$tc('validation.min.array', minimum, context) return vm.$tc('validation.min.array', minimum, context)
@ -144,38 +110,25 @@ export default {
return vm.$tc('validation.min.default', minimum, context) return vm.$tc('validation.min.default', minimum, context)
}, },
/**
* The field is not an allowed value
*/
not (vm: Vue, context: ValidationContext): string { not (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.not.default', context) return vm.$t('validation.not.default', context)
}, },
/**
* The field is not a number
*/
number (vm: Vue, context: ValidationContext): string { number (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.number.default', context) return vm.$t('validation.number.default', context)
}, },
/**
* Required field.
*/
required (vm: Vue, context: ValidationContext): string { required (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.required.default', context) return vm.$t('validation.required.default', context)
}, },
/**
* Starts with specified value
*/
startsWith (vm: Vue, context: ValidationContext): string { startsWith (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.startsWith.default', context) return vm.$t('validation.startsWith.default', context)
}, },
/**
* Value is not a url.
*/
url (vm: Vue, context: ValidationContext): string { url (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.url.default', context) return vm.$t('validation.url.default', context)
} }
} }
export default messages

View File

@ -1,8 +1,11 @@
import isUrl from 'is-url' import isUrl from 'is-url'
import { has, regexForFormat, shallowEqualObjects } from '@/utils' import { has, regexForFormat, shallowEqualObjects } from '@/utils'
import { ValidationContext } from '@/validation/validator' import {
ValidationContext,
ValidationRuleFn,
} from '@/validation/validator'
export default { const rules: Record<string, ValidationRuleFn> = {
/** /**
* Rule: the value must be "yes", "on", "1", or true * Rule: the value must be "yes", "on", "1", or true
*/ */
@ -257,3 +260,5 @@ export default {
return true return true
}, },
} }
export default rules

View File

@ -121,7 +121,7 @@ export function processSingleStringConstraint (
} }
export function processSingleConstraint ( export function processSingleConstraint (
constraint: string|Validator|[Validator|string, ...any[]], constraint: Validator|string|[Validator|string, ...any[]],
rules: Record<string, ValidationRuleFn>, rules: Record<string, ValidationRuleFn>,
messages: Record<string, ValidationMessageFn> messages: Record<string, ValidationMessageFn>
): [Validator, string|null, string|null] { ): [Validator, string|null, string|null] {