/** * @dataProvider getSimpleTestData */ public function testJsonSimpleTestSchemaValidate($data, $schema) { $schemaschematext = file_get_contents('schemas/schemaschema.json'); $schemaschema = json_decode($schemaschematext, true); $jsonref = new JsonTreeRef($schema); $jsonref->attachSchema($schemaschema); $jsonref->validate(); return $jsonref; }
public static function validateDataEditFilter($editor, $text, $section, &$error, $summary) { // I can't remember if jsondataobj needs to be a singleton/global, but // will chance calling a new instance here. $title = $editor->getTitle(); $ns = $title->getNamespace(); if (!JsonData::isJsonDataNeeded($ns)) { return true; } $jsondataobj = new JsonData($title); $json = JsonData::stripOuterTagsFromText($text); try { $schematext = $jsondataobj->getSchemaText(); } catch (JsonDataException $e) { $schematext = $jsondataobj->readJsonFromPredefined('openschema'); $error = "<b>" . wfMessage('jsondata-servervalidationerror') . "</b>: "; $error .= wfMessage('jsondata-invalidjson'); } $data = json_decode($json, true); if (is_null($data)) { $error = "<b>" . wfMessage('jsondata-servervalidationerror') . "</b>: "; $error .= wfMessage('jsondata-invalidjson'); return true; } $schema = json_decode($schematext, true); $rootjson = new JsonTreeRef($data); $rootjson->attachSchema($schema); try { $rootjson->validate(); } catch (JsonSchemaException $e) { $error = "<b>" . wfMessage('jsondata-servervalidationerror') . "</b>: "; $error .= htmlspecialchars($e->getMessage()); } return true; }
/** * Validates object against JSON Schema. * * @throws JsonSchemaException: If the object fails to validate. * @param array $object: Object to be validated. * @param array $schema: Schema to validate against (default: JSON Schema). * @return bool: True. */ function efSchemaValidate($object, $schema = NULL) { if ($schema === NULL) { // Default to JSON Schema $json = file_get_contents(__DIR__ . '/schemas/schemaschema.json'); $schema = FormatJson::decode($json, true); } // We depart from the JSON Schema specification in disallowing by default // additional event fields not mentioned in the schema. // See <https://bugzilla.wikimedia.org/show_bug.cgi?id=44454> and // <http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.4>. if (!array_key_exists('additionalProperties', $schema)) { $schema['additionalProperties'] = false; } $root = new JsonTreeRef($object); $root->attachSchema($schema); return $root->validate(); }