/**
  * The Json Schema Schema should validte the Json Schema!
  * @bug validation will enter recursive loop if the target is derefd because refs are not detected.
  */
 public function testJsonSchemaSchema()
 {
     $jsonDocs = new JsonDocs();
     $schemaDoc = $jsonDocs->loadDocStr(file_get_contents(getenv('DATADIR') . '/schema.json'), new Uri("file:///schema.json"));
     $targetDoc = json_decode(file_get_contents(getenv('DATADIR') . '/schema.json'));
     $schema = new JsonSchema($schemaDoc);
     $valid = $schema->validate($targetDoc);
     $this->assertEquals(true, $valid, $valid);
 }
 /**
  * @dataProvider fileProvider
  */
 public function testJsonSchema($file)
 {
     if (in_array(basename($file), self::$SKIP_FILES)) {
         $this->markTestSkipped("Skipping {basename({$file})}");
     }
     $testGroup = json_decode(file_get_contents($file));
     $jsonDocs = new JsonDocs();
     foreach ($testGroup as $k => $tests) {
         $schemaDoc = $jsonDocs->loadDocStr(json_encode($tests->schema), new Uri("file:///test-{$k}.json"));
         $schema = new JsonSchema($schemaDoc);
         foreach ($tests->tests as $test) {
             $valid = $schema->validate($test->data);
             if ($test->valid) {
                 $this->assertEquals(true, $valid, $test->description);
             } else {
                 $this->assertInstanceOf('\\JsonSchema\\Constraint\\ValidationError', $valid, $test->description);
             }
         }
     }
 }
function main($argc, $argv)
{
    sizeof($argv) == 3 or sizeof($argv) == 4 or die(usage());
    $jsonDocs = new JsonDocs(new JsonLoader());
    list($schemaFile, $schemaPointer) = makePath($argv[1]) or die("Invalid schema file\n");
    $schemaDoc = $jsonDocs->loadUri(new Uri($schemaFile));
    $schema = new JsonSchema($schemaDoc);
    print "Schema created from {$schemaFile}\n";
    $target = json_decode(file_get_contents($argv[2])) or die("Invalid JSON file\n");
    print "Target loaded\n";
    if (isset($argv[3])) {
        $target = $jsonDocs::getPointer($target, $argv[3]);
    }
    print "Validate [at {$schemaPointer}]:\n";
    $valid = $schema->validate($target, $schemaPointer);
    if ($valid === true) {
        print "OK\n";
    } else {
        print $valid;
    }
}