Skip to content

useValidation ​

Provides 30+ validation rule factories, a rule-string-to-function converter, and helpers to generate a complete rule array for a form input.

File: vue/src/js/hooks/useValidation.js


Usage ​

js
import { useValidation } from '@/hooks'

const { generateInputRules, invokeRuleGenerator } = useValidation()

// Generate rules from a schema field definition
const rules = generateInputRules(schemaField)

// Convert a rule string into a callable rule function
const rule = invokeRuleGenerator('required')
html
<v-text-field :rules="generateInputRules(field)" />

Returns ​

Core Helpers ​

NameSignatureDescription
generateInputRules(field) => Array<Function>Reads field.rules (array of strings or objects) and returns an array of Vuetify-compatible rule functions
invokeRuleGenerator(rule: String|Object) => FunctionResolves a rule name string (e.g. 'required') to the corresponding rule factory and returns the callable rule function
validateInput(value, rules) => Boolean|StringRuns a value through an array of rules; returns true on success or the first error message string

Rule Factories ​

All rule factories return (value) => true | errorMessage.

RuleDescription
requiredValue must not be empty / null
emailMust be a valid e-mail address
phoneMust be a valid phone number
urlMust be a valid URL
dateMust be a parseable date string
min(n)String/array length or numeric value ≥ n
max(n)String/array length or numeric value ≤ n
minLength(n)String length ≥ n
maxLength(n)String length ≤ n
minValue(n)Numeric value ≥ n
maxValue(n)Numeric value ≤ n
numericMust be a number
integerMust be an integer
alphaLetters only
alphaNumLetters and digits only
passwordMust meet password complexity requirements
confirmed(field)Must equal the value of another field
uniqueMust be unique (resolved via async endpoint)
regex(pattern)Must match the given regex
sameAs(other)Must equal other
notSameAs(other)Must not equal other
between(min, max)Value must be between min and max
decimalMust be a decimal number
ipAddressMust be a valid IPv4 or IPv6 address
macAddressMust be a valid MAC address
jsonMust be valid JSON
acceptedMust be truthy (checkbox accepted)
requiredIf(condition)Required when condition is true
requiredUnless(condition)Required unless condition is true
maxFileSize(kb)File size must not exceed kb kilobytes
mimes(types)File MIME type must be in types list

Schema Rule Format ​

Rules in a schema field are expressed as strings or objects:

js
{
  rules: [
    'required',
    { name: 'minLength', params: [3] },
    { name: 'maxLength', params: [100] }
  ]
}

See Also ​

  • useInput — base input state that consumes generateInputRules
  • useForm — top-level form validation orchestration