VOOZH about

URL: https://deepwiki.com/hypervel/validation/3.2-built-in-validation-rules

⇱ Built-in Validation Rules | hypervel/validation | DeepWiki


Loading...
Menu

Built-in Validation Rules

This page provides a comprehensive reference for all 50+ built-in validation methods implemented in the ValidatesAttributes trait. These methods form the core validation logic of the Hypervel Validation package and are automatically invoked when string-based rules are specified.

Scope: This page documents the validation methods themselves, their parameters, and behavior. For information about creating custom validation rules, see Custom Validation Rules. For object-based rule implementations, see Built-in Rule Objects.

Rule Categories Overview

The built-in validation rules are organized into 13 functional categories based on their purpose:


Sources: src/Concerns/ValidatesAttributes.php1-2334

Rule Name to Method Mapping

All built-in validation rules follow a consistent naming convention where the rule name maps directly to a method in the ValidatesAttributes trait:


Sources: src/Concerns/ValidatesAttributes.php37-2333

Type Validation Rules

These rules verify that a value matches a specific PHP type.

RuleMethodParametersDescription
booleanvalidateBooleanNoneAccepts true, false, 0, 1, '0', '1'
stringvalidateStringNoneVerifies value is a PHP string
integervalidateIntegerNoneUses FILTER_VALIDATE_INT
numericvalidateNumericNoneVerifies is_numeric() returns true
arrayvalidateArrayOptional: key listValidates array structure; optionally checks for specific keys
listvalidateListNoneValidates array is a sequential list (uses array_is_list())

Implementation Details:

Sources: src/Concerns/ValidatesAttributes.php398-403 src/Concerns/ValidatesAttributes.php2149-2152 src/Concerns/ValidatesAttributes.php1270-1273 src/Concerns/ValidatesAttributes.php1591-1594 src/Concerns/ValidatesAttributes.php339-350 src/Concerns/ValidatesAttributes.php355-358

Format Validation Rules

Character Set Rules

RuleMethodParametersDescription
alphavalidateAlphaOptional: 'ascii'Alphabetic characters only; with ascii limits to ASCII
alpha_dashvalidateAlphaDashOptional: 'ascii'Alphanumeric plus dashes and underscores
alpha_numvalidateAlphaNumOptional: 'ascii'Alphanumeric characters only
asciivalidateAsciiNone7-bit ASCII characters only
lowercasevalidateLowercaseNoneAll characters must be lowercase
uppercasevalidateUppercaseNoneAll characters must be uppercase

Implementation Details:

The character validation methods src/Concerns/ValidatesAttributes.php290-332 use Unicode-aware regular expressions by default:

  • \pL matches any Unicode letter
  • \pM matches any Unicode mark
  • \pN matches any Unicode number

When the ascii parameter is provided, the validation switches to ASCII-only patterns ([a-zA-Z], [a-zA-Z0-9]).

Sources: src/Concerns/ValidatesAttributes.php290-297 src/Concerns/ValidatesAttributes.php304-315 src/Concerns/ValidatesAttributes.php321-332 src/Concerns/ValidatesAttributes.php134-137 src/Concerns/ValidatesAttributes.php1186-1192 src/Concerns/ValidatesAttributes.php1198-1203

Network and Identifier Rules

RuleMethodParametersDescription
emailvalidateEmailOptional: rfc, strict, dns, spoof, filter, filter_unicodeEmail validation with multiple strategies
urlvalidateUrlOptional: protocolsValidates URL format
active_urlvalidateActiveUrlNoneValidates URL with DNS lookup
ipvalidateIpNoneValid IPv4 or IPv6 address
ipv4validateIpv4NoneValid IPv4 address
ipv6validateIpv6NoneValid IPv6 address
mac_addressvalidateMacAddressNoneValid MAC address
uuidvalidateUuidOptional: versionValid UUID (optionally specific version)
ulidvalidateUlidNoneValid ULID
jsonvalidateJsonNoneValid JSON string
timezonevalidateTimezoneOptional: group, countryValid timezone identifier
hex_colorvalidateHexColorNoneValid hexadecimal color code

Email Validation Strategy src/Concerns/ValidatesAttributes.php779-802:


The email validation integrates with the egulias/email-validator library and supports multiple validation strategies that can be combined. See Email Validation Strategies for detailed documentation.

Sources: src/Concerns/ValidatesAttributes.php779-802 src/Concerns/ValidatesAttributes.php2168-2171 src/Concerns/ValidatesAttributes.php102-121 src/Concerns/ValidatesAttributes.php1278-1305 src/Concerns/ValidatesAttributes.php2186-2199 src/Concerns/ValidatesAttributes.php2176-2179 src/Concerns/ValidatesAttributes.php1310-1328 src/Concerns/ValidatesAttributes.php2157-2163 src/Concerns/ValidatesAttributes.php1208-1211

String Content Rules

RuleMethodParametersDescription
starts_withvalidateStartsWithsubstring(s)Value starts with any of the given substrings
doesnt_start_withvalidateDoesntStartWithsubstring(s)Value doesn't start with any given substring
ends_withvalidateEndsWithsubstring(s)Value ends with any of the given substrings
doesnt_end_withvalidateDoesntEndWithsubstring(s)Value doesn't end with any given substring
containsvalidateContainsvalue(s)Array contains all specified values
doesnt_containvalidateDoesntContainvalue(s)Array doesn't contain any specified values
regexvalidateRegexpatternMatches regular expression pattern
not_regexvalidateNotRegexpatternDoesn't match regular expression pattern

Implementation Note: The contains and doesnt_contain rules src/Concerns/ValidatesAttributes.php420-453 operate on array values, not string content. They verify that specific values exist (or don't exist) within an array.

Sources: src/Concerns/ValidatesAttributes.php2111-2144 src/Concerns/ValidatesAttributes.php420-433 src/Concerns/ValidatesAttributes.php440-453 src/Concerns/ValidatesAttributes.php1677-1702

Size Validation Rules

Size validation rules adapt their behavior based on the type of value being validated:

Value TypeSize Meaning
NumericThe numeric value itself
StringString length in characters (MB-aware)
ArrayNumber of elements
FileFile size in kilobytes

Size Comparison Rules

RuleMethodParametersComparisonDescription
sizevalidateSizevalue==Exactly equal to size
minvalidateMinvalue>=Greater than or equal to size
maxvalidateMaxvalue<=Less than or equal to size
betweenvalidateBetweenmin, maxmin <= x <= maxBetween two sizes inclusive

Attribute Comparison Rules

RuleMethodParametersComparisonDescription
gtvalidateGtattribute>Greater than another attribute
gtevalidateGteattribute>=Greater than or equal to another attribute
ltvalidateLtattribute<Less than another attribute
ltevalidateLteattribute<=Less than or equal to another attribute

Size Calculation Logic src/Concerns/ValidatesAttributes.php2204-2223:


Precision Note: All size comparisons use the Brick\Math\BigNumber library src/Concerns/ValidatesAttributes.php385-393 for arbitrary precision arithmetic, preventing floating-point precision issues.

Sources: src/Concerns/ValidatesAttributes.php2089-2094 src/Concerns/ValidatesAttributes.php1432-1437 src/Concerns/ValidatesAttributes.php1335-1344 src/Concerns/ValidatesAttributes.php385-393 src/Concerns/ValidatesAttributes.php1058-1179 src/Concerns/ValidatesAttributes.php2204-2223

Numeric Validation Rules

RuleMethodParametersDescription
digitsvalidateDigitscountExactly count digits (numeric string)
digits_betweenvalidateDigitsBetweenmin, maxBetween min and max digits
min_digitsvalidateMinDigitsminAt least min digits
max_digitsvalidateMaxDigitsmaxAt most max digits
decimalvalidateDecimalplaces [, max]Decimal with exact or range of decimal places
multiple_ofvalidateMultipleOfvalueValue is a multiple of parameter

Implementation Details:

Sources: src/Concerns/ValidatesAttributes.php592-600 src/Concerns/ValidatesAttributes.php607-615 src/Concerns/ValidatesAttributes.php1444-1451 src/Concerns/ValidatesAttributes.php1351-1358 src/Concerns/ValidatesAttributes.php541-563 src/Concerns/ValidatesAttributes.php1538-1566

Date and Time Validation Rules

Basic Date Rules

RuleMethodParametersDescription
datevalidateDateNoneValid date (uses strtotime and checkdate)
date_formatvalidateDateFormatformat(s)Date matches specific format(s)
timezonevalidateTimezoneOptional: group, countryValid timezone identifier

Date Comparison Rules

RuleMethodParametersComparisonDescription
date_equalsvalidateDateEqualsdate==Date equals comparison date
beforevalidateBeforedate<Date is before comparison date
before_or_equalvalidateBeforeOrEqualdate<=Date is before or equal to comparison date
aftervalidateAfterdate>Date is after comparison date
after_or_equalvalidateAfterOrEqualdate>=Date is after or equal to comparison date

Date Comparison Logic src/Concerns/ValidatesAttributes.php202-217:


Date Parsing: The system uses Carbon::parse() src/Concerns/ValidatesAttributes.php276-284 as a fallback for flexible date parsing when explicit format matching fails.

Sources: src/Concerns/ValidatesAttributes.php477-494 src/Concerns/ValidatesAttributes.php501-522 src/Concerns/ValidatesAttributes.php2157-2163 src/Concerns/ValidatesAttributes.php154-195 src/Concerns/ValidatesAttributes.php529-534 src/Concerns/ValidatesAttributes.php202-259

File Validation Rules

Basic File Rules

RuleMethodParametersDescription
filevalidateFileNoneValid file upload
imagevalidateImageOptional: 'allow_svg'Image file (jpg, jpeg, png, gif, bmp, webp)
mimesvalidateMimesextensionsFile extension matches list
mimetypesvalidateMimetypesmime typesMIME type matches list
extensionsvalidateExtensionsextensionsFile extension in list

Advanced File Rules

RuleMethodParametersDescription
dimensionsvalidateDimensionsconstraintsImage dimensions validation

File Validation Process src/Concerns/ValidatesAttributes.php1036-1039:


PHP Upload Protection src/Concerns/ValidatesAttributes.php1407-1425: By default, all file validation rules block PHP file uploads (extensions: php, php3, php4, php5, php7, php8, phtml, phar) unless explicitly allowed by including 'php' in the parameters.

Dimensions Validation src/Concerns/ValidatesAttributes.php622-652 accepts named parameters:

  • width, height - Exact dimensions
  • min_width, max_width, min_height, max_height - Dimension ranges
  • ratio, min_ratio, max_ratio - Aspect ratio constraints (format: 3/2)

SVG files bypass dimension checking src/Concerns/ValidatesAttributes.php624-626

Sources: src/Concerns/ValidatesAttributes.php1036-1039 src/Concerns/ValidatesAttributes.php1216-1225 src/Concerns/ValidatesAttributes.php1365-1400 src/Concerns/ValidatesAttributes.php1020-1031 src/Concerns/ValidatesAttributes.php622-720 src/Concerns/ValidatesAttributes.php2228-2235

Presence Validation Rules

Presence rules determine whether an attribute must exist, may exist, or must not exist based on various conditions.

Core Presence Rules

RuleMethodDescription
requiredvalidateRequiredAttribute must exist and not be empty
presentvalidatePresentAttribute must exist in data (can be empty)
filledvalidateFilledIf attribute exists, it must not be empty
missingvalidateMissingAttribute must not exist in data

Required Value Definition src/Concerns/ValidatesAttributes.php1707-1723:

  • null values fail
  • Empty strings (after trim) fail
  • Countable values with count < 1 fail
  • Files with empty path fail
  • All other values pass

Conditional Presence Rules


Dependent Rule Parameter Parsing src/Concerns/ValidatesAttributes.php1942-1957: The parseDependentRuleParameters method handles type coercion for conditional rules. It converts string 'true'/'false' to boolean and 'null' to null when comparing against boolean or null fields.

Sources: src/Concerns/ValidatesAttributes.php1707-1723 src/Concerns/ValidatesAttributes.php1599-1602 src/Concerns/ValidatesAttributes.php1044-1051 src/Concerns/ValidatesAttributes.php1460-1463 src/Concerns/ValidatesAttributes.php1728-2040 src/Concerns/ValidatesAttributes.php1609-1670 src/Concerns/ValidatesAttributes.php1470-1531

Prohibition Validation Rules

Prohibition rules ensure that attributes do not exist or are empty based on various conditions.

RuleMethodDescription
prohibitedvalidateProhibitedAttribute must not exist or be empty
prohibited_ifvalidateProhibitedIfProhibited when another field has specific value
prohibited_if_acceptedvalidateProhibitedIfAcceptedProhibited when another field is accepted
prohibited_if_declinedvalidateProhibitedIfDeclinedProhibited when another field is declined
prohibited_unlessvalidateProhibitedUnlessProhibited unless another field has specific value
prohibitsvalidateProhibitsWhen this exists, other fields must not exist

Implementation: Prohibited rules src/Concerns/ValidatesAttributes.php1776-1855 are essentially the inverse of required rules - they pass when validateRequired would fail.

Sources: src/Concerns/ValidatesAttributes.php1776-1779 src/Concerns/ValidatesAttributes.php1784-1839 src/Concerns/ValidatesAttributes.php1844-1855

Exclusion Validation Rules

Exclusion rules mark attributes that should be removed from the validated data. Unlike prohibition rules which validate absence, exclusion rules allow presence but exclude the attribute from the final output.

RuleMethodReturnsDescription
excludevalidateExcludefalseAlways exclude this attribute
exclude_ifvalidateExcludeIffalseExclude when other field has specific value
exclude_unlessvalidateExcludeUnlessfalseExclude unless other field has specific value
exclude_withvalidateExcludeWithfalseExclude when another field is present
exclude_withoutvalidateExcludeWithoutfalseExclude when another field is missing

Special Behavior src/Concerns/ValidatesAttributes.php1860-1935: Exclusion rules return false to indicate the attribute should be excluded from validated data, not that validation failed. The Validator checks for these rules when building the final validated data set.

Sources: src/Concerns/ValidatesAttributes.php1860-1863 src/Concerns/ValidatesAttributes.php1868-1935

Acceptance and Declination Rules

These rules validate boolean-like consent or refusal values.

Acceptance Rules

RuleMethodAccepted ValuesDescription
acceptedvalidateAccepted'yes', 'on', '1', 1, true, 'true'Attribute must be accepted (implies required)
accepted_ifvalidateAcceptedIfSameAccepted when other field has specific value

Declination Rules

RuleMethodDeclined ValuesDescription
declinedvalidateDeclined'no', 'off', '0', 0, false, 'false'Attribute must be declined (implies required)
declined_ifvalidateDeclinedIfSameDeclined when other field has specific value

Important: Both acceptance and declination rules src/Concerns/ValidatesAttributes.php44-97 internally call validateRequired, so they implicitly require the attribute to be present.

Sources: src/Concerns/ValidatesAttributes.php44-49 src/Concerns/ValidatesAttributes.php54-67 src/Concerns/ValidatesAttributes.php74-79 src/Concerns/ValidatesAttributes.php84-97

Comparison and Relationship Rules

Value Comparison Rules

RuleMethodParametersDescription
samevalidateSameattributeValue must equal another attribute's value
differentvalidateDifferentattribute(s)Value must differ from all listed attributes
confirmedvalidateConfirmedOptional: fieldValue must match {attribute}_confirmation field

List Membership Rules

RuleMethodParametersDescription
invalidateInvalue(s)Value must be in the list
not_invalidateNotInvalue(s)Value must not be in the list
in_arrayvalidateInArrayattributeValue must exist in another attribute's array
distinctvalidateDistinctOptional: 'strict', 'ignore_case'Array value must be unique among siblings
required_array_keysvalidateRequiredArrayKeyskey(s)Array must contain all specified keys

Array Handling src/Concerns/ValidatesAttributes.php1232-1245: The in rule has special handling when validating an array value against array rules. It ensures all array elements are in the allowed list and no elements are themselves arrays.

Distinct Values Caching src/Concerns/ValidatesAttributes.php741-755: The distinct rule caches extracted values per attribute to avoid repeated extraction when validating array elements.

Sources: src/Concerns/ValidatesAttributes.php2075-2082 src/Concerns/ValidatesAttributes.php570-585 src/Concerns/ValidatesAttributes.php410-413 src/Concerns/ValidatesAttributes.php1232-1245 src/Concerns/ValidatesAttributes.php1583-1586 src/Concerns/ValidatesAttributes.php1252-1265 src/Concerns/ValidatesAttributes.php727-772 src/Concerns/ValidatesAttributes.php365-378

Database Validation Rules

Database rules verify data against database records using the PresenceVerifierInterface. These rules require database integration to be configured. See Database Integration for setup details.

RuleMethodParametersDescription
existsvalidateExiststable[,column][,conditions...]Record must exist in database
uniquevalidateUniquetable[,column][,id][,idColumn][,conditions...]Value must be unique in database

Parameter Format:

table - Table name or Model::class
column - Column to check (defaults to attribute name)
id - ID to exclude (for unique, during updates)
idColumn - ID column name (defaults to 'id')
conditions... - Additional where clauses (key,value pairs)

Table Specification src/Concerns/ValidatesAttributes.php955-973:


Integration with Rule Objects src/Concerns/ValidatesAttributes.php848-850: When using Exists or Unique rule objects, additional query callbacks from the rule object are merged with the parameters.

Sources: src/Concerns/ValidatesAttributes.php809-833 src/Concerns/ValidatesAttributes.php864-904 src/Concerns/ValidatesAttributes.php955-997 src/Concerns/ValidatesAttributes.php1002-1013

Meta and Control Flow Rules

These rules don't perform validation themselves but control validation behavior.

RuleMethodReturnsPurpose
bailvalidateBailtrueStop validation on first failure for this attribute
nullablevalidateNullabletrueAllow null values to pass all rules
sometimesvalidateSometimestrueMarker for conditional validation

Implementation: These rules src/Concerns/ValidatesAttributes.php144-147 src/Concerns/ValidatesAttributes.php1573-1576 src/Concerns/ValidatesAttributes.php2101-2104 always return true and serve as markers for the Validator's control flow logic. The Validator checks for these specific rule names to alter its behavior.

Sources: src/Concerns/ValidatesAttributes.php144-147 src/Concerns/ValidatesAttributes.php1573-1576 src/Concerns/ValidatesAttributes.php2101-2104

Helper Methods and Utilities

The ValidatesAttributes trait includes several protected helper methods used by validation rules:

Size and Type Helpers

MethodPurpose
getSize(attribute, value)Calculate size based on value type
isValidFileInstance(value)Check if value is valid file
isSameType(first, second)Compare types of two values
trim(value)Trim string values

Date Helpers

MethodPurpose
getDateTime(value)Parse value to DateTime using Carbon
getDateTimestamp(value)Get Unix timestamp from date value
getDateFormat(attribute)Get date_format rule parameter if exists
checkDateTimeOrder(format, first, second, operator)Compare formatted dates

Comparison Helpers

MethodPurpose
compare(first, second, operator)Generic comparison with operator
compareDates(attribute, value, parameters, operator)Date comparison logic

Database Helpers

MethodPurpose
parseTable(table)Parse table specification to [connection, table, idColumn]
getQueryColumn(parameters, attribute)Determine database column name
guessColumnForQuery(attribute)Extract column from attribute path
getExtraConditions(segments)Parse additional where conditions

Parameter Helpers

MethodPurpose
parseNamedParameters(parameters)Convert key=value strings to array
parseDependentRuleParameters(parameters)Extract and convert comparison values
requireParameterCount(count, parameters, rule)Validate parameter count
shouldBeNumeric(attribute, rule)Add rule to numeric rules list

Math Helpers

MethodPurpose
ensureExponentWithinAllowedRange(attribute, value)Validate scientific notation exponent

Sources: src/Concerns/ValidatesAttributes.php2204-2333 src/Concerns/ValidatesAttributes.php234-284 src/Concerns/ValidatesAttributes.php2242-2252 src/Concerns/ValidatesAttributes.php955-1013 src/Concerns/ValidatesAttributes.php2259-2280 src/Concerns/ValidatesAttributes.php1942-1992 src/Concerns/ValidatesAttributes.php2300-2332

Refresh this wiki

On this page