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

Validator object appears to keep its validation status #385

Closed
CMCDragonkai opened this issue Mar 16, 2017 · 7 comments
Closed

Validator object appears to keep its validation status #385

CMCDragonkai opened this issue Mar 16, 2017 · 7 comments

Comments

@CMCDragonkai
Copy link

CMCDragonkai commented Mar 16, 2017

I'm using 5.1 version.

<?php

use JsonSchema\Validator;

$validator = new Validator;

$data = (object)['key' => true];

$validator->validate(
    $data,
    (object)[
        'type' => 'object',
        'properties' => (object)[
            'key' => (object)[
                'type' => 'boolean'
            ]
        ]
    ]
);

var_dump($validator->isValid()); // true

$data = ['key' => true];

$validator->validate(
    $data,
    (object)[
        'type' => 'object',
        'properties' => (object)[
            'key' => (object)[
                'type' => 'boolean'
            ]
        ]
    ]
);

var_dump($validator->isValid()); // false


$data = (object)['key' => true];

$validator->validate(
    $data,
    (object)[
        'type' => 'object',
        'properties' => (object)[
            'key' => (object)[
                'type' => 'boolean'
            ]
        ]
    ]
);

var_dump($validator->isValid()); // false but should be true

There should be a way to clear the Validator status? Or that subsequent invocations of validate should not keep the original validation status.

@erayd
Copy link
Contributor

erayd commented Mar 16, 2017

@CMCDragonkai Thanks for letting us know - will take a look now, stand by.

@erayd
Copy link
Contributor

erayd commented Mar 16, 2017

@CMCDragonkai It's already possible to reset the validation status - just use Validator::reset():

<?php

use JsonSchema\Validator;

require('vendor/autoload.php');
$v = new Validator();

$validSchema = json_decode('{"type":"string"}');
$invalidSchema = json_decode('{"type":"object"}');
$data = json_decode('"ABC"');

$v->validate($data, $invalidSchema);
assert(!$v->isValid());

$v->reset(); // This line resets isValid

$v->validate($data, $validSchema);
assert($v->isValid());

For the sake of usability, I have added this call in PR #386 , which will allow object reuse without needing to manually reset it.

@bighappyface
Copy link
Collaborator

See #390 - The built-in reset will be official in 6.0.0

@aboyton
Copy link

aboyton commented Feb 11, 2018

Can we maybe update the documentation for 5.x while we wait for 6.x. I've just spent a long while trying to work out what was going wrong digging through the source to find this, especially as the current docs don't mention reset at all.

@bighappyface
Copy link
Collaborator

PRs welcome

@erayd
Copy link
Contributor

erayd commented Feb 12, 2018

@aboyton The main problem is that this project is woefully short of manpower, and contains a massive amount of legacy code that dates to well before any of us were involved with it. There aren't many active contributors, and none of us have much time available. In order to update the documentation, somebody would need to commit the time to do it. It's on my list, but there are other things that are higher priority than writing documentation for 5.x, and I don't have much time available at present.

As @bighappyface mentioned above, PRs are always welcome - if you would like to submit documentation for 5.x, we'd love to have it. Otherwise, regrettably, there will be something of a wait.

In the meantime, if you have specific questions, please feel free to ask, and I'll do my best to assist you. I'm very aware that the documentation is significantly lacking. You may also wish to refer to #440 - this PR is a work in progress that adds better PHPDoc comments to the public API. It's only partly complete, but hopefully it helps a little in the interim.

aboyton pushed a commit to aboyton/json-schema that referenced this issue May 23, 2018
aboyton pushed a commit to aboyton/json-schema that referenced this issue May 23, 2018
@aboyton
Copy link

aboyton commented May 23, 2018

Sorry I realised that I never replied to this nor made a pull request. Thanks for your work on this, I've made a small pull request with a sentence added for calling reset.

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

4 participants