File: coerce.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 (59 lines) | stat: -rw-r--r-- 1,515 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
use Mojo::Base -strict;
use JSON::Validator;
use Mojo::JSON 'to_json';
use Test::More;

my $validator = JSON::Validator->new;
my %coerce = (booleans => 1);
is_deeply(
  $validator->coerce(%coerce)->coerce,
  {booleans => 1},
  'hash is accepted'
);
is_deeply(
  $validator->coerce(\%coerce)->coerce,
  {booleans => 1},
  'hash reference is accepted'
);

note
  'coerce(1) is here for back compat reasons, even though not documented any more';
is_deeply(
  $validator->coerce(1)->coerce,
  {%coerce, numbers => 1, strings => 1},
  '1 is accepted'
);

note 'make sure input is coerced';
my @items = ([boolean => 'true'], [integer => '42'], [number => '4.2']);
for my $i (@items) {
  for my $schema (schemas($i->[0])) {
    my $x = $i->[1];
    $validator->validate($x, $schema);
    is to_json($x), $i->[1], sprintf 'no quotes around %s %s', $i->[0],
      to_json($schema);

    $x = {v => $i->[1]};
    $validator->validate($x, {type => 'object', properties => {v => $schema}});
    is to_json($x->{v}), $i->[1], sprintf 'no quotes around %s %s', $i->[0],
      to_json($schema);

    $x = [$i->[1]];
    $validator->validate($x, {type => 'array', items => $schema});
    is to_json($x->[0]), $i->[1], sprintf 'no quotes around %s %s', $i->[0],
      to_json($schema);
  }
}

done_testing;

sub schemas {
  my $base = {type => shift};
  return (
    $base,
    {type  => ['array', $base->{type}]},
    {allOf => [$base]},
    {anyOf => [{type => 'array'}, $base]},
    {oneOf => [$base, {type => 'array'}]},
  );
}