Skip to content

Getting fragments from SchemaStorage by path #534

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
richardeaxon opened this issue Sep 26, 2018 · 1 comment
Closed

Getting fragments from SchemaStorage by path #534

richardeaxon opened this issue Sep 26, 2018 · 1 comment

Comments

@richardeaxon
Copy link

I have the following example schema:

{
    "definitions": {},
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://example.com/root.json",
    "type": "object",
    "title": "The Root Schema",
    "required": [
      "data",
      "test"
    ],
    "properties": {
      "data": {
        "$id": "#/properties/data",
        "type": "integer",
        "title": "The Data Schema",
        "default": 0,
        "examples": [
          123
        ]
      },
      "test": {
        "$id": "#/properties/test",
        "type": "object",
        "title": "The Test Schema",
        "required": [
          "blah"
        ],
        "properties": {
          "blah": {
            "$id": "#/properties/test/properties/blah",
            "type": "integer",
            "title": "The Blah Schema",
            "default": 0,
            "examples": [
              1
            ]
          }
        }
      }
    }
  }

Which I then load as per the following example:

$schemaStorage = new SchemaStorage();
$schemaStorage->addSchema('file://mySchema', $jsonSchemaObject);

I can then grab a fragment from the schema if I know the $id of a property:

print_r($schemaStorage->resolveRef('file://mySchema#/properties/test/properties/blah'));

How can I extract the fragment from the schema without knowing the $id but I know the path? For example getErrors() returns the path, not the ref or $id. For all schemas the path will exist if defined but the $id is optional.

See https://json-schema.org/understanding-json-schema/structuring.html#structuring

The reason for this requirement is that I am looking for a way to get access to user defined property keywords of a schema (which are allowed as per spec and correctly ignored by this implementation - see http://json-schema.org/latest/json-schema-core.html#rfc.section.6.4 )

Thanks.

@DannyvdSluijs
Copy link
Collaborator

@richardeaxon very late to the game, but still an answer though.

Using the following code snippet from the master branch:

<?php

use JsonSchema\Uri\UriRetriever;

require_once '../../vendor/autoload.php';

$retriever = new UriRetriever();
$schema = $retriever->retrieve('file://' . realpath('./schema.json'). '#/properties/test/properties/blah');

var_dump($schema);

You can check that the output contains what you were looking for:

object(stdClass)#11 (6) {
  ["$id"]=>
  string(33) "#/properties/test/properties/blah"
  ["type"]=>
  string(7) "integer"
  ["title"]=>
  string(15) "The Blah Schema"
  ["default"]=>
  int(0)
  ["examples"]=>
  array(1) {
    [0]=>
    int(1)
  }
  ["id"]=>
  string(100) "file:///Users/danny/Projects/OSS/json-schema/issues/534/schema.json#/properties/test/properties/blah"
}

In an attempt to cleanup this repo we are trying to filter the issues and see which ones might be closed. Is it safe to assume this is a rather old issue, which now has a answer, and the issue can be closed? Feel free to close it yourself with some comments if helpful.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants