Skip to content

Draft-06/07/2019-09 constraints keep stale checkMode when a Validator is reused #977

Open

Description

@DannyvdSluijs

Summary

When a Validator is reused, checkMode flags passed to Validator::validate() after the first call are not seen by the Draft-06, Draft-07 and Draft 2019-09 constraints. Those constraints keep the config from the first time they were created.

Cause

Draft06Constraint, Draft07Constraint and Draft2019Constraint each build their own draft Factory in the constructor and copy the parent's config into it as a plain int:

parent::__construct(new Factory(
    $factory ? $factory->getSchemaStorage() : new SchemaStorage(),
    $factory ? $factory->getUriRetriever() : new UriRetriever(),
    $factory ? $factory->getConfig() : Constraint::CHECK_MODE_NORMAL // copied by value
));

The root factory caches that draft constraint in instanceCache, and createInstanceFor() returns a shallow clone, so all clones share the same draft factory. Validator::validate() only calls setConfig() on the root factory, so the draft factory, and every constraint it creates, keeps the config from the first call.

Affected:

  • src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php
  • src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php
  • src/JsonSchema/Constraints/Drafts/Draft2019/Draft2019Constraint.php

Reproduction

The value "not-a-date" against a Draft-07 schema with "format": "date-time", using a single Validator instance (with the CHECK_MODE_DISABLE_FORMAT support from #972):

Call order Second call should be Second call actually is
STRICT | DISABLE_FORMAT, then STRICT invalid valid (format skipped)
STRICT, then STRICT | DISABLE_FORMAT valid invalid (format still checked)

The same happens for Draft-06 and Draft 2019-09. This applies to any checkMode flag read through the draft factory, not only CHECK_MODE_DISABLE_FORMAT. The first row fails open: invalid data passes silently.

Possible fix

Make the draft factory read the root factory's config live instead of copying it. For example, keep a reference to the parent factory and delegate getConfig()/setConfig() to it, or share the config through an object. An integration test should call Validator::validate() twice on the same instance, with and without a flag, for each draft.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions