You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Draft-06/07/2019-09 constraints keep stale checkMode when a Validator is reused #977
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(newFactory(
$factory ? $factory->getSchemaStorage() : newSchemaStorage(),
$factory ? $factory->getUriRetriever() : newUriRetriever(),
$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.
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