File: id-keyword-draft7.t

package info (click to toggle)
libjson-validator-perl 5.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,160 kB
  • sloc: perl: 3,015; makefile: 14
file content (46 lines) | stat: -rw-r--r-- 1,412 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use Mojo::Base -strict;
use JSON::Validator;
use Test::Mojo;
use Test::More;

my ($base_url, $jv, $t, @e);

use Mojolicious::Lite;
get '/person'           => [format => ['json']] => 'person';
get '/invalid-relative' => [format => ['json']] => 'invalid-relative';

$t  = Test::Mojo->new;
$jv = JSON::Validator->new(ua => $t->ua);

eval {
  $t->get_ok('/person.json')->status_is(200);
  $base_url = $t->tx->req->url->to_abs->path('/');
  $jv->load_and_validate_schema("${base_url}person.json", {schema => 'http://json-schema.org/draft-07/schema#'});
};
ok !$@, "${base_url}schema.json" or diag $@;
isa_ok $jv->schema, 'JSON::Validator::Schema::Draft7';

is $jv->schema->id,            'http://example.com/person.json',          'schema id';
is $jv->schema->moniker,       'draft07',                                 'moniker';
is $jv->schema->specification, 'http://json-schema.org/draft-07/schema#', 'schema specification';

eval { $jv->load_and_validate_schema("${base_url}invalid-relative.json") };
like $@, qr{Relative URL not allowed}, 'Root id cannot be relative' or diag $@;

done_testing;

__DATA__
@@ invalid-relative.json.ep
{"$id": "whatever", "$schema": "http://json-schema.org/draft-07/schema#"}
@@ person.json.ep
{
  "$id": "http://example.com/person.json",
  "definitions": {
    "Person": {
      "type": "object",
      "properties": {
        "firstName": { "type": "string" }
      }
    }
  }
}