1JSON::Validator::Joi(3)User Contributed Perl DocumentatioJnSON::Validator::Joi(3)
2
3
4
6 JSON::Validator::Joi - Joi validation sugar for JSON::Validator
7
9 use JSON::Validator "joi";
10
11 my @errors = joi(
12 {
13 name => "Jan Henning",
14 age => 34,
15 email => "jhthorsen@cpan.org",
16 },
17 joi->object->props(
18 age => joi->integer->min(0)->max(200),
19 email => joi->regex(".@.")->required,
20 name => joi->string->min(1),
21 )
22 );
23
24 die "@errors" if @errors;
25
27 JSON::Validator::Joi is an elegant DSL schema-builder. The main purpose
28 is to build a JSON Schema <https://json-schema.org/> for
29 JSON::Validator, but it can also validate data directly with sane
30 defaults.
31
33 enum
34 my $joi = $joi->enum(["foo", "bar"]);
35 my $array_ref = $joi->enum;
36
37 Defines a list of enum values for "integer", "number" and "string".
38
39 format
40 my $joi = $joi->format("email");
41 my $str = $joi->format;
42
43 Used to set the format of the "string". See also "iso_date", "email"
44 and "uri".
45
46 max
47 my $joi = $joi->max(10);
48 my $int = $joi->max;
49
50 · array
51
52 Defines the max number of items in the array.
53
54 · integer, number
55
56 Defined the max value.
57
58 · object
59
60 Defines the max number of items in the object.
61
62 · string
63
64 Defines how long the string can be.
65
66 min
67 my $joi = $joi->min(10);
68 my $int = $joi->min;
69
70 · array
71
72 Defines the minimum number of items in the array.
73
74 · integer, number
75
76 Defined the minimum value.
77
78 · object
79
80 Defines the minimum number of items in the object.
81
82 · string
83
84 Defines how short the string can be.
85
86 multiple_of
87 my $joi = $joi->multiple_of(3);
88 my $int = $joi->multiple_of;
89
90 Used by "integer" and "number" to define what the number must be a
91 multiple of.
92
93 regex
94 my $joi = $joi->regex("^\w+$");
95 my $str = $joi->regex;
96
97 Defines a pattern that "string" will be validated against.
98
99 type
100 my $joi = $joi->type("string");
101 my $joi = $joi->type([qw(null integer)]);
102 my $any = $joi->type;
103
104 Sets the required type. This attribute is set by the convenience
105 methods "array", "integer", "object" and "string", but can be set
106 manually if you need to check against a list of type.
107
109 TO_JSON
110 Alias for "compile".
111
112 alphanum
113 my $joi = $joi->alphanum;
114
115 Sets "regex" to "^\w*$".
116
117 array
118 my $joi = $joi->array;
119
120 Sets "type" to "array".
121
122 boolean
123 my $joi = $joi->boolean;
124
125 Sets "type" to "boolean".
126
127 compile
128 my $hash_ref = $joi->compile;
129
130 Will convert this object into a JSON-Schema data structure that
131 "schema" in JSON::Validator understands.
132
133 date_time
134 my $joi = $joi->date_time;
135
136 Sets "format" to date-time.
137
138 email
139 my $joi = $joi->email;
140
141 Sets "format" to email.
142
143 extend
144 my $new_joi = $joi->extend($other_joi_object);
145
146 Will extend $joi with the definitions in $other_joi_object and return a
147 new object.
148
149 iso_date
150 Alias for "date_time".
151
152 integer
153 my $joi = $joi->integer;
154
155 Sets "type" to "integer".
156
157 items
158 my $joi = $joi->items($joi);
159 my $joi = $joi->items([$joi, ...]);
160
161 Defines a list of items for the "array" type.
162
163 length
164 my $joi = $joi->length(10);
165
166 Sets both "min" and "max" to the number provided.
167
168 lowercase
169 my $joi = $joi->lowercase;
170
171 Will set "regex" to only match lower case strings.
172
173 negative
174 my $joi = $joi->negative;
175
176 Sets "max" to 0.
177
178 number
179 my $joi = $joi->number;
180
181 Sets "type" to "number".
182
183 object
184 my $joi = $joi->object;
185
186 Sets "type" to "object".
187
188 pattern
189 Alias for "regex".
190
191 positive
192 my $joi = $joi->positive;
193
194 Sets "min" to 0.
195
196 props
197 my $joi = $joi->props(name => JSON::Validator::Joi->new->string, ...);
198
199 Used to define properties for an "object" type. Each key is the name of
200 the parameter and the values must be a JSON::Validator::Joi object.
201
202 required
203 my $joi = $joi->required;
204
205 Marks the current property as required.
206
207 strict
208 my $joi = $joi->strict;
209
210 Sets "array" and "object" to not allow any more items/keys than what is
211 defined.
212
213 string
214 my $joi = $joi->string;
215
216 Sets "type" to "string".
217
218 token
219 my $joi = $joi->token;
220
221 Sets "regex" to "^[a-zA-Z0-9_]+$".
222
223 validate
224 my @errors = $joi->validate($data);
225
226 Used to validate $data using "validate" in JSON::Validator. Returns a
227 list of JSON::Validator::Error objects on invalid input.
228
229 unique
230 my $joi = $joi->unique;
231
232 Used to force the "array" to only contain unique items.
233
234 uppercase
235 my $joi = $joi->uppercase;
236
237 Will set "regex" to only match upper case strings.
238
239 uri
240 my $joi = $joi->uri;
241
242 Sets "format" to uri.
243
245 JSON::Validator
246
247 <https://github.com/hapijs/joi>.
248
249
250
251perl v5.30.1 2020-02-09 JSON::Validator::Joi(3)