1
0
mirror of synced 2024-11-22 13:26:06 +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 {
return vm.$t('validation.default', context)
},
/**
* Valid accepted value.
*/
accepted (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.accepted', context)
},
/**
* The date is not after.
*/
after (vm: Vue, context: ValidationContext, compare: string | false = false): string {
if (typeof compare === 'string' && compare.length) {
return vm.$t('validation.after.compare', context)
@ -26,23 +28,14 @@ export default {
return vm.$t('validation.after.default', context)
},
/**
* The value is not a letter.
*/
alpha (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.alpha', context)
},
/**
* Rule: checks if the value is alpha numeric
*/
alphanumeric (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.alphanumeric', context)
},
/**
* The date is not before.
*/
before (vm: Vue, context: ValidationContext, compare: string|false = false): string {
if (typeof compare === 'string' && compare.length) {
return vm.$t('validation.before.compare', context)
@ -51,9 +44,6 @@ export default {
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 {
const data = { ...context, from, to }
@ -64,16 +54,10 @@ export default {
return vm.$t('validation.between.default', data)
},
/**
* The confirmation field does not match
*/
confirm (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.confirm', context)
},
/**
* Is not a valid date.
*/
date (vm: Vue, context: ValidationContext, format: string | false = false): string {
if (typeof format === 'string' && format.length) {
return vm.$t('validation.date.format', context)
@ -82,24 +66,15 @@ export default {
return vm.$t('validation.date.default', context)
},
/**
* Is not a valid email address.
*/
email (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.email.default', context)
},
/**
* Ends with specified value
*/
endsWith (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.endsWith.default', context)
},
/**
* Value is an allowed value.
*/
in: function (vm: Vue, context: ValidationContext): string {
in (vm: Vue, context: ValidationContext): string {
if (typeof context.value === 'string' && context.value) {
return vm.$t('validation.in.string', context)
}
@ -107,16 +82,10 @@ export default {
return vm.$t('validation.in.default', context)
},
/**
* Value is not a match.
*/
matches (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.matches.default', context)
},
/**
* The maximum value allowed.
*/
max (vm: Vue, context: ValidationContext, maximum: string | number = 10, force?: string): string {
if (Array.isArray(context.value)) {
return vm.$tc('validation.max.array', maximum, context)
@ -129,9 +98,6 @@ export default {
return vm.$tc('validation.max.default', maximum, context)
},
/**
* The maximum value allowed.
*/
min (vm: Vue, context: ValidationContext, minimum: number | any = 1, force?: string): string {
if (Array.isArray(context.value)) {
return vm.$tc('validation.min.array', minimum, context)
@ -144,38 +110,25 @@ export default {
return vm.$tc('validation.min.default', minimum, context)
},
/**
* The field is not an allowed value
*/
not (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.not.default', context)
},
/**
* The field is not a number
*/
number (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.number.default', context)
},
/**
* Required field.
*/
required (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.required.default', context)
},
/**
* Starts with specified value
*/
startsWith (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.startsWith.default', context)
},
/**
* Value is not a url.
*/
url (vm: Vue, context: ValidationContext): string {
return vm.$t('validation.url.default', context)
}
}
export default messages

View File

@ -1,8 +1,11 @@
import isUrl from 'is-url'
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
*/
@ -257,3 +260,5 @@ export default {
return true
},
}
export default rules

View File

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