Пример #1
0
 /**
  * @dataProvider getAddressTestData
  */
 public function testJsonAddressTestValidate($data, $schema)
 {
     $schemaIndex = new JsonSchemaIndex($schema);
     $this->assertEquals($schemaIndex->root['type'], 'array');
     $nodename = isset($schema['title']) ? $schema['title'] : "Root node";
     $rootschema = $schemaIndex->newRef($schema, null, null, $nodename);
     $rootjson = new JsonTreeRef($data, null, null, $nodename, $rootschema);
     $this->assertEquals($rootjson->getTitle(), 'Address Book');
     return $schemaIndex;
 }
Пример #2
0
 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;
 }
Пример #3
0
/**
 * 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();
}