// 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'); } if (!recursiveEqual($store->missing(), array($urlBase . "somewhere-else"))) { throw new Exception('$store->missing() is not correct: ' . json_encode($store->missing()) . ' is not ' . json_encode(array($urlBase . "somewhere-else"))); }
<?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"); }
}, "nestedSchema": { "id": "/test-schema/foo", "nested": { "id": "#bar" } }, "testSchemaFoo": { "id": "/test-schema-foo" }, "somewhereElse": { "id": "http://somewhere-else.com/test-schema" } } }'); $store->add($url, $schema); if (!recursiveEqual($store->get($url . "#foo"), $schema->properties->foo)) { throw new Exception("#foo not found"); } if (!recursiveEqual($store->get($url . "?baz=1"), $schema->properties->baz)) { throw new Exception("?baz=1 not found"); } if (!recursiveEqual($store->get($url . "/foobar"), $schema->properties->foobar)) { throw new Exception("/foobar not found"); } if (!recursiveEqual($store->get($url . "/foo#bar"), $schema->properties->nestedSchema->nested)) { throw new Exception("/foo#bar not found"); } if ($store->get($urlBase . "bar")) { throw new Exception("/bar should not be indexed, as it should not be trusted"); }
/** * 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")));