mirror of
https://github.com/ConnectedHumber/Air-Quality-Web
synced 2024-11-22 06:23:01 +00:00
Add is_min & is_max to validator
This commit is contained in:
parent
bbf3d02b5d
commit
7710331f1e
1 changed files with 32 additions and 1 deletions
|
@ -51,6 +51,37 @@ class Validator {
|
||||||
"Error: The field $key in your request isn't a number."
|
"Error: The field $key in your request isn't a number."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensures that the value associated with the given key is at most a given
|
||||||
|
* value.
|
||||||
|
* @param string $key The key to test the value of.
|
||||||
|
* @param int|float $max The maximum value allowed.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function is_max(string $key, $max) {
|
||||||
|
$this->add_test(
|
||||||
|
$key,
|
||||||
|
function($data) use($max) { return floatval($data) <= $max; },
|
||||||
|
400,
|
||||||
|
"Error: The field $key in your request must be at most $max."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Ensures that the value associated with the given key is at least a given
|
||||||
|
* value.
|
||||||
|
* @param string $key The key to test the value of.
|
||||||
|
* @param int|float $min The minimum value allowed.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function is_min(string $key, $min) {
|
||||||
|
$this->add_test(
|
||||||
|
$key,
|
||||||
|
function($data) use($min) { return floatval($data) >= $min; },
|
||||||
|
400,
|
||||||
|
"Error: The field $key in your request must be at least $min."
|
||||||
|
);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Checks that the value associated with the given key is a least a given
|
* Checks that the value associated with the given key is a least a given
|
||||||
* number of characters long.
|
* number of characters long.
|
||||||
|
|
Loading…
Reference in a new issue