Skip to content

fix: Throw for a schema that is not an object, an array or a boolean - #1008

Open
Roman3349 wants to merge 1 commit into
jsonrainbow:mainfrom
Roman3349:fix/833-invalid-schema-type
Open

Roman3349 wants to merge 1 commit into
jsonrainbow:mainfrom
Roman3349:fix/833-invalid-schema-type

Conversation

@Roman3349

Copy link
Copy Markdown
Contributor

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:

Schema Default CHECK_MODE_STRICT CHECK_MODE_VALIDATE_SCHEMA
'{"type": "integer"}', 'foo' valid valid RuntimeException
5, 1.5 valid TypeError from property_exists() RuntimeException
null InvalidArgumentException TypeError from property_exists() InvalidArgumentException

As 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(): throws InvalidArgumentException (The schema must be an object, an array or a boolean, string given) for any other type, in all check modes. null keeps 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 what SchemaStorageInterface::addSchema() declares.
  • Validator::validate(): in strict mode, a null schema throws the same InvalidArgumentException (no schema found to verify against) as in the default mode, instead of a TypeError. The strict mode has no inline schema to fall back to.

Related Issue

Fixes #833

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Other (please describe):

Checklist

  • I have read the CONTRIBUTING guidelines
  • My code follows the code style of this project
  • I have added tests that prove my fix is effective or that my feature works
  • All new and existing tests pass
  • I have updated the documentation accordingly
  • My changes generate no new warnings

Additional Notes

With CHECK_MODE_VALIDATE_SCHEMA, a string schema threw a RuntimeException (Cannot validate the schema of a non-object) before, and throws an InvalidArgumentException now, so SchemaValidationTest::testNonObjectSchema is updated. Both implement JsonSchema\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.

Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validator considers the validation result valid if the schema is passed as a string value.

2 participants