File: jv-anyof.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 (66 lines) | stat: -rw-r--r-- 1,702 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use lib '.';
use t::Helper;

my $schema
  = {anyOf =>
    [{type => "string", maxLength => 5}, {type => "number", minimum => 0}]
  };

validate_ok 'short',    $schema;
validate_ok 'too long', $schema, E('/', '/anyOf/0 String is too long: 8/5.');
validate_ok 12,         $schema;
validate_ok int(-1), $schema, E('/', '/anyOf/1 -1 < minimum(0)');
validate_ok {}, $schema, E('/', '/anyOf Expected string/number - got object.');

# anyOf with explicit integer (where _guess_data_type returns 'number')
my $schemaB = {anyOf => [{type => 'integer'}, {minimum => 2}]};
validate_ok 1, $schemaB;

validate_ok(
  {type => 'string'},
  {
    properties => {
      type => {
        anyOf => [
          {'$ref' => '#/definitions/simpleTypes'},
          {
            type        => 'array',
            items       => {'$ref' => '#/definitions/simpleTypes'},
            minItems    => 1,
            uniqueItems => Mojo::JSON::true,
          }
        ]
      },
    },
    definitions => {
      simpleTypes =>
        {enum => [qw(array boolean integer null number object string)]}
    }
  }
);

validate_ok(
  {age => 6},
  {
    '$schema'   => 'http://json-schema.org/draft-04/schema#',
    type        => 'object',
    title       => 'test',
    description => 'test',
    properties  => {
      age => {type => 'number', anyOf => [{multipleOf => 5}, {multipleOf => 3}]}
    }
  }
);

validate_ok(
  {c => 'c present, a/b is missing'},
  {
    type       => 'object',
    properties => {a => {type => 'number'}, b => {type => 'string'}},
    anyOf      => [{required => ['a']}, {required => ['b']}],
  },
  E('/a', '/anyOf/0 Missing property.'),
  E('/b', '/anyOf/1 Missing property.'),
);

done_testing;