Download the PHP package wscore/leanvalidator without Composer

On this page you can find all versions of the php package wscore/leanvalidator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package leanvalidator

LeanValidator

PhpUnit

An AI-friendly and simple validator for PHP.

Features

Sanitizer:

This package also provides a sanitization utility that can be used to clean and transform input data.

Installation

Use composer to install the package.

Basic Usage

Initialization

You can create a validator instance using the make method.

Defining Rules

Use field to specify the field and chain validation rules.

Checking Results

Rules and Messages

Default Error Messages

By default, the validator provides two error messages for common validation rules:

You can change these default messages for the entire validator instance:

Custom Error Messages

You can set custom messages at three levels:

  1. Rule level: Use with() to set a custom message for the next rule in the chain. This message is used only for that rule and is cleared immediately after.
  2. Field level: Use the second argument of field() to set a default error message for all rules in that field.
  3. Manual level: Use addError(string $field, string $message) to manually add an error to a specific field.
  4. Required level: Pass a custom message directly to required().

The message() call only affects the rule that immediately follows it. If you want to use custom messages for multiple rules, call message() before each one.

The validator searches for messages in the following order:

  1. Message passed directly to the rule (via message() or required($msg)).
  2. Message passed to field($key, $msg).
  3. Global default message (defaultMessage or defaultMessageRequired).

Getting Error Messages

If isValid() returns false, you can retrieve error messages from the validator:

The MessageBag also supports retrieving messages using HTML form field names:

Validation Flow

Optional Fields

Required Fields

Conditional Required Fields (requiredIf, requiredWith, requiredWithout)

Use these methods to make a field required based on another field.

requiredIf(string $otherKey, mixed $expect, ?string $msg = null, mixed $elseOverwrite = null)

Required if $otherKey's value matches $expect.

requiredUnless(string $otherKey, mixed $expect, ?string $msg = null, mixed $elseOverwrite = null)

Required unless $otherKey's value matches $expect.

requiredWith(string $otherKey, ?string $msg = null, mixed $elseOverwrite = null)

Required if $otherKey exists in the input data (even if it's null).

requiredWithout(string $otherKey, ?string $msg = null, mixed $elseOverwrite = null)

Required if $otherKey does not exist in the input data.

requiredWhen(callable $call, ?string $msg = null, mixed $elseOverwrite = null)

Required if the callback $call($data) returns true. The $data contains all input data.

elseOverwrite

For all these methods, if the condition is not met, you can provide an elseOverwrite value. The field will be set to this value and further validation rules in the chain will be skipped.

Built-in Rules

Advanced Validation

Validating Enums

You can validate against PHP Enums using enum() or in().

When using enum() with a BackedEnum: int, the validator automatically casts numeric strings to integers before validation.

Validating Arrays of Scalars

Use asList() to apply a rule to every element in an array.

Validating Array Size

Use arrayCount() to validate the number of elements in an array.

Validating Nested Objects (Associative Arrays)

Use asObject() to validate a nested associative array without using dot-notation in field. Only the validated fields will be included in getValidatedData() (whitelist).

Validating Arrays of Objects (Nested Data)

Use asListObject() to validate complex nested structures.

Custom Validators

You can use apply() to use any callable, closure, or rule class as a validation rule.

Extending: Custom Rules and IDE Completion

You can add project-specific rules with IDE code completion by extending ValidatorRules and overriding createRules() on your Validator (or ValidatorData) subclass.

  1. Extend ValidatorRules and add your methods (or register names in $rules and use @method in the docblock).
  2. Override createRules() in your Validator subclass to return your rules instance.
  3. Override field() with @return YourValidatorRules so that $v->field('x') is inferred as your class and your custom methods appear in autocomplete.

Language- or context-specific Rules (Japanese, etc.)

For Japanese-specific validations, use the Wscore\LeanValidator\Rule\Ja class.

Available rules in Ja class:

Network-specific Rules (IP, UUID, etc.)

For network-related validations, use the Wscore\LeanValidator\Rule\Net class.

Available rules in Net class:

Date-specific Rules (HTML date/time, etc.)

For HTML date/time formats and date constraints, use the Wscore\LeanValidator\Rule\Date class.

Available rules in Date class:

You can also create a Rules subclass for better IDE support:

Rules added with addRule() or by merging callables into $this->rules in the constructor (same shape as Sanitizer::$rules: fn ($value) => bool, e.g. 'ip' => fn ($v) => is_string($v) && filter_var($v, FILTER_VALIDATE_IP) !== false) work with apply('name') and __call, but will not show in IDE completion unless you add a corresponding @method on your Rules subclass.

API Reference

Validator::make(array|string|numeric $data): static

Creates a validator. If a string or number is passed, it treats it as a single item to be validated.

field(string $key, ?string $errorMsg = null): static

Specifies the field to validate. Optionally sets a default error message for any rule in the chain.

required(?string $msg = null): static

Marks the field as required.

optional(mixed $default = null): static

Marks the field as optional. If the field is missing, it will be set to the default value.

requiredIf(string $otherKey, mixed $expect, ?string $msg = null, mixed $elseOverwrite = null): static

Marks the field as required if the value of $otherKey matches $expect.

requiredWith(string $otherKey, ?string $msg = null, mixed $elseOverwrite = null): static

Marks the field as required if $otherKey exists.

requiredWithout(string $otherKey, ?string $msg = null, mixed $elseOverwrite = null): static

Marks the field as required if $otherKey does not exist.

requiredWhen(callable $call, ?string $msg = null, mixed $elseOverwrite = null): static

Marks the field as required if $call($data) returns true.

isValid(): bool

Returns true if there are no validation errors.

getValidatedData(): array

Returns the validated data. Throws RuntimeException if validation failed.

getErrorsFlat(): array

Returns a flat array of error messages where keys are the field names.

addError(string $fieldName, string $errorMessage): static

Manually adds an error message to a specific field. Useful for errors that cannot be expressed with validation rules.

Sanitizer Features

The Sanitizer class provides various methods to clean and transform input data.

Default Sanitization

Built-in Rules

Use these methods to apply specific transformations:

Skipping Sanitization

Dot-Notation and Wildcards

You can target nested data using dot-notation and wildcards.

Adding Custom Rules

You can add your own sanitization rules using addRule().

Data Sanitization

By default, the Validator can sanitize input data using the Sanitizer class. To apply sanitization, you must explicitly call the sanitize() method before validation.

By default, the Sanitizer converts strings to UTF-8 and trims surrounding whitespace. If you don't call sanitize(), the validation will be performed on the raw input data.


All versions of leanvalidator with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
ext-mbstring Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package wscore/leanvalidator contains the following files

Loading the files please wait ...