Debian Codegraph @

README

raw
1 NAME
2 Types::XSD - type constraints based on XML schema datatypes
3
4 SYNOPSIS
5 package Person;
6
7 use Moo;
8 use Types::XSD qw( PositiveInteger String );
9
10 has name => (is => "ro", isa => String[ minLength => 1 ]);
11 has age => (is => "ro", isa => PositiveInteger);
12
13 DESCRIPTION
14 Types::XSD is a type constraint library inspired by XML Schema, and built
15 with Type::Library. It can be used as a type constraint library for Moo,
16 Mouse or Moose, or used completely independently of any OO framework.
17
18 This module is an extension of Types::XSD::Lite (which has fewer type
19 constraints, but fewer dependencies). For completeness, the type
20 constraints and other features inherited from Types::XSD::Lite are
21 documented below too.
22
23 Type Constraints
24 This module defines the following type constraints based on the data types
25 defined in XML Schema <http://www.w3.org/TR/xmlschema-2/>. (The names of
26 the type constraints are the same as the XML Schema data types, but
27 capitalization often differs.)
28
29 I've added some quick explainations of what each type is, but for details,
30 see the XML Schema specification.
31
32 `AnyType`
33 As per `Any` from Types::Standard.
34
35 `AnySimpleType`
36 As per `Value` from Types::Standard.
37
38 `String`
39 As per `Str` from Types::Standard.
40
41 `NormalizedString`
42 A string containing no line breaks, carriage returns or tabs.
43
44 `Token`
45 Like `NormalizedString`, but also no leading or trailing space, and no
46 doubled spaces (i.e. not `/\s{2,}/`).
47
48 `Language`
49 An RFC 3066 language code.
50
51 `Name`
52 Something that could be a valid XML element or attribute name. These
53 roughly correspond to Perl identifiers but may also contain colons,
54 hyphens and stops. (Digits, hyphens and stops are not allowed as the
55 first character.)
56
57 `NmToken`
58 Slightly looser version of `Name`; allows digits, hyphens and stops in
59 the first character.
60
61 `NmTokens`
62 Space-separated list of `NmToken`.
63
64 `NCName`
65 Slightly tighter vesion of `Name`; disallows colons.
66
67 `Id`
68 Effectively the same as `NCName`.
69
70 `IdRef`
71 Effectively the same as `NCName`.
72
73 `IdRefs`
74 Space-separated list of `IdRef`.
75
76 `Entity`
77 Effectively the same as `NCName`.
78
79 `Entities`
80 Space-separated list of `Entity`.
81
82 `Boolean`
83 Allows "true", "false", "1" and "0" (case-insensitively).
84
85 Gotcha: The string "false" evaluates to true in Perl. You probably
86 want to use `Bool` from Types::Standard instead.
87
88 `Base64Binary`
89 Strings which are valid Base64 data. Allows whitespace.
90
91 Gotcha: If you parameterize this with `length`, `maxLength` or
92 `minLength`, it is the length of the *decoded* string which will be
93 checked.
94
95 `HexBinary`
96 Strings which are valid hexadecimal data. Disallows whitespace;
97 disallows leading `0x`.
98
99 Gotcha: If you parameterize this with `length`, `maxLength` or
100 `minLength`, it is the length of the *decoded* string which will be
101 checked.
102
103 `Float`
104 As per `Num` from Types::Standard.
105
106 `Double`
107 As per `Num` from Types::Standard.
108
109 `AnyURI`
110 Any absolute *or relative* URI. Effectively, any string at all!
111
112 `QName`
113 An XML QName; something that could be used as a valid element name in
114 a namespaced XML document.
115
116 Gotcha: while `length`, `maxLength` and `minLength` are allowed facets
117 for parameterization, they are silently ignored, as per the
118 specification!
119
120 `Notation`
121 Effectively the same as `QName`. According to XML Schema, this is
122 *always* supposed to be parameterized with an enumeration. But we
123 don't enforce that.
124
125 Gotcha: while `length`, `maxLength` and `minLength` are allowed facets
126 for parameterization, they are silently ignored, as per the
127 specification!
128
129 `Decimal`
130 Numbers possibly including a decimal point, but not allowing
131 exponential notation (e.g. "3.14e-3").
132
133 `Integer`
134 As per `Int` from Types::Standard.
135
136 `NonPositiveInteger`
137 An `Integer` 0 or below.
138
139 `NegativeInteger`
140 An `Integer` -1 or below.
141
142 `Long`
143 An `Integer` between -9223372036854775808 and 9223372036854775807
144 (inclusive).
145
146 `Int`
147 An `Integer` between -2147483648 and 2147483647 (inclusive).
148
149 `Short`
150 An `Integer` between -32768 and 32767 (inclusive).
151
152 `Byte`
153 An `Integer` between -128 and 127 (inclusive).
154
155 `NonNegativeInteger`
156 An `Integer` 0 or above.
157
158 `PositiveInteger`
159 An `Integer` 1 or above.
160
161 `UnsignedLong`
162 A `NonNegativeInteger` between 0 and 18446744073709551615 (inclusive).
163
164 `UnsignedInt`
165 A `NonNegativeInteger` between 0 and 4294967295 (inclusive).
166
167 `UnsignedShort`
168 A `NonNegativeInteger` between 0 and 65535 (inclusive).
169
170 `UnsignedByte`
171 A `NonNegativeInteger` between 0 and 255 (inclusive).
172
173 `Duration`
174 An ISO 8601 duration.
175
176 `YearMonthDuration`
177 An ISO 8601 duration restricted to cover only years and months.
178
179 `DayTimeDuration`
180 An ISO 8601 duration restricted to cover only days, hours, minutes and
181 seconds. (Note that this still permits durations of many years, as the
182 days component is an arbitrary non-negative integer.)
183
184 `DateTime`
185 An ISO 8601 datetime with optional timezone.
186
187 `DateTimeStamp`
188 An ISO 8601 datetime with required timezone.
189
190 `Time`
191 An ISO 8601 time with optional timezone.
192
193 `Date`
194 An ISO 8601 date with optional timezone.
195
196 `GYearMonth`
197 An year-month pair with optional timezone.
198
199 `GYear`
200 An year with optional timezone.
201
202 `GMonthDay`
203 An month-day pair with optional timezone.
204
205 `GDay`
206 An day with optional timezone.
207
208 `GMonth`
209 An month with optional timezone.
210
211 Parameters
212 Datatypes can be parameterized using the facets defined by XML Schema. For
213 example:
214
215 use Types::XSD qw( String Decimal PositiveInteger Token );
216
217 my @sizes = qw( XS S M L XL XXL );
218
219 has name => (is => "ro", isa => String[ minLength => 1 ]);
220 has price => (is => "ro", isa => Decimal[ fractionDigits => 2 ]);
221 has rating => (is => "ro", isa => PositiveInteger[ maxInclusive => 5 ]);
222 has size => (is => "ro", isa => Token[ enumeration => \@sizes ]);
223
224 The following facets exist, but not all facets are supported for all
225 datatypes. (The module will croak if you try to use an unsupported facet.)
226
227 `enumeration`
228 An arrayref of allowable values. You should probably use
229 Type::Tiny::Enum instead.
230
231 `pattern`
232 A regular expression that the value is expected to conform to. Use a
233 normal Perl quoted regexp:
234
235 Token[ pattern => qr{^[a-z]+$} ]
236
237 `whiteSpace`
238 The `whiteSpace` facet is ignored as I'm not entirely sure what it
239 should do. It perhaps makes sense for coercions, but this module
240 doesn't define any coercions.
241
242 `assertions`
243 An arrayref of arbitrary additional restrictions, expressed as strings
244 of Perl code or coderefs operating on $_.
245
246 For example:
247
248 Integer[
249 assertions => [
250 '$_ % 3 == 0', # multiple of three, and...
251 sub { is_nice($_) }, # is nice (whatever that means)
252 ],
253 ],
254
255 Strings of Perl code will result in faster-running type constraints.
256
257 `length`, `maxLength`, `minLength`
258 Restrict the length of a value. For example `Integer[length=>2]`
259 allows 10, 99 and -1, but not 100, 9 or -10.
260
261 Types::XSD won't prevent you from making ridiculous constraints such
262 as `String[ maxLength => 1, minLength => 2 ]`.
263
264 Note that on `HexBinary` and `Base64Binary` types, the lengths apply
265 to the decoded string. Length restrictions are silently ignored for
266 `QName` and `Notation` because the W3C doesn't think you should care
267 what length these datatypes are.
268
269 `maxInclusive`, `minInclusive`, `maxExclusive`, `minExclusive`
270 Supported for numeric types and datetime/duration-related types.
271
272 Note that to be super-correct, the `{max,min}{Inclusive,Exclusive}`
273 facets for numeric types are performed by passing the numbers through
274 Math::BigInt or Math::BigFloat, so may be a little slow.
275
276 `totalDigits`
277 For a decimal (or type derived from decimals) specifies that the total
278 number of digits for the value must be at most this number. Given
279 `Decimal[ totalDigits => 3 ]`, 1.23, 12.3, 123, 1.2 and 1 are all
280 allowable; 1.234 is not. 1.230 is also not, but this may change in a
281 future version.
282
283 `fractionDigits`
284 Like `totalDigits` but ignores digits before the decimal point.
285
286 `explicitTimezone`
287 May be "optional", "prohibited" or "required". For example:
288
289 Time[ explicitTimezone => "prohibited" ]
290
291 Functions
292 This module also exports some convenience functions:
293
294 `dur_parse($str)`
295 Parse an xsd:duration string, returning a DateTime::Duration.
296
297 `dur_cmp($a, $b)`
298 Compare two strings conforming to the xsd:duration datatype to
299 indicate which is the longer duration.
300
301 Returns -1 if $a is shorter. Returns 1 if $b is shorter. Returns 0 if
302 the durations are identical. Returns undef if the comparison is
303 indeterminate; for example, "P1Y" (one year) and "P365D" (365 days)
304 are not necessarily identical - in leap years "P365D" is shorter.
305
306 `dt_cmp($type, $a, $b)`
307 Compare two datetime-like strings. For example, two `gYearMonth`
308 strings can be compared using:
309
310 dt_cmp(GYearMonth, "2009-02", "2010-10");
311
312 Both strings are expected to conform to the same datatype. It doesn't
313 make much sense to compare them otherwise.
314
315 `dt_parse($type, $str)`
316 Parse a datetime-like string, returning a DateTime::Incomplete object.
317 Note that DateTime::Incomplete objects are always returned, even if
318 the datetime is potentially complete.
319
320 BUGS
321 Please report any bugs to
322 <http://rt.cpan.org/Dist/Display.html?Queue=Types-XSD>.
323
324 SEE ALSO
325 Type::Tiny, Types::XSD::Lite, Types::Standard.
326
327 * <http://www.w3.org/TR/xmlschema-2/> Datatypes in XML Schema 1.0
328
329 * <http://www.w3.org/TR/xmlschema11-2/> Datatypes in XML Schema 1.1
330
331 AUTHOR
332 Toby Inkster <[email protected]>.
333
334 COPYRIGHT AND LICENCE
335 This software is copyright (c) 2013-2014, 2021 by Toby Inkster.
336
337 This is free software; you can redistribute it and/or modify it under the
338 same terms as the Perl 5 programming language system itself.
339
340 DISCLAIMER OF WARRANTIES
341 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
342 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
343 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
344