Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ $validator->
isLen($length) // The string must be the exact length
isLen($min, $max) // The string must be between $min and $max length (inclusive)
isInt() // Check for a valid integer
isBetween($min, $max) // The number must be between $min and $max (inclusive)
isFloat() // Check for a valid float/decimal
isEmail() // Check for a valid email
isUrl() // Check for a valid URL
Expand Down
3 changes: 3 additions & 0 deletions src/Klein/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public static function addDefault()
static::$methods['int'] = function ($str) {
return (string)$str === ((string)(int)$str);
};
static::$methods['between'] = function ($str, $min, $max) {
return (real)$str >= $min && (real)$str <= $max;
};
static::$methods['float'] = function ($str) {
return (string)$str === ((string)(float)$str);
};
Expand Down
58 changes: 58 additions & 0 deletions tests/Klein/Tests/ValidationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,64 @@ function ($request, $response, $service) {
);
}

public function testBetween()
{
$this->klein_app->respond(
'/[:test_param]',
function ($request, $response, $service) {
$service->validateParam('test_param')
->notNull()
->isBetween(0, 5);

// We should only get here if we passed our validations
echo 'yup!';
}
);

$this->assertSame(
'fail',
$this->dispatchAndReturnOutput(
MockRequestFactory::create('/-1')
)
);
$this->assertSame(
'yup!',
$this->dispatchAndReturnOutput(
MockRequestFactory::create('/0')
)
);
$this->assertSame(
'yup!',
$this->dispatchAndReturnOutput(
MockRequestFactory::create('/3')
)
);
$this->assertSame(
'yup!',
$this->dispatchAndReturnOutput(
MockRequestFactory::create('/5')
)
);
$this->assertSame(
'yup!',
$this->dispatchAndReturnOutput(
MockRequestFactory::create('/3.0')
)
);
$this->assertSame(
'yup!',
$this->dispatchAndReturnOutput(
MockRequestFactory::create('/2e0')
)
);
$this->assertSame(
'fail',
$this->dispatchAndReturnOutput(
MockRequestFactory::create('/10,000')
)
);
}

public function testFloat()
{
$this->klein_app->respond(
Expand Down