File: joi.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 (136 lines) | stat: -rw-r--r-- 3,677 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
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
use lib '.';
use t::Helper;
use JSON::Validator 'joi';
use Test::More;

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.'),
);

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

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

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

is_deeply(
  edj(
    joi->object->props(x => joi->integer, y => joi->integer)
      ->extend(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',
);

done_testing;