File: jv-oneof.t

package info (click to toggle)
libjson-validator-perl 4.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 828 kB
  • sloc: perl: 2,816; makefile: 14
file content (65 lines) | stat: -rw-r--r-- 3,215 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
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
use lib '.';
use t::Helper;

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

validate_ok 'short', $schema;
validate_ok 12,      $schema;

$schema = {oneOf => [{type => 'number', multipleOf => 5}, {type => 'number', multipleOf => 3}]};
validate_ok 10, $schema;
validate_ok 9,  $schema;
validate_ok 15, $schema, E('/', 'All of the oneOf rules match.');
validate_ok 13, $schema, E('/', '/oneOf/0 Not multiple of 5.'), E('/', '/oneOf/1 Not multiple of 3.');

$schema = {oneOf => [{type => 'object'}, {type => 'string', multipleOf => 3}]};
validate_ok 13, $schema, E('/', '/oneOf Expected object/string - got number.');

$schema = {oneOf => [{type => 'object'}, {type => 'number', multipleOf => 3}]};
validate_ok 13, $schema, E('/', '/oneOf/0 Expected object - got number.'), E('/', '/oneOf/1 Not multiple of 3.');

# Alternative oneOf
# https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.7
$schema = {type => 'object', properties => {x => {type => ['string', 'null'], format => 'date-time'}}};
validate_ok {x => 'foo'}, $schema, E('/x', 'Does not match date-time format.');
validate_ok {x => '2015-04-21T20:30:43.000Z'}, $schema;
validate_ok {x => undef},                      $schema;

$schema = {type => 'object', properties => {x => {type => ['null', 'number']}}};
validate_ok {x => 'foo'}, $schema, E('/x', 'Expected null/number - got string.');

validate_ok 1, {oneOf => [{minimum => 1}, {minimum => 2}, {maximum => 3}]}, E('/', 'oneOf rules 0, 2 match.');

validate_ok 'hello', {oneOf => [true, false]};

validate_ok 'hello', {oneOf => [true, true]}, E('/', 'All of the oneOf rules match.');

validate_ok 'hello', {oneOf => [false, false]}, E('/', '/oneOf/0 Should not match.'),
  E('/', '/oneOf/1 Should not match.');

validate_ok 'hello', {oneOf => [true, {type => ['string', 'boolean']}]}, E('/', 'All of the oneOf rules match.');

validate_ok 'hello', {type => ['integer', 'boolean']}, E('/', 'Expected integer/boolean - got string.');

validate_ok 'hello', {oneOf => [false, {type => ['integer', 'string'], enum => [123, 'HELLO']}]},
  E('/', '/oneOf/0 Should not match.'), E('/', '/oneOf/1 Not in enum list: 123, HELLO.');

validate_ok 'hello', {oneOf => [false, {type => ['integer', 'boolean']}]}, E('/', '/oneOf/0 Should not match.'),
  E('/', '/oneOf/1 Expected integer/boolean - got string.');

validate_ok 'hello', {oneOf => [false, {type => 'integer'}]}, E('/', '/oneOf/0 Should not match.'),
  E('/', '/oneOf/1 Expected integer - got string.');

validate_ok 'hello', {oneOf => [{type => ['integer', 'boolean']}]},
  E('/', '/oneOf/0 Expected integer/boolean - got string.');

validate_ok 'hello',
  {oneOf => [{oneOf => [{type => 'boolean'}, {type => 'string', maxLength => 2}]}, {type => 'integer'}]},
  E('/', '/oneOf/0/oneOf/0 Expected boolean - got string.'), E('/', '/oneOf/0/oneOf/1 String is too long: 5/2.'),
  E('/', '/oneOf/1 Expected integer - got string.');

validate_ok {foo => 'not an arrayref'},
  {oneOf => [{type => 'object', properties => {foo => {type => 'array'}}}, {type => 'boolean'}]},
  E('/foo', '/oneOf/0 Expected array - got string.'), E('/', '/oneOf/1 Expected boolean - got object.');

done_testing;