Open DannyBen opened 9 years ago
Try sending the data instead of json-encoded data to Jsv4. (Though I do agree it should not return valid)
Still no luck either way. The first test already has the data as non-json-encoded, and it returns valid even though the data is not an object. Test 2 without json_encode on the data also still returns valid.
For good measure, I also tried without having json_ecode on the schema, same result.
What worries me, is that it is difficult to get a "not valid" response. The opposite should be true...
Agreed.
So... why am I getting "valid"? What is wrong with the above tests?
Any news on this issue; got the same problem here ?
You are passing your schema as a string ( http://php.net/manual/en/function.json-encode.php - which return a string ) while the schema in the unit tests are passed as an object ( http://php.net/manual/en/function.json-decode.php - which returns a mixed type )
Take a look at https://github.com/geraintluff/jsv4-php/blob/master/test.php He first does a json_decode on his schema data JSON string:
$tests = json_decode(file_get_contents($filename));
and then uses that OBJECT to pass to the validate function
$result = Jsv4::validate($test->data, $test->schema);
Your print_r also suggests that the value of your schema is a string, not an object :
[schema:Jsv4:private] => {"type":"object"}
I'll give your example a try with the above modifications in mind.
Yup, if you change your code :
$schema = json_encode(["type" => "object"]);
to
$schema = json_decode(json_encode(["type" => "object"]));
The validation returns the expected output:
Jsv4 Object
(
[data:Jsv4:private] => asd
[schema:Jsv4:private] => stdClass Object
(
[type] => object
)
[firstErrorOnly:Jsv4:private] =>
[coerce:Jsv4:private] =>
[valid] =>
[errors] => Array
(
[0] => Jsv4Error Object
(
[code] => 0
[dataPath] =>
[schemaPath] => /type
[message] => Invalid type: string
[string:Exception:private] =>
[file:protected] => /Users/sponnet/Documents/projects/spikes/phpvalidator/jsv4.php
[line:protected] => 134
[trace:Exception:private] => Array
...etc...
p.s.
$schema = ["type" => "object"];
print_r($schema);
Array
(
[type] => object
)
While
$schema = (Object)["type" => "object"];
print_r($schema);
stdClass Object
(
[type] => object
)
So just casting your array to an object also works in your case.
This is definitely something simple I am missing, but I am unable to make the
validate
orisValid
methods returnfalse
.