File: validate-schema.t

package info (click to toggle)
libjson-validator-perl 3.06%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 616 kB
  • sloc: perl: 1,308; makefile: 6
file content (40 lines) | stat: -rw-r--r-- 1,168 bytes parent folder | download
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
use Mojo::Base -strict;
use Test::More;
use JSON::Validator;
use lib '.';
use t::Helper ();

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": []
  }
}