Skip to content

Validator overview

Fynix v3 keeps validator construction simple: create a validator through Rule, then configure it with immutable fluent methods. This keeps the API predictable and gives you proper autocomplete for each validator family.

Rule methodValidatorBest for
string()StringValidatorText, labels, and names
boolean()BooleanValidatorStrict booleans
number()NumberValidatorNumeric values and ranges
integer()IntegerValidatorStrict integers
decimal()DecimalValidatorStrict floats
email()EmailValidatorEmail addresses
phoneNumber()PhoneNumberValidatorPhone numbers
password()PasswordValidatorPassword strength and policy
dateTime()DateTimeValidatorDates and times
url()UrlValidatorURLs
uuid()UuidValidatorUUID values
ipAddress()IpAddressValidatorIPv4 and IPv6
regex()RegexValidatorCustom format matching
array() / arrayOf()ArrayValidatorArrays and per-item rules
enum()EnumValidatorEnum values and instances
file()FileValidatorFile upload inputs
image()ImageValidatorA single uploaded image
images()ImagesValidatorMultiple uploaded images
object()ObjectValidatorNested DTOs
objectArray()ObjectArrayValidatorArrays of DTOs
username()UsernameValidatorUsernames and uniqueness

Every validator is built on the same base pipeline:

  • requiredness checks
  • normalization and cleanup
  • HTML tag rejection where applicable
  • common constraints like in(), requiredIf(), sameAs()
  • type-specific validation delegated to validateValue()
use Fynix\Rule;
$validator = Rule::string('displayName')
->label('Display name')
->length(2, 80)
->in(['Alice', 'Bob']);
$first = $validator->validate('A');
$all = $validator->validateAll('A');

Shared methods include label(), required(), optional(), in(), notIn(), sameAs(), differentFrom(), conditional requiredness, and prohibited-field constraints. Fluent methods are immutable and return a new validator instance each time.

use Fynix\Rule;
$error = Rule::email('email')->validate('not-an-email');
$allErrors = Rule::password('password')
->length(12, 128)
->validateAll('abc');

Use validate() when you only care about the first failure. Use validateAll() when you need every applicable violation at once.

$user = new User();
$user->firstName = 'A';
$error = Rule::for($user)
->string('firstName')
->min(2)
->max(50)
->validate();

This validates the named property on the supplied instance without using a global registry.

Use withoutGenericValidation() when a specialized validator must bypass the shared requiredness, HTML, and length layer. File and array validators manage their own structure checks and related validation semantics.