laminas / laminas-validator

Validation classes for a wide range of domains, and the ability to chain validators to create complex validation criteria

Home Page:https://docs.laminas.dev/laminas-validator/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GpsPoint validator needs a rewrite because it returns true for the input "foo, bar"

froschdesign opened this issue · comments

I believe this validator should either be rewritten or removed entirely as it will return true for the input "foo, bar"

It expects a string as input in the form "latitude, longitude" :

    public function isValid($value)
    {
        if (strpos($value, ',') === false) {
            $this->error(self::INCOMPLETE_COORDINATE, $value);
            return false;
        }

        [$lat, $long] = explode(',', $value);

but the isValidCoordinate() method blindly casts the supposed lat or long to a double:

        $doubleLatitude = (double) $value;

        if ($doubleLatitude <= $maxBoundary && $doubleLatitude >= $maxBoundary * -1) {
            return true;
        }

which results in a 0 value in the case of non-numerical strings, which then passes validation as a valid lat/long.

At the very least it should have explicit comments stating that the expected input is in a very particular format already.

Originally posted by @ajgale in #69 (comment)

Deprecation would be best then.

I have checked it in the unit tests and confirm the problem.

Another option would be to check if a number is included before conversion to double is done:

$doubleLatitude = (double) $value;
if ($doubleLatitude <= $maxBoundary && $doubleLatitude >= $maxBoundary * -1) {
return true;
}
$this->error(self::OUT_OF_BOUNDS);
return false;

Yah, using is_numeric should be suitable here

@boesing

Yah, using is_numeric should be suitable here

This will fail with the current test cases because coordinates like 63 47 24.691 N are converted to 634724.691N. The error for "out of bounds" is not set.

public function errorMessageTestValues(): array
{
return [
['63 47 24.691 N, 18 2 54.363 W', GpsPoint::OUT_OF_BOUNDS, '63 47 24.691 N'],
['° \' " N,° \' " E', GpsPoint::CONVERT_ERROR, '° \' " N'],
['° \' " N', GpsPoint::INCOMPLETE_COORDINATE, '° \' " N'],
];
}