public function execute() { if (!class_exists('JsonSchema\\Uri\\UriRetriever')) { $this->error('The JsonSchema library cannot be found, please install it through composer.', 1); } $path = $this->getArg(0); $data = json_decode(file_get_contents($path)); if (!is_object($data)) { $this->error("{$path} is not a valid JSON file.", 1); } if (!isset($data->manifest_version)) { $this->output("Warning: No manifest_version set, assuming 1.\n"); // For backwards-compatability assume 1 $data->manifest_version = 1; } $version = $data->manifest_version; if ($version !== ExtensionRegistry::MANIFEST_VERSION) { $schemaPath = dirname(__DIR__) . "/docs/extension.schema.v{$version}.json"; } else { $schemaPath = dirname(__DIR__) . '/docs/extension.schema.json'; } if ($version < ExtensionRegistry::OLDEST_MANIFEST_VERSION || $version > ExtensionRegistry::MANIFEST_VERSION) { $this->error("Error: {$path} is using a non-supported schema version, it should use " . ExtensionRegistry::MANIFEST_VERSION, 1); } elseif ($version < ExtensionRegistry::MANIFEST_VERSION) { $this->output("Warning: {$path} is using a deprecated schema, and should be updated to " . ExtensionRegistry::MANIFEST_VERSION . "\n"); } $retriever = new JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve('file://' . $schemaPath); $validator = new JsonSchema\Validator(); $validator->check($data, $schema); if ($validator->isValid()) { $this->output("{$path} validates against the version {$version} schema!\n"); } else { foreach ($validator->getErrors() as $error) { $this->output("[{$error['property']}] {$error['message']}\n"); } $this->error("{$path} does not validate.", 1); } }
/** * @expectedException JsonSchema\Exception\ResourceNotFoundException */ public function testResolvePointerFragmentNoArray() { $schema = (object) array('definitions' => (object) array('foo' => array('title' => 'foo')), 'title' => 'schema'); $retriever = new \JsonSchema\Uri\UriRetriever(); $retriever->resolvePointer($schema, 'http://example.org/schema.json#/definitions/foo'); }
public function datajson_schema($version = '') { $version_path = !empty($version) ? $version . '/' : ''; $path = './schema/' . $version_path . 'catalog.json'; // Get the schema and data as objects $retriever = new JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve('file://' . realpath($path)); $refResolver = new JsonSchema\RefResolver($retriever); $refResolver->resolve($schema, 'file://' . __DIR__ . '/../../schema/' . $version_path); return $schema; }
public function execute() { if (!class_exists('JsonSchema\\Uri\\UriRetriever')) { $this->error('The JsonSchema library cannot be found, please install it through composer.', 1); } $retriever = new JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve('file://' . dirname(__DIR__) . '/docs/extension.schema.json'); $path = $this->getArg(0); $data = json_decode(file_get_contents($path)); if (!is_object($data)) { $this->error("{$path} is not a valid JSON file.", 1); } $validator = new JsonSchema\Validator(); $validator->check($data, $schema); if ($validator->isValid()) { $this->output("{$path} validates against the schema!\n"); } else { foreach ($validator->getErrors() as $error) { $this->output("[{$error['property']}] {$error['message']}\n"); } $this->error("{$path} does not validate.", 1); } }
<?php include '../vendor/autoload.php'; $retriever = new JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve('file://' . realpath('schema2.json')); $refResolver = new JsonSchema\RefResolver($retriever); $refResolver->resolve($schema, 'file://' . __DIR__); $generator = new JsonSchemaForm\Generator($schema); ?> <html> <head> <!-- Foundation CSS framework (Bootstrap and jQueryUI also supported) --> <link rel="stylesheet" href="//cdn.jsdelivr.net/foundation/5.0.2/css/foundation.min.css"> <!-- Font Awesome icons (Bootstrap, Foundation, and jQueryUI also supported) --> <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css"> </head> <body> <?php echo $generator->render(array('action' => '#')); ?> <pre> <?php print_r($schema); ?> </pre> </body> </html> <form role="form"> <div class="form-group"> <label for="exampleInputEmail1">Email address</label>
public function parseSingleSchema($schemeSpec) { if ($schemeSpec === null) { throw new ParseException('scheme must not be empty'); } if (!is_string($schemeSpec)) { throw new ParseException('scheme must be a string'); } $schemaJson = json_decode($schemeSpec); if (json_last_error() == JSON_ERROR_NONE) { $schemeUri = $schemaJson->{'$schema'}; $retriever = new \JsonSchema\Uri\UriRetriever(); $scheme = $retriever->retrieve($schemeUri); $validator = new Validator(); $validator->check($schemaJson, $scheme); if ($validator->isValid()) { $arrayScheme = json_decode($schemeSpec, true); unset($arrayScheme['$schema']); return $arrayScheme; } else { foreach ($validator->getErrors() as $error) { echo sprintf("[%s] %s\n", $error['property'], $error['message']); } } } }
/** * @dataProvider providePassesValidation * @param string $path Path to thing's json file */ public function testPassesValidation($path) { $data = json_decode(file_get_contents($path)); $this->assertInstanceOf('stdClass', $data, "{$path} is not valid JSON"); $this->assertObjectHasAttribute('manifest_version', $data, "{$path} does not have manifest_version set."); $version = $data->manifest_version; if ($version !== ExtensionRegistry::MANIFEST_VERSION) { $schemaPath = __DIR__ . "/../../../docs/extension.schema.v{$version}.json"; } else { $schemaPath = __DIR__ . '/../../../docs/extension.schema.json'; } // Not too old $this->assertTrue($version >= ExtensionRegistry::OLDEST_MANIFEST_VERSION, "{$path} is using a non-supported schema version"); // Not too new $this->assertTrue($version <= ExtensionRegistry::MANIFEST_VERSION, "{$path} is using a non-supported schema version"); $retriever = new JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve('file://' . $schemaPath); $validator = new JsonSchema\Validator(); $validator->check($data, $schema); if ($validator->isValid()) { // All good. $this->assertTrue(true); } else { $out = "{$path} did pass validation.\n"; foreach ($validator->getErrors() as $error) { $out .= "[{$error['property']}] {$error['message']}\n"; } $this->assertTrue(false, $out); } }
function load_user_schema($user_schema_file = 'user_schema.json') { $retriever = new JsonSchema\Uri\UriRetriever(); $user_schema = $retriever->retrieve('file://' . realpath($user_schema_file)); $refResolver = new JsonSchema\RefResolver($retriever); $refResolver->resolve($user_schema, 'file://' . realpath($user_schema_file)); return $user_schema; }
/** * Get the Recommendation schema definition * * @param string $version * @return <array> */ public function datajson_schema($version = '') { $version_path = !empty($version) ? $version . '/' : ''; $path = './schema/' . $version_path . '/recommendation/single_entry.json'; // Get the schema and data as objects $retriever = new JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve('file://' . realpath($path)); return $schema; }
function load_poi_schema($poi_schema_file = 'poi_schema.json') { global $supported_components; $retriever = new JsonSchema\Uri\UriRetriever(); $poi_schema = $retriever->retrieve('file://' . realpath($poi_schema_file)); $supported_components = array_keys(get_object_vars($poi_schema->properties)); $refResolver = new JsonSchema\RefResolver($retriever); $refResolver->resolve($poi_schema, 'file://' . realpath($poi_schema_file)); return $poi_schema; }
function validateAgainstSchema($schema_name, $data) { $retriever = new JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve('file://' . __DIR__ . '/assets/' . $schema_name); $ref_resolver = new JsonSchema\RefResolver(new JsonSchema\Uri\UriRetriever()); $ref_resolver->resolve($schema); $validator = new JsonSchema\Validator(); $validator->check($data, $schema); return array('valid' => $validator->isValid(), 'errors' => $validator->getErrors()); }
/** * @param string $schemaName e.g. "MobilePayment" for /json/MobilePayment.json or "sale" for /json/sale.json */ function __construct($schemaName) { $this->schemaUri = 'file://' . self::schemaPathFor($schemaName); //var_dump('schemaUri', $this->schemaUri); $retriever = new \JsonSchema\Uri\UriRetriever(); $this->schema = $retriever->retrieve($this->schemaUri); // If you use '$ref' or 'extends' or if you are unsure, resolve those references here // This modifies the $schema object $refResolver = new \JsonSchema\RefResolver($retriever); $refResolver->resolve($this->schema, $this->schemaUri); }
/** * @return boolean */ public function isValid() { $retriever = new \JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve($this->schema); $refResolver = new \JsonSchema\RefResolver($retriever); // @see https://github.com/justinrainbow/json-schema/issues/180 $refResolver::$maxDepth = 20; $refResolver->resolve($schema, $this->refResolverPath); $this->validator = new \JsonSchema\Validator(); $this->validator->check($this->data, $schema); return $this->validator->isValid(); }
public function validate(\stdClass $json) { $retriever = new \JsonSchema\Uri\UriRetriever(); $validator = new \JsonSchema\Validator(); $resource = $this->schemafilepath; if (strpos($resource, ':') === false && file_exists($resource)) { $resource = "file://{$resource}"; } $validator->check($json, $retriever->retrieve($resource)); if (!$validator->isValid()) { throw new ValidationException('JSON does not validate', $validator); } return true; }
public static function validate($data, $schemaFile = '') { if (\Config::get('json-schema-validator::config.run') === false) { return true; } if (!empty($schemaFile)) { $retriever = new \JsonSchema\Uri\UriRetriever(); $path = \Config::get('json-schema-validator::config.schemaDir') . '/response/' . $schemaFile; $schema = $retriever->retrieve('file://' . $path); $validator = new \JsonSchema\Validator(); $validator->check($data, $schema); return $validator->isValid(); } return true; }
public function validate_json($json, $schemas_name, $schemas_path) { $retriever = new JsonSchema\Uri\UriRetriever(); $json_schemas = $retriever->retrieve('file://' . $schemas_path . "/{$schemas_name}"); $refResolver = new JsonSchema\RefResolver($retriever); $refResolver->resolve($json_schemas, 'file://' . $schemas_path . "/"); $validator = new JsonSchema\Validator(); $validator->check($json, $json_schemas); if ($validator->isValid()) { return false; } $details = ""; foreach ($validator->getErrors() as $error) { $details .= sprintf("[%s] %s\n", $error['property'], $error['message']); } return $details; }
public function setDefaultSchemaByLocation($location) { $retriever = new \JsonSchema\Uri\UriRetriever(); $this->schema = $retriever->retrieve($location); }
$savefolder = "saves"; $schemafile = "schema.json"; $returnmessage = new stdClass(); $jsonpostbody = file_get_contents('php://input'); // check query is not empty if (empty($jsonpostbody)) { $returnmessage->error = "POST body is empty"; } else { //convert json string to json object $data = json_decode($jsonpostbody); if (is_null($data)) { $returnmessage->error = "JSON does not decode"; } else { // load json-schema for PHP (https://github.com/justinrainbow/json-schema) require __DIR__ . '/vendor/autoload.php'; $retriever = new JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve('file://' . realpath($schemafile)); $validator = new JsonSchema\Validator(); $validator->check($data, $schema); // validate against schema if (!$validator->isValid()) { $returnmessage->error = "JSON does not validate to schema (serverside):"; foreach ($validator->getErrors() as $error) { $returnmessage->error .= " " . $error['message'] . ';'; } } else { // restringify the json object and get crc of the string $jsonstring = json_encode($data); $hash = dechex(crc32($jsonstring)); $filename = $savefolder . '/' . $hash . '.json'; $file = './' . $filename;
/** * @expectedException JsonSchema\Exception\UriResolverException */ public function testResolveExcessLevelUp() { $retriever = new \JsonSchema\Uri\UriRetriever(); $retriever->resolve('../schema.json#', 'http://example.org/schema.json#'); }
public function schema() { static $schema; if (!$schema) { $retriever = new \JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve('http://json-schema.org/hyper-schema'); (new \JsonSchema\RefResolver($retriever))->resolve($schema); unset($retriever); } return $schema; }
/** * Get the Recommendation schema definition * * @param string $component * @return <array> */ public function datajson_schema($component) { $path = './schema/' . $component . '.json'; // Get the schema and data as objects $retriever = new JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve('file://' . realpath($path)); return $schema; }
public function checkRequest() { $retriever = new JsonSchema\Uri\UriRetriever(); #$path = realpath('schemas/post_call.json'); $path = realpath($this->getSchemaPath()); $schema = $retriever->retrieve('file://' . $path); $refResolver = new JsonSchema\RefResolver($retriever); $refResolver->resolve($schema, 'file://' . __DIR__); $validator = new JsonSchema\Validator(); $data = json_decode(file_get_contents("php://input")); $validator->check($data, $schema); if ($validator->isValid()) { $this->valid = true; $this->params = (array) $data->request; $this->action = $data->request->action; } else { $this->errors = ["JSON does not validate"]; foreach ($validator->getErrors() as $error) { array_push($this->errors, sprintf("[%s] %s\n", $error['property'], $error['message'])); } } }
function _testComposerJSONSchema($filename) { // Get the schema and data as objects $retriever = new \JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve("file://" . __DIR__ . "/../composer-schema.json"); $data = json_decode(file_get_contents($filename)); // // If you use $ref or if you are unsure, resolve those references here // // This modifies the $schema object // $refResolver = new JsonSchema\RefResolver($retriever); // $refResolver->resolve($schema, 'file://' . __DIR__); // Validate $validator = new \JsonSchema\Validator(); $validator->check($data, $schema); $message = "(no message)"; $errors = $validator->getErrors(); if (count($errors) > 0) { $message = trim($errors[0]['property'] . " " . $errors[0]['message']); } if (!$validator->isValid() && $this->printComposerJSONSchemaErrors()) { foreach ($validator->getErrors() as $error) { echo sprintf("[%s] %s\n", $error['property'], $error['message']); } } $this->assertTrue($validator->isValid(), "File '{$filename}' was not valid Composer JSON according to composer-schema.json: {$message}"); }
public function datajson_schema($version = '') { $prefix = 'fitara'; if (!empty($version)) { if (substr($version, 0, strlen($prefix)) == $prefix) { $version_path = $prefix . '/' . substr($version, strlen($prefix) + 1) . '.json'; } else { $version_path = $version . '/catalog.json'; } } else { $version_path = 'catalog.json'; } $path = './schema/' . $version_path; // Get the schema and data as objects $retriever = new JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve('file://' . realpath($path)); $refResolver = new JsonSchema\RefResolver($retriever); $refResolver->resolve($schema, 'file://' . __DIR__ . '/../../schema/' . $version_path); return $schema; }