geraintluff / jsv4-php

A (coercive) JSON Schema v4 Validator for PHP
Other
114 stars 31 forks source link

Always getting true when validating #18

Open DannyBen opened 9 years ago

DannyBen commented 9 years ago

This is definitely something simple I am missing, but I am unable to make the validate or isValid methods return false.

    // Test 1: 
    $data   = "asd";
    $schema = json_encode(["type" => "object"]);
    $result = Jsv4::validate($data, $schema);
    print_r($result);
    // Jsv4 Object
    // (
    //     [data:Jsv4:private] => asd
    //     [schema:Jsv4:private] => {"type":"object"}
    //     [firstErrorOnly:Jsv4:private] => 
    //     [coerce:Jsv4:private] => 
    //     [valid] => 1
    //     [errors] => Array()
    // )

    // Test 2: 
    $data   = json_encode(["name" => 23]);
    $schema = json_encode(["properties" => ["name" => ["type" => "string"]]]);
    $result = Jsv4::validate($data, $schema);
    print_r($result);
    // Jsv4 Object
    // (
    //     [data:Jsv4:private] => {"name":23}
    //     [schema:Jsv4:private] => {"properties":{"name":{"type":"string"}}}
    //     [firstErrorOnly:Jsv4:private] => 
    //     [coerce:Jsv4:private] => 
    //     [valid] => 1
    //     [errors] => Array()
    // )
rebra commented 9 years ago

Try sending the data instead of json-encoded data to Jsv4. (Though I do agree it should not return valid)

DannyBen commented 9 years ago

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...

rebra commented 9 years ago

Agreed.

DannyBen commented 9 years ago

So... why am I getting "valid"? What is wrong with the above tests?

DennisGryncewicz commented 9 years ago

Any news on this issue; got the same problem here ?

sponnet commented 9 years ago

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.

sponnet commented 9 years ago

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...
sponnet commented 9 years ago

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.