The IntSchema validates integer values with numeric constraints.
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->int();
$data = $schema->parse(1337); // Returns: 1337$schema->minimum(5); // Greater than or equal to 5
$schema->exclusiveMinimum(5); // Greater than 5
$schema->exclusiveMaximum(10); // Less than 10
$schema->maximum(10); // Less than or equal to 10
$schema->multipleOf(5); // Multiple of 5
$schema->multipleOf(0.5); // Multiple of 0.5 (JSON Schema allows a non-integer multipleOf on integers)multipleOf uses exact decimal math (following the JSON Schema specification: division
by this keyword's value results in an integer), shared with FloatSchema.
$schema->toDateTime(); // Convert Unix timestamp to DateTimeImmutable
$schema->toFloat(); // Convert to float
$schema->toString(); // Convert to string$positiveSchema = $p->int()->exclusiveMinimum(0);
$positiveSchema->parse(42); // Returns: 42
$positiveSchema->parse(0); // Throws: exclusiveMinimum validation error
$positiveSchema->parse(-1); // Throws: exclusiveMinimum validation error$ageSchema = $p->int()->minimum(0)->maximum(150);
$ageSchema->parse(25); // Returns: 25
$ageSchema->parse(-1); // Throws: minimum validation error
$ageSchema->parse(200); // Throws: maximum validation error$offsetSchema = $p->int()->minimum(0);
$limitSchema = $p->int()->exclusiveMinimum(0)->maximum(100);
$paginationSchema = $p->object([
'offset' => $offsetSchema,
'limit' => $limitSchema,
]);$timestampSchema = $p->int()->exclusiveMinimum(0)->toDateTime();
$date = $timestampSchema->parse(1705744500);
// Returns: DateTimeImmutable instance for 2024-01-20T09:15:00+00:00| Code | Description |
|---|---|
int.type |
Value is not an integer |
int.minimum |
Value is not greater than or equal to threshold |
int.exclusiveMinimum |
Value is not greater than to threshold |
int.exclusiveMaximum |
Value is not less than to threshold |
int.maximum |
Value is not less than or equal to threshold |
int.multipleOf |
Value is not a multiple of threshold |