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