Skip to content

Latest commit

 

History

History
94 lines (70 loc) · 4.01 KB

03-rules_type.md

File metadata and controls

94 lines (70 loc) · 4.01 KB

Type

Validates that a value is of a specific type.

If an array with multiple types is provided, it will validate if the value is of at least one of the given types. For example, if ['alpha', 'numeric'] is provided, it will validate if the value is of type alpha or of type numeric.

Type(
    string|array $constraint,
    ?string $message = null
);

Basic Usage

// single type
Validator::type('string')->validate('green'); // true
Validator::type('alphanumeric')->validate('gr33n'); // true

// multiple types
// validates if value is of at least one of the provided types
Validator::type(['alpha', 'numeric'])->validate('green'); // true (alpha)
Validator::type(['alpha', 'numeric'])->validate('33'); // true (numeric)
Validator::type(['alpha', 'numeric'])->validate('gr33n'); // false (not alpha nor numeric)

// class or interface type
Validator::type(\DateTime::class)->validate(new \DateTime()); // true
Validator::type(\DateTimeInterface::class)->validate(new \DateTime()); // true

Note

An UnexpectedValueException will be thrown when a constraint type, class or interface is invalid.

Options

constraint

type: string|array required

Type(s) to validate the input value type. Can validate instances of classes and interfaces.

If an array with multiple types is provided, it will validate if the value is of at least one of the given types. For example, if ['alpha', 'numeric'] is provided, it will validate if the value is of type alpha or of type numeric.

Available data type constraints:

Available character type constraints:

message

type: ?string default: The {{ name }} value should be of type {{ constraint }}, {{ value }} given.

Message that will be shown if input value is not of a specific type.

The following parameters are available:

Parameter Description
{{ value }} The current invalid value
{{ name }} Name of the invalid value
{{ constraint }} The valid type(s)

Changelog

  • 0.2.0 Created