From c82c8de5dba4d92698b3dda3d58930d706061011 Mon Sep 17 00:00:00 2001 From: Zaytsev Kirill Date: Fri, 6 Nov 2020 20:59:49 +0300 Subject: [PATCH] chore: Typehints, rewritten comments in @/validation/messages --- src/validation/messages.ts | 75 +++++++------------------------------ src/validation/rules.ts | 9 ++++- src/validation/validator.ts | 2 +- 3 files changed, 22 insertions(+), 64 deletions(-) diff --git a/src/validation/messages.ts b/src/validation/messages.ts index e42cd05..90bae34 100644 --- a/src/validation/messages.ts +++ b/src/validation/messages.ts @@ -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 = { /** - * 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 diff --git a/src/validation/rules.ts b/src/validation/rules.ts index 4ecbfba..c88f86c 100644 --- a/src/validation/rules.ts +++ b/src/validation/rules.ts @@ -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 = { /** * Rule: the value must be "yes", "on", "1", or true */ @@ -257,3 +260,5 @@ export default { return true }, } + +export default rules diff --git a/src/validation/validator.ts b/src/validation/validator.ts index 50b767a..2954a8e 100644 --- a/src/validation/validator.ts +++ b/src/validation/validator.ts @@ -121,7 +121,7 @@ export function processSingleStringConstraint ( } export function processSingleConstraint ( - constraint: string|Validator|[Validator|string, ...any[]], + constraint: Validator|string|[Validator|string, ...any[]], rules: Record, messages: Record ): [Validator, string|null, string|null] {