<?php $store = new SchemaStore(); $urlBase = "http://example.com/"; // Add internal $ref, and make sure it's resolved $url = $urlBase . "test-schema"; $schema = json_decode('{ "title": "Test schema", "properties": { "foo": { "$ref": "#/definitions/foo" } }, "definitions": { "foo": { "title": "foo" } } }'); $store->add($url, $schema); $schema = $store->get($url); if ($schema->properties->foo != $schema->definitions->foo) { throw new Exception('$ref was not resolved'); } // Add external $ref, and don't resolve it // While we're at it, use an array, not an object $schema = array("title" => "Test schema 2", "properties" => array("foo" => array('$ref' => "somewhere-else"))); $store->add($urlBase . "test-schema-2", $schema); $schema = $store->get($urlBase . "test-schema-2"); if (!$schema->properties->foo->{'$ref'}) { throw new Exception('$ref should still exist');
<?php $store = new SchemaStore(); $url = "http://example.com/test-schema"; $schema = json_decode('{ "title": "Test schema" }'); $store->add($url, $schema); if (!recursiveEqual($store->get($url), $schema)) { throw new Exception("Not equal"); } if (!recursiveEqual($store->get($url . "#/title"), $schema->title)) { throw new Exception("Not equal"); }
<?php $store = new SchemaStore(); $urlBase = "http://example.com/"; $url = $urlBase . "test-schema"; $schema = json_decode('{ "title": "Test schema", "properties": { "foo": { "id": "#foo" }, "bar": { "id": "/bar" }, "baz": { "id": "?baz=1" }, "foobar": { "id": "test-schema/foobar" }, "nestedSchema": { "id": "/test-schema/foo", "nested": { "id": "#bar" } }, "testSchemaFoo": { "id": "/test-schema-foo" }, "somewhereElse": { "id": "http://somewhere-else.com/test-schema"
/** * Tests the JSON crossword data * * @param string $json Raw json string. * @param array(string) &$msg By reference. List of debug error messages. * * @return mixed { * Crossword metadata or false if not valid. * * @type string $name Crossword name. * @type int $name Difficulty level. * } */ function crw_verify_json($json, &$msg) { $easy_directions = array('right', 'down'); include 'schema/jsv4.php'; include 'schema/schema-store.php'; include 'l10n.php'; //schema loading $raw_schema = json_decode(file_get_contents(CRW_PLUGIN_DIR . 'schema/schema.json')); $url = $raw_schema->id; $store = new SchemaStore(); $store->add($url, $raw_schema); $schema = $store->get($url); $locale_data = crw_get_locale_data(); $schema->definitions->word->properties->letter->pattern = $locale_data["letterRegEx"]; // json string decoding try { $crossword = json_decode($json); } catch (Exception $e) { $msg = array('decode exception'); return false; } // schema validation $answer = Jsv4::validate($crossword, $schema); if (!$answer->valid) { $msg = array('schema error:'); foreach ($answer->errors as $err) { array_push($msg, $err->dataPath . " " . $err->message); } return false; } // verify width and height are consistent if ($crossword->size->height !== count($crossword->table)) { $msg = array('height inconsistency'); return false; } foreach ($crossword->table as $line) { if ($crossword->size->width !== count($line)) { $msg = array('width inconsistency'); return false; } } foreach ($crossword->words as $key => $word) { // verify keys match ID content if ((int) $key !== $word->ID) { $msg = array('word key inconsistency'); return false; } // verify word lengths are consistent with start/stop positions $computed_length = max(abs($word->stop->x - $word->start->x), abs($word->stop->y - $word->start->y)) + 1; if ($computed_length !== count($word->fields)) { $msg = array('word length inconsistency'); return false; } // verify direction restriction by level if (!($crossword->level & 1) && !in_array($word->direction, $easy_directions)) { $msg = array('word level and direction inconsistency'); return false; } // even more you could test: // direction fits start/stop position // each letter is in the right position } return array('name' => $crossword->name, 'level' => $crossword->level); }
#!/usr/bin/php5 <?php require __DIR__ . "/vendor/autoload.php"; $schemaStore = new SchemaStore(); foreach (glob(__DIR__ . "/../resources/openeyes/schema/*.json") as $schemaPath) { $schemaStore->add("oe:" . str_replace(".json", "", basename($schemaPath)), json_decode(file_get_contents($schemaPath))); } $testVal = (object) array("numerator" => 6, "denominator" => 6); var_dump(Jsv4::validate($testVal, $schemaStore->get("oe:VisualAcuityValueSnellenMetre")));