<?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 testInnerDefinitions() { $schema = <<<JSN { "type": "object", "additionalProperties":false, "properties": { "person": { "\$ref": "#/definitions/persondef" } }, "definitions": { "persondef": { "type": "object", "additionalProperties":false, "properties": { "name": { "type": "string" }, "age": { "type" : "integer" } } } } } JSN; $schemaObj = json_decode($schema); $resolver = new \JsonSchema\RefResolver(); $resolver->resolve($schemaObj); $schema = json_encode($schemaObj); $this->testValidCases('{"person": {"name" : "John Doe", "age" : 30} }', $schema); $this->testInvalidCases('{"person": {"name" : "John Doe", "age" : "wrong"} }', $schema); }
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; }
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()); }
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; }
/** * @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_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; }
/** * Validate response by json schema * * @param string $schema path to json schema file */ public function canSeeResponseIsValidOnSchemaFile($schema) { $schemaPath = realpath($schema); $retriever = new \JsonSchema\Uri\UriRetriever(); $schema = $retriever->retrieve('file://' . $schemaPath); $refResolver = new \JsonSchema\RefResolver($retriever); $refResolver->resolve($schema, 'file://' . $schemaPath); $response = $this->getModule('REST')->response; $validator = new \JsonSchema\Validator(); $validator->setUriRetriever(new UriRetriever()); $validator->check(json_decode($response), $schema); $message = ''; $isValid = $validator->isValid(); if (!$isValid) { $message = "JSON does not validate. Violations:\n"; foreach ($validator->getErrors() as $error) { $message .= $error['property'] . " " . $error['message'] . PHP_EOL; } } $this->assertTrue($isValid, $message); }
public function json_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; }
/** * @expectedException \JsonSchema\Exception\JsonDecodingException */ public function testMaxDepthExceeded() { // stub schema $jsonSchema = new \stdClass(); $jsonSchema->id = 'stub'; $jsonSchema->additionalItems = 'stub'; // mock retriever $retriever = $this->getMock('JsonSchema\\Uri\\UriRetriever', array('retrieve')); $retriever->expects($this->any())->method('retrieve')->will($this->returnValue($jsonSchema)); // stub resolver \JsonSchema\RefResolver::$maxDepth = 0; $resolver = new \JsonSchema\RefResolver($retriever); $resolver->resolve($jsonSchema); }
public function testFetchRef() { // stub schema $jsonSchema = new \stdClass(); $jsonSchema->id = 'stub'; $jsonSchema->additionalItems = 'stub'; $ref = 'ref'; $sourceUri = null; // mock retriever $retriever = $this->getMock('JsonSchema\\Uri\\UriRetriever', array('retrieve')); $retriever->expects($this->any())->method('retrieve')->will($this->returnValue($jsonSchema)); // stub resolver $resolver = new \JsonSchema\RefResolver(); $resolver->setUriRetriever($retriever); $this->assertEquals($jsonSchema, $resolver->fetchRef($ref, $sourceUri)); }
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'])); } } }
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; }