File: jv-const.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 (58 lines) | stat: -rw-r--r-- 2,398 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
use lib '.';
use t::Helper;

my $faithful  = {type => 'object', properties => {constancy => {const => "as the northern star"}}};
my $ambitious = {type => 'object', properties => {constancy => {const => "there is a tide in the affairs of men"}}};

validate_ok {name => "Caesar", constancy => "as the northern star"},                  $faithful;
validate_ok {name => "Brutus", constancy => "there is a tide in the affairs of men"}, $ambitious;

validate_ok {name => "Cassius", constancy => "Cassius from bondage will deliver Cassius"}, $faithful,
  E('/constancy', q{Does not match const: "as the northern star".});

validate_ok({name => "Calpurnia", constancy => "Do not go forth today. Call it my fear That keeps you in the house"},
  $ambitious, E('/constancy', q{Does not match const: "there is a tide in the affairs of men".}));

# Now oneOf should work right
# before the fix, this failed with: "All of the oneOf rules match."
# because "likes: chocolate" vs. "peanutbutter" wasn't being considered
my $schema = {
  type       => 'object',
  properties => {
    people => {
      type  => 'array',
      items => {oneOf => [{'$ref' => '#/definitions/chocolate'}, {'$ref' => '#/definitions/peanutbutter'}]},
    },
  },
  definitions => {
    chocolate => {
      type       => 'object',
      properties => {name => {type => 'string'}, age => {type => 'number'}, likes => {const => 'chocolate'}},
    },
    peanutbutter => {
      type       => 'object',
      properties => {name => {type => 'string'}, age => {type => 'number'}, likes => {const => 'peanutbutter'}},
    },
  },
};
validate_ok {people => [{name => 'mr. chocolate fan', age => 42, likes => 'peanutbutter'}]}, $schema;

my $null_const = {const => undef};
validate_ok 'foo', $null_const, E('/', q{Does not match const: null.});
validate_ok undef, $null_const;

my $empty_const = {const => ''};
validate_ok 'foo', $empty_const, E('/', q{Does not match const: "".});
validate_ok '', $empty_const;

my $array_constant = {const => [1, 'a', undef]};
validate_ok [1, 'a', undef], $array_constant;
validate_ok [1, 'b', undef], $array_constant, E('/', q{Does not match const: [1,"a",null].});

validate_ok true,  {const => true};
validate_ok false, {const => false};

validate_ok false, {const => true},  E('/', 'Does not match const: true.');
validate_ok true,  {const => false}, E('/', 'Does not match const: false.');

done_testing;