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($joi);
145
146 Will extend $joi with the definitions in $joi and return a new object.
147
148 iso_date
149 Alias for "date_time".
150
151 integer
152 my $joi = $joi->integer;
153
154 Sets "type" to "integer".
155
156 items
157 my $joi = $joi->items($joi);
158 my $joi = $joi->items([$joi, ...]);
159
160 Defines a list of items for the "array" type.
161
162 length
163 my $joi = $joi->length(10);
164
165 Sets both "min" and "max" to the number provided.
166
167 lowercase
168 my $joi = $joi->lowercase;
169
170 Will set "regex" to only match lower case strings.
171
172 negative
173 my $joi = $joi->negative;
174
175 Sets "max" to 0.
176
177 number
178 my $joi = $joi->number;
179
180 Sets "type" to "number".
181
182 object
183 my $joi = $joi->object;
184
185 Sets "type" to "object".
186
187 pattern
188 Alias for "regex".
189
190 positive
191 my $joi = $joi->positive;
192
193 Sets "min" to 0.
194
195 props
196 my $joi = $joi->props(name => JSON::Validator::Joi->new->string, ...);
197
198 Used to define properties for an "object" type. Each key is the name of
199 the parameter and the values must be a JSON::Validator::Joi object.
200
201 required
202 my $joi = $joi->required;
203
204 Marks the current property as required.
205
206 strict
207 my $joi = $joi->strict;
208
209 Sets "array" and "object" to not allow any more items/keys than what is
210 defined.
211
212 string
213 my $joi = $joi->string;
214
215 Sets "type" to "string".
216
217 token
218 my $joi = $joi->token;
219
220 Sets "regex" to "^[a-zA-Z0-9_]+$".
221
222 validate
223 my @errors = $joi->validate($data);
224
225 Used to validate $data using "validate" in JSON::Validator. Returns a
226 list of JSON::Validator::Error objects on invalid input.
227
228 unique
229 my $joi = $joi->unique;
230
231 Used to force the "array" to only contain unique items.
232
233 uppercase
234 my $joi = $joi->uppercase;
235
236 Will set "regex" to only match upper case strings.
237
238 uri
239 my $joi = $joi->uri;
240
241 Sets "format" to uri.
242
244 JSON::Validator
245
246 <https://github.com/hapijs/joi>.
247
248
249
250perl v5.28.1 2019-01-20 JSON::Validator::Joi(3)