File: validate-schema.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 (34 lines) | stat: -rw-r--r-- 1,131 bytes parent folder | download | duplicates (3)
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
use Mojo::Base -strict;
use JSON::Validator;
use Test::More;

my $should_fail = JSON::Validator->new->schema('data://main/invalid.json');
my $json_schema = JSON::Validator->new->schema('http://json-schema.org/draft-04/schema#');
my @errors;

# The schema is invalid...
@errors = $json_schema->validate($should_fail->schema->data);
is $errors[0], '/properties/should_fail: Expected object - got array.', 'invalid property element';

# ...but can still be used to validate data.
@errors = $should_fail->validate({foo => 123});
is int(@errors), 0, 'data is valid';

# Can also use load_and_validate_schema() to do the same as above
eval { JSON::Validator->new->load_and_validate_schema('data://main/invalid.json'); };
like $@, qr{Expected object - got array}, 'invalid schema';

done_testing;

__DATA__
@@ invalid.json
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Example Schema That Should Fail To Load",
  "description": "There is an array as the value of an object property, which should not be allowed.",
  "type": "object",
  "properties": {
    "foo": { "type": "integer" },
    "should_fail": []
  }
}