File: joi.t

package info (click to toggle)
libjson-validator-perl 5.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,160 kB
  • sloc: perl: 3,015; makefile: 14
file content (158 lines) | stat: -rw-r--r-- 5,419 bytes parent folder | download | duplicates (2)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use lib '.';
use t::Helper;
use JSON::Validator::Joi 'joi';
use Storable 'dclone';

isa_ok +joi->validator, 'JSON::Validator::Schema';
is_deeply +joi->validator->coerce, {booleans => 1, numbers => 1, strings => 1}, 'default coercion';

is_deeply(
  edj(joi->object->strict->props(
    age       => joi->integer->min(0)->max(200),
    alphanum  => joi->alphanum->length(12),
    color     => joi->string->min(2)->max(12)->pattern('^\w+$'),
    date_time => joi->iso_date,
    email     => joi->string->email->required,
    exists    => joi->boolean,
    lc        => joi->lowercase,
    name      => joi->string->min(1),
    pos       => joi->positive,
    token     => joi->token,
    uc        => joi->uppercase,
    uri       => joi->uri,
  )),
  {
    type       => 'object',
    required   => ['email'],
    properties => {
      age       => {type => 'integer', minimum   => 0,  maximum   => 200},
      alphanum  => {type => 'string',  minLength => 12, maxLength => 12, pattern => '^\w*$'},
      color     => {type => 'string',  minLength => 2,  maxLength => 12, pattern => '^\w+$'},
      date_time => {type => 'string',  format    => 'date-time'},
      email     => {type => 'string',  format    => 'email'},
      exists    => {type => 'boolean'},
      lc        => {type => 'string', pattern   => '^\p{Lowercase}*$'},
      name      => {type => 'string', minLength => 1},
      pos       => {type => 'number', minimum   => 0},
      token     => {type => 'string', pattern   => '^[a-zA-Z0-9_]+$'},
      uc        => {type => 'string', pattern   => '^\p{Uppercase}*$'},
      uri       => {type => 'string', format    => 'uri'},
    },
    additionalProperties => false
  },
  'generated correct object schema'
);

is_deeply(
  edj(joi->array->min(0)->max(10)->strict->items(joi->integer->negative)),
  {
    additionalItems => false,
    type            => 'array',
    minItems        => 0,
    maxItems        => 10,
    items           => {type => 'integer', maximum => 0}
  },
  'generated correct array schema'
);

is_deeply(edj(joi->string->enum([qw(1.0 2.0)])), {type => 'string', enum => [qw(1.0 2.0)]}, 'enum for string');

is_deeply(edj(joi->integer->enum([qw(1 2 4 8 16)])), {type => 'integer', enum => [qw(1 2 4 8 16)]}, 'enum for integer');

joi_ok(
  {age => 34, email => 'jhthorsen@cpan.org', name => 'Jan Henning Thorsen'},
  joi->props(
    age   => joi->integer->min(0)->max(200),
    email => joi->string->email->required,
    name  => joi->string->min(1),
  ),
);

joi_ok(
  {age => -1, name => 'Jan Henning Thorsen'},
  joi->props(
    age   => joi->integer->min(0)->max(200),
    email => joi->string->email->required,
    name  => joi->string->min(1),
  ),
  E('/age',   '-1 < minimum(0)'),
  E('/email', 'Missing property.'),
);

note 'test that compile and not compile generates same strict result';
my $strict_obj = joi->object->strict->props({ns => joi->string->required});
joi_ok({ns => 'plop', toto => 'plouf'}, $strict_obj, E('/', 'Properties not allowed: toto.'));
for my $item ($strict_obj->compile, $strict_obj) {
  joi_ok([{ns => 'plop', toto => 'plouf'}], joi->array->strict->items($item), E('/0', 'Properties not allowed: toto.'),
  );
}

note "can omit non-required objects containing required properties";
joi_ok({}, joi->object->props(a => joi->object->props(b => joi->integer->required)));

note "must include required objects containing required properties";
joi_ok(
  {},
  joi->object->props(a => joi->object->required->props(b => joi->integer->required)),
  E('/a', 'Missing property.'),
);

eval { joi->number->extend(joi->integer) };
like $@, qr{Cannot extend joi 'number' by 'integer'}, 'need to extend same type';

test_extend(
  joi->array->min(0)->max(10),
  joi->array->min(5),
  {type => 'array', minItems => 5, maxItems => 10},
  'extended array',
);

test_extend(
  joi->array->items([joi->integer]),                joi->array->items([joi->number]),
  {type => 'array', items => [{type => 'number'}]}, 'extended items in an array',
);

test_extend(
  joi->integer->min(0)->max(10),
  joi->integer->min(5),
  {type => 'integer', minimum => 5, maximum => 10},
  'extended integer',
  'extended integer',
);

test_extend(
  joi->object->props(x => joi->integer, y => joi->integer),
  joi->object->props(x => joi->number),
  {type => 'object', properties => {x => {type => 'number'}, y => {type => 'integer'}}},
  'extended object',
);

is_deeply(
  edj(joi->object->props(ip => joi->type([qw(string null)])->format('ip'), ns => joi->string)),
  {type => 'object', properties => {ip => {format => 'ip', type => [qw(string null)]}, ns => {type => 'string'}}},
  'null or string',
);

test_extend(
  joi->object->props(a => joi->integer, b => joi->integer->required),
  joi->object->props(b => joi->integer->required, x => joi->string->required, y => joi->string->required),
  {
    type       => 'object',
    required   => bag(qw(b x y)),
    properties =>
      {a => {type => 'integer'}, b => {type => 'integer'}, x => {type => 'string'}, y => {type => 'string'}},
  },
  'extended object with required',
);

done_testing;

sub test_extend {
  my ($joi, $by, $expected, $description) = @_;
  my $joi_clone = dclone $joi;
  my $by_clone  = dclone $by;

  cmp_deeply(edj($joi->extend($by)), $expected, $description);
  cmp_deeply $joi, $joi_clone, "$description did not mutate \$joi";
  cmp_deeply $by,  $by_clone,  "$description did not mutate \$by";
}