Migrate to opis/json-schema v2#24
Open
dcgoodwin2112 wants to merge 2 commits into
Open
Conversation
- Replace v1 Schema::fromJsonString with explicit json_decode + InvalidSchemaException - Replace v1 getFirstError/keywordArgs with v2 error()/args(), walking subErrors() to a leaf - Add local RootedData\Exception\InvalidSchemaException to replace the dropped opis class - Update tests to import the local exception and catch the v2 SchemaException interface All 21 existing tests pass against opis/json-schema 2.6.0.
- Reject non-object, non-boolean schemas at construction with clear errors
(matches JSON Schema spec and v1 fromJsonString semantics)
- Pre-decode schema in validate() so v2 never URI-parses bare strings —
fixes regression on boolean schemas (true/false) and avoids any
unintended HTTP fetches for URL-shaped schemas
- Use ErrorFormatter::formatErrorMessage in the non-type fallback so v2
message templates ({missing}, etc.) are properly interpolated
- Add tests for boolean schemas, schema type rejection, and the
fallback message format
8 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.
Summary
Migrate to
opis/json-schemav2 (^2.4). v1 has been unmaintained for years and is increasingly hard to keep on modern PHP/Drupal stacks. This is a coordinated upgrade with downstream DKAN core (PR GetDKAN/dkan#4706).What changed
opis/json-schemabumped from v1 to^2.4.RootedData\Exception\InvalidSchemaException(extends\InvalidArgumentException). Replaces references to v1'sOpis\JsonSchema\Exception\InvalidSchemaException, which no longer exists.RootedJsonData::__construct): tightened to accept onlyis_object($schema) || is_bool($schema). v1'sSchema::fromJsonString()quietly accepted bare strings; v2 has no analogous helper, so we require pre-parsed schemas (matching the JSON Schema spec, where a schema is an object or boolean).validate(): pre-decodes the schema viajson_decode($schema, false)before passing toValidator::validate(). v2's URI-first parse path would otherwise treat URL-shaped strings as remote refs, and bare booleans as URIs — both regressions vs. v1.properties); leaf errors live undererror()->subErrors(). The path-builder now walks to a leaf so error pointer + args remain meaningful.Opis\JsonSchema\Errors\ErrorFormatter::formatErrorMessage()is used for the non-type fallback so v2 message templates (e.g.{missing}) are interpolated rather than emitted verbatim.Breaking changes for consumers
json_decode(..., false)firstOpis\JsonSchema\Schemais now an interfaceOpis\JsonSchema\Exception\InvalidSchemaExceptionno longer existsRootedData\Exception\InvalidSchemaExceptionJSON Schema draft compatibility
opis/json-schemav2 ships parsers for draft-06, draft-07, 2019-09, and 2020-12. Draft-04 is not supported — schemas declaring"$schema": "http://json-schema.org/draft-04/schema#"will throwOpis\JsonSchema\Exceptions\ParseExceptionat validation time.Why this matters for downstream consumers
Many existing schemas in the wild — including DKAN's own
dataset/distribution/publisherschemas, derived from Project Open Data v1.1 — were authored against draft-04. These need to be migrated.Migration cookbook
For each schema declaring draft-04:
Bump
$schemadeclaration:Rename
id→$id(the keyword was renamed at the draft-04 → draft-06 boundary):Use absolute URIs for
$idvalues. v2 rejects relative URIs at the schema root. Pick a stable namespace your project owns (https://schemas.example.com/...).exclusiveMinimum/exclusiveMaximum(if used): change from boolean modifier to numeric value. DKAN's schemas didn't use these, but custom schemas might:Why draft-07 (and not draft-06)?
The
$idrename happens at the draft-04 → draft-06 boundary, so picking draft-06 instead of draft-07 would not have spared us the renaming work. Draft-07 additionally provides keywords DKAN actually uses (readOnly), so it's the natural target. Drafts 2019-09 and 2020-12 introduced further keyword splits (dependentRequired/dependentSchemas,$defsoverdefinitions) that would have required broader rewrites without functional benefit.Test plan
opis/json-schema 2.6.0(was 21 tests on v1; 4 new tests cover boolean schemas, schema-type rejection, and the formatErrorMessage fallback path)composer installclean on PHP 8.1+Related
Co-authored-by: Copilot copilot@github.com