public static function open($id) { $model = newStdClass; $model->id = $id; $results = self::search(JsonSchema::fromModel($model)); return count($results) ? $results[0] : NULL; }
function validate() { $errors = JsonSchema::check($this); if ($errors) { throw new Errors\ValidationError($errors); } }
/** * DO NOT USE, PRIVATE METHOD! must be public for unittests * @param string $uid scheduler.unique_id * @param array $params hash keys (all optional): page, per_page * @return Hypercharge\RecurringScheduler */ static function index($uid, $params = array()) { Helper::validateUniqueId($uid); JsonSchema::validate('scheduler_transactions_index', $params); $factory = Config::getFactory(); $url = $factory->createUrl(array('scheduler', $uid, 'transactions'), $params); $response = $factory->createHttpsClient()->jsonGet($url); if ($response->entries_base_type != 'PaymentTransaction') { throw new Errors\ResponseFormatError('entries_base_type expected "PaymentTransaction" but was: ' . $response->entries_base_type, $response); } return $response; }
public function setMetaProperty($name, $type, $title = '', $extraParams = []) { if (!isset($this->properties->{'@meta'})) { $this->setOptionalProperty('@meta', 'object', ['title' => 'Mason meta properties']); } if (!isset($this->properties->{'@meta'}->properties)) { $this->properties->{'@meta'}->properties = new \stdClass(); } $property = JsonSchema::make(['type' => $type]); if ($title) { $property->setTitle($title); } if ($extraParams) { $property->addParams($extraParams); } $this->properties->{'@meta'}->properties->{$name} = $property; return $this; }
static function checkObj($instance, $objTypeDef, $path, $additionalProp, $_changing) { if ($objTypeDef instanceof StdClass) { if (!($instance instanceof StdClass || is_array($instance))) { self::$errors[] = array('property' => $path, 'message' => "an object is required"); } foreach ($objTypeDef as $i => $value) { $value = array_key_exists($i, $instance) ? is_array($instance) ? $instance[$i] : $instance->{$i} : new JsonSchemaUndefined(); $propDef = $objTypeDef->{$i}; self::checkProp($value, $propDef, $path, $i, $_changing); } } // additional properties and requires foreach ($instance as $i => $value) { // verify additional properties, when its not allowed if (!isset($objTypeDef->{$i}) && $additionalProp === false && $i !== '$schema') { self::$errors[] = array('property' => $path, 'message' => "The property " . $i . " is not defined in the objTypeDef and the objTypeDef does not allow additional properties"); } // verify requires if ($objTypeDef && isset($objTypeDef->{$i}) && isset($objTypeDef->{$i}->requires)) { $requires = $objTypeDef->{$i}->requires; if (!array_key_exists($requires, $instance)) { self::$errors[] = array('property' => $path, 'message' => "the presence of the property " . $i . " requires that " . $requires . " also be present"); } } $value = is_array($instance) ? $instance[$i] : $instance->{$i}; // To verify additional properties types. if ($objTypeDef && is_object($objTypeDef) && !isset($objTypeDef->{$i})) { self::checkProp($value, $additionalProp, $path, $i); } // Verify inner schema definitions $schemaPropName = '$schema'; if (!$_changing && $value && isset($value->{$schemaPropName})) { self::$errors = array_merge(self::$errors, checkProp($value, $value->{$schemaPropname}, $path, $i)); } } return self::$errors; }
<?php require_once 'JsonSchema.php'; require_once 'JsonSchemaUndefined.php'; Dbg::$quietMode = true; if ($_REQUEST['schema']) { $schema = json_decode($_REQUEST['schema']); if (!$schema) { trigger_error('Could not parse the SCHEMA object.', E_USER_ERROR); } } else { $schema = null; } $json = json_decode($_REQUEST['json']); if (!$json) { trigger_error('Could not parse the JSON object.', E_USER_ERROR); } if ($_REQUEST['typeCastMode'] == 'true') { JsonSchema::$checkMode = JsonSchema::CHECK_MODE_TYPE_CAST; } $result = JsonSchema::validate($json, $schema); header('Content-type: application/x-json'); echo json_encode($result);
/** * @param string $uid the scheduler.unique_id * @param array $data key-value-Hash, Scheduler fields to update * @return Hypercharge\Scheduler */ static function update($uid, $data) { Helper::validateUniqueId($uid); JsonSchema::validate('scheduler_update', $data); $factory = Config::getFactory(); $url = $factory->createUrl(array('scheduler', $uid)); $response = $factory->createHttpsClient()->jsonPut($url, $data); return new Scheduler($response); }
public function ValidateJSONMAnifestAgainstShema($json) { include_once dirname(__FILE__) . '/../extensions/JSONShemaValidator/JsonSchema.php'; include_once dirname(__FILE__) . '/../extensions/JSONShemaValidator/JsonSchemaUndefined.php'; $shema = file_get_contents(dirname(__FILE__) . '/../extensions/JSONShemaValidator/Bober_Interacitve_Task_Object_Shema.json'); $schema = json_decode($shema); if (!$schema) { return array('return' => false, 'error' => 'Could not parse the SCHEMA object.'); } else { $schema = null; } $json = json_decode($json); if (!$json) { switch (json_last_error()) { case JSON_ERROR_NONE: echo ' - No errors'; break; case JSON_ERROR_DEPTH: echo ' - Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: echo ' - Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: echo ' - Unexpected control character found'; break; case JSON_ERROR_SYNTAX: echo ' - Syntax error, malformed JSON'; break; case JSON_ERROR_UTF8: echo ' - Malformed UTF-8 characters, possibly incorrectly encoded'; break; default: echo ' - Unknown error'; break; } return array('return' => false, 'error' => 'Could not parse the JSON object.'); } $result = json_decode(json_encode(JsonSchema::validate($json, $schema)), true); return array('return' => $result['valid'], 'error' => implode("\n", $result['errors'])); }
public function testGenerate() { $generator = new JsonSchema('foo', 'http://api.phpsx.org', 'http://foo.phpsx.org'); $json = $generator->generate($this->getResource()); $expect = <<<'JSON' { "$schema": "http://json-schema.org/draft-04/schema#", "id": "foo", "type": "object", "definitions": {"ref993f4bb37f524889fc963fedd6381458": { "type": "object", "properties": { "id": {"type": "integer"}, "userId": {"type": "integer"}, "title": { "type": "string", "minLength": 3, "maxLength": 16, "pattern": "[A-z]+" }, "date": {"type": "string"} }, "additionalProperties": false }}, "properties": { "getResponse": { "type": "object", "properties": {"entry": { "type": "array", "items": {"$ref": "#/definitions/ref993f4bb37f524889fc963fedd6381458"} }}, "additionalProperties": false }, "postRequest": { "type": "object", "properties": { "id": {"type": "integer"}, "userId": {"type": "integer"}, "title": { "type": "string", "minLength": 3, "maxLength": 16, "pattern": "[A-z]+" }, "date": {"type": "string"} }, "required": [ "title", "date" ], "additionalProperties": false }, "postResponse": { "type": "object", "properties": { "success": {"type": "boolean"}, "message": {"type": "string"} }, "additionalProperties": false }, "putRequest": { "type": "object", "properties": { "id": {"type": "integer"}, "userId": {"type": "integer"}, "title": { "type": "string", "minLength": 3, "maxLength": 16, "pattern": "[A-z]+" }, "date": {"type": "string"} }, "required": ["id"], "additionalProperties": false }, "putResponse": { "type": "object", "properties": { "success": {"type": "boolean"}, "message": {"type": "string"} }, "additionalProperties": false }, "deleteRequest": { "type": "object", "properties": { "id": {"type": "integer"}, "userId": {"type": "integer"}, "title": { "type": "string", "minLength": 3, "maxLength": 16, "pattern": "[A-z]+" }, "date": {"type": "string"} }, "required": ["id"], "additionalProperties": false }, "deleteResponse": { "type": "object", "properties": { "success": {"type": "boolean"}, "message": {"type": "string"} }, "additionalProperties": false } } } JSON; $this->assertJsonStringEqualsJsonString($expect, $json); }
public function testGenerate() { $generator = new JsonSchema(); $result = $generator->generate($this->getSchema()); $expect = <<<'JSON' { "$schema": "http://json-schema.org/draft-04/schema#", "id": "urn:schema.phpsx.org#", "description": "An general news entry", "definitions": { "ref11c55f48b558e06534c2dccf005c97cb": { "type": "object", "description": "An simple author element with some description", "properties": { "title": { "type": "string", "pattern": "[A-z]{3,16}" }, "email": { "type": "string", "description": "We will send no spam to this addresss" }, "categories": { "type": "array", "maxItems": 8, "items": { "type": "string" } }, "locations": { "type": "array", "description": "Array of locations", "items": { "$ref": "#/definitions/refe081a664cb5227a334bc5e0fa367f178" } }, "origin": { "$ref": "#/definitions/refe081a664cb5227a334bc5e0fa367f178" } }, "required": [ "title" ], "additionalProperties": false }, "refe081a664cb5227a334bc5e0fa367f178": { "type": "object", "description": "Location of the person", "properties": { "lat": { "type": "integer" }, "long": { "type": "integer" } }, "additionalProperties": false }, "refaf92365f86505945496a4ce039023ec6": { "type": "object", "description": "An application", "properties": { "name": { "type": "string" }, "url": { "type": "string" } }, "additionalProperties": false } }, "type": "object", "properties": { "tags": { "type": "array", "items": { "type": "string" }, "minItems": 1, "maxItems": 6 }, "receiver": { "type": "array", "items": { "$ref": "#/definitions/ref11c55f48b558e06534c2dccf005c97cb" }, "minItems": 1 }, "resources": { "type": "array", "items": { "oneOf": [{ "$ref": "#/definitions/refe081a664cb5227a334bc5e0fa367f178" },{ "$ref": "#/definitions/refaf92365f86505945496a4ce039023ec6" }] } }, "read": { "type": "boolean" }, "source": { "oneOf": [{ "$ref": "#/definitions/ref11c55f48b558e06534c2dccf005c97cb" },{ "$ref": "#/definitions/refaf92365f86505945496a4ce039023ec6" }] }, "author": { "$ref": "#/definitions/ref11c55f48b558e06534c2dccf005c97cb" }, "sendDate": { "type": "string" }, "readDate": { "type": "string" }, "expires": { "type": "string" }, "price": { "type": "number", "minimum": 1, "maximum": 100 }, "rating": { "type": "integer", "minimum": 1, "maximum": 5 }, "content": { "type": "string", "minLength": 3, "maxLength": 512, "description": "Contains the main content of the news entry" }, "question": { "type": "string", "enum": [ "foo", "bar" ] }, "coffeeTime": { "type": "string" } }, "required": [ "receiver", "author", "price", "content" ], "additionalProperties": false } JSON; $this->assertJsonStringEqualsJsonString($expect, $result); }