Repository navigation
Conversation
Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
Roman3349
force-pushed
the
fix/833-invalid-schema-type
branch
from
December 4, 2026 14:37
cff7dd6 to
28133b2
Compare
6 of 12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
A schema passed to
Validator::validate()as a string, for example a JSON string that was not decoded, or as another scalar was accepted without any error, and every value passed validation. How such a schema failed depended on the check mode:CHECK_MODE_STRICTCHECK_MODE_VALIDATE_SCHEMA'{"type": "integer"}','foo'RuntimeException5,1.5TypeErrorfromproperty_exists()RuntimeExceptionnullInvalidArgumentExceptionTypeErrorfromproperty_exists()InvalidArgumentExceptionAs discussed in the issue, a schema is an object or a boolean, plus an associative array, which this library accepts as well. Decoding a JSON string would not be compliant, so this throws instead.
Changes:
Validator::validate(): throwsInvalidArgumentException(The schema must be an object, an array or a boolean, string given) for any other type, in all check modes.nullkeeps its meaning, the default mode still falls back to an inline schema in the data.Validator::validate(): casts an array schema to an object before it is stored.SchemaStorage::addSchema()did the same already, this only keeps the type within whatSchemaStorageInterface::addSchema()declares.Validator::validate(): in strict mode, anullschema throws the sameInvalidArgumentException(no schema found to verify against) as in the default mode, instead of aTypeError. The strict mode has no inline schema to fall back to.Related Issue
Fixes #833
Type of Change
Checklist
Additional Notes
With
CHECK_MODE_VALIDATE_SCHEMA, a string schema threw aRuntimeException(Cannot validate the schema of a non-object) before, and throws anInvalidArgumentExceptionnow, soSchemaValidationTest::testNonObjectSchemais updated. Both implementJsonSchema\Exception\ExceptionInterface.In the issue, a check mode for this was suggested as well. I went with throwing in every mode, since a string schema can only be a mistake: it never validates anything, and the default mode throws for a missing schema already. If you prefer it behind a check mode, I can change it.