Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation : Is the readme example actually working ? #299

Closed
etiennechabert opened this issue Aug 22, 2016 · 18 comments
Closed

Documentation : Is the readme example actually working ? #299

etiennechabert opened this issue Aug 22, 2016 · 18 comments

Comments

@etiennechabert
Copy link

I am using your library for few days and I struggled a bit at the beginning to make it work with a very basic case.

I did my best to follow your example, but it was not really helping :

$data = json_decode(file_get_contents('data.json'));

// Validate
$validator = new JsonSchema\Validator;
$validator->check($data, (object)['$ref' => 'file://' . realpath('schema.json')]);

I created this stackoverflow post with my issue, please feel free to read it : http://stackoverflow.com/questions/39042346/json-schema-required-properties-ignored/39048262?noredirect=1#comment65497165_39048262

The answer was not really helping, for this reason I finally execute my code and your library step-by-step to understand what was going on. To be honest I don't understand how your example could work. After debugging your code, I am not able to tell where you are loading the JSON schema.

Inside the UndefinedConstraint:validateCommonProperties function, where you are checking if required properties are in, my schema is not loaded yet.

As you can see in the stackoverflow post, I finally make it work by loading the schema myself.

I created this ticket because I get quite confused by your example, I am definitely missing something about it... Is it possible for you to add more details on how dummies like me are supposed to use it ;)

Best regards,

@bighappyface
Copy link
Collaborator

PRs are welcome.

I am not sure why you sent to SO first, but you are at the right place now.

@etiennechabert
Copy link
Author

Well I was looking for help to solve my issue, in my mind I was doing something wrong.

After being able to solve it and found where the problem was coming from, I am now felling a bit more confident to create an issue here.

@bighappyface
Copy link
Collaborator

I see. You and all are welcome to raise an issue. We may not be the most responsive bunch, but we we are friendly and want to help.

@etiennechabert
Copy link
Author

Glad to hear that @bighappyface, according to what you read here and in SO, do know where I messed up ? The example you are giving on the readme is supposed to work out of the box ?

More basically I am looking for some documentation / resources about this project, describing the different features. I did find the 'docs' folder after cloning the repo, but there is nothing really new in comparison with the actual readme.

Thank's for your help.

@bighappyface
Copy link
Collaborator

@etiennechabert I have not had a chance to dive in yet. I'll try to get back to you in the next week or so (super busy with work and school).

Thanks for your patience.

@miwels
Copy link

miwels commented Aug 26, 2016

Hi there,

I have followed your stackoverflow thread and, even though I can see my schema and data being loaded, the 'check' method returns NULL.

Any idea of why is this happening? I assumed that if there was something wrong loading the data and/or schema an Exception would be thrown but I'm just getting a NULL value so I cannot use any if the isValid or getErrors methods.

My code is as follows:

<?php
namespace Tests\Validator;

use JsonSchema\Validator;

class ValidatorTest {

    protected $validator;
    protected $sampleJson;

    public function __construct() {
        $this->validator = new Validator();
        $this->sampleJson = json_decode($this->generateSampleData());
        $this->schema = $this->getSchema(realpath("schema.json"));
    }

    public function run()
    {
        $validator = new \JsonSchema\Validator;
        $result = $validator->check($this->sampleJson, $this->schema);

        // at this point $result is NULL so none of the following functions can be used

        if($result->isValid()) {
            print "JSON data matches schema";
        }
        else {
            echo "JSON data does not match the schema";
            foreach($this->validator->getErrors() as $error) {
                echo sprintf("[%s] %s\n", $error['property'], $error['message']);
            }
        }
    }

    protected function getSchema($filePath) {
        return json_decode(file_get_contents($filePath));
    }

    protected function generateSampleData() {
        // return some sample json
    }

@etiennechabert
Copy link
Author

Hey Buddy,

You should ignore the return value from the check function.

After the check just focus on $validator->isValid() which is going to return a boolean value.

If you are interrested by an array of errors you should try $validator->getErrors().

Good luck ;-)

@miwels
Copy link

miwels commented Aug 26, 2016

Thanks for your reply @etiennechabert

Unfortunately isValid() returns always true no matter what I pass in. I tried to add some garbage to the data and isValid() is still returning "true".

Edit: okay, now it's validating the JSON properly. I think there was a problem with my schema definition :) thanks again for your help @etiennechabert

@etiennechabert
Copy link
Author

Got you, so you are in the same situation than I was.

Could you please give me the print_r output for your json_data and json_schema ?

@miwels
Copy link

miwels commented Aug 26, 2016

The problem (I think) is that json_decode() already parses and validates the JSON. If there are any syntax errors it will return "null", and if that's the case the validator will return "true". I don't know if this is a bug or a feature but maybe they author should look into that.

@etiennechabert
Copy link
Author

json_schema role is not to check your JSON syntax validity, but to check the schema of the provided data. For example data_type, mendatory_value, extra properties and so on.

For this reason you should first provide a correct JSON before trying to check his schema

@bighappyface
Copy link
Collaborator

To be clear, these tests are being performed against 3.0.0?

Are you encountering similar behavior on 2.0.5?

@stamster
Copy link

I'm using 2.0.x and it just doesn't work. isValid() always returns bool true. It just doesn't care about any data you'll pass to it.

Source code has this comment:

 /**
     * Validates the given data against the schema and returns an object containing the results
     * Both the php object and the schema are supposed to be a result of a json_decode call.
     * The validation works as defined by the schema proposal in http://json-schema.org
     *

I don't see how this code is suppose to work, it lacks the very basic function - to actually load json schema from a file and to compare it with in memory object (decoded json).

I guess no one uses this package, that's why we are discussing such a basic issue here. It just does not work, plain and simple. Sorry.

@etiennechabert
Copy link
Author

@stamster It would be interesting to have a look at the source code leading to a isValid() always returning true, you probably failed somewhere.

Anyway I agree with you : the README exemple doesn't work and probably need to be fixed, but the library do work and I am actually using it to check user's input on a WS actually in production.

@stamster
Copy link

stamster commented Sep 19, 2016

I just used official example posted here on GH. If that fails (actually validates), w/o any error reported back - user cannot be doing anything wrong, it's the app which fails (i.e. broken).

BTW, I took a 5 mins to try latest 3.0.x version, and guess what - it works. Documentation suggests 2.0 branch, which is obviously wrong and not working.

That's the conclusion - by just upgrading to the new branch my code (official example) runs fine, so 2.0.x is totally broken.

Nevertheless, I already have switched to another lib, which is, IMHO, much more cleaner.

@etiennechabert thanks for reply!

@stamster
Copy link

I have created new PR for README file change.

@shmax
Copy link
Collaborator

shmax commented Oct 27, 2016

@bighappyface Time to close this one?

@bighappyface
Copy link
Collaborator

Closing for now, can re-open if needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants