Skip to content

Latest commit

 

History

History
87 lines (62 loc) · 2.3 KB

FloatSchema.md

File metadata and controls

87 lines (62 loc) · 2.3 KB

FloatSchema

The FloatSchema validates floating-point values with numeric constraints.

Basic Usage

use Chubbyphp\Parsing\Parser;

$p = new Parser();

$schema = $p->float();

$data = $schema->parse(4.2); // Returns: 4.2

Validations

Comparison Constraints

$schema->minimum(5.0);  // Greater than or equal to 5.0
$schema->exclusiveMinimum(5.0);   // Greater than 5.0
$schema->exclusiveMaximum(10.0);  // Less than 10.0
$schema->maximum(10.0); // Less than or equal to 10.0
$schema->multipleOf(0.5); // Multiple of 0.5

multipleOf uses exact decimal math (following the JSON Schema specification: division must result in an integer), not a binary floating point comparison. This means 0.3 is a multiple of 0.1, while 0.30000000000000004 (e.g. the result of 0.1 + 0.2) is not. Requires the bcmath extension.

Conversions

$schema->toInt();    // Convert to integer (truncates decimal)
$schema->toString(); // Convert to string

Common Patterns

Price Validation

$priceSchema = $p->float()->minimum(0.0);

$priceSchema->parse(19.99); // Returns: 19.99
$priceSchema->parse(0.0);   // Returns: 0.0
$priceSchema->parse(-5.0);  // Throws: minimum validation error

Percentage

$percentageSchema = $p->float()->minimum(0.0)->maximum(100.0);

$percentageSchema->parse(75.5); // Returns: 75.5
$percentageSchema->parse(-1.0); // Throws: minimum validation error
$percentageSchema->parse(150.0); // Throws: maximum validation error

Coordinates

$latitudeSchema = $p->float()->minimum(-90.0)->maximum(90.0);
$longitudeSchema = $p->float()->minimum(-180.0)->maximum(180.0);

$coordinatesSchema = $p->object([
    'lat' => $latitudeSchema,
    'lng' => $longitudeSchema,
]);

$coordinatesSchema->parse(['lat' => 47.1, 'lng' => 8.2]);

Error Codes

Code Description
float.type Value is not a float
float.minimum Value is not greater than or equal to threshold
float.exclusiveMinimum Value is not greater than to threshold
float.exclusiveMaximum Value is not less than to threshold
float.maximum Value is not less than or equal to threshold
float.multipleOf Value is not a multiple of threshold
float.int Cannot convert float to int without precision loss (for toInt())