| 1 |
NAME |
| 2 |
Types::XSD::Lite - type constraints based on a subset of XML schema |
| 3 |
datatypes |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
package Person; |
| 7 |
|
| 8 |
use Moo; |
| 9 |
use Types::XSD::Lite qw( PositiveInteger String ); |
| 10 |
|
| 11 |
has name => (is => "ro", isa => String[ minLength => 1 ]); |
| 12 |
has age => (is => "ro", isa => PositiveInteger); |
| 13 |
|
| 14 |
DESCRIPTION |
| 15 |
These are all the type constraints from XML Schema that could be |
| 16 |
implemented without introducing extra runtime dependencies (above |
| 17 |
Type::Tiny). That's basically all of the XSD types, except |
| 18 |
datetime-related ones, and XML-specific ones (QNames, IDRefs, etc). |
| 19 |
|
| 20 |
If you want the full set of XML Schema types, see Types::XSD. |
| 21 |
|
| 22 |
Type Constraints |
| 23 |
I've added some quick explanations of what each type is, but for details, |
| 24 |
see the XML Schema specification. |
| 25 |
|
| 26 |
`AnyType` |
| 27 |
As per `Any` from Types::Standard. |
| 28 |
|
| 29 |
`AnySimpleType` |
| 30 |
As per `Value` from Types::Standard. |
| 31 |
|
| 32 |
`String` |
| 33 |
As per `Str` from Types::Standard. |
| 34 |
|
| 35 |
`NormalizedString` |
| 36 |
A string containing no line breaks, carriage returns or tabs. |
| 37 |
|
| 38 |
`Token` |
| 39 |
Like `NormalizedString`, but also no leading or trailing space, and no |
| 40 |
doubled spaces (i.e. not `/\s{2,}/`). |
| 41 |
|
| 42 |
`Language` |
| 43 |
An RFC 3066 language code. |
| 44 |
|
| 45 |
`Boolean` |
| 46 |
Allows "true", "false", "1" and "0" (case-insensitively). |
| 47 |
|
| 48 |
Gotcha: The string "false" evaluates to true in Perl. You probably |
| 49 |
want to use `Bool` from Types::Standard instead. |
| 50 |
|
| 51 |
`Base64Binary` |
| 52 |
Strings which are valid Base64 data. Allows whitespace. |
| 53 |
|
| 54 |
Gotcha: If you parameterize this with `length`, `maxLength` or |
| 55 |
`minLength`, it is the length of the *decoded* string which will be |
| 56 |
checked. |
| 57 |
|
| 58 |
`HexBinary` |
| 59 |
Strings which are valid hexadecimal data. Disallows whitespace; |
| 60 |
disallows leading `0x`. |
| 61 |
|
| 62 |
Gotcha: If you parameterize this with `length`, `maxLength` or |
| 63 |
`minLength`, it is the length of the *decoded* string which will be |
| 64 |
checked. |
| 65 |
|
| 66 |
`Float` |
| 67 |
As per `Num` from Types::Standard. |
| 68 |
|
| 69 |
`Double` |
| 70 |
As per `Num` from Types::Standard. |
| 71 |
|
| 72 |
`AnyURI` |
| 73 |
Any absolute *or relative* URI. Effectively, any string at all! |
| 74 |
|
| 75 |
`Decimal` |
| 76 |
Numbers possibly including a decimal point, but not allowing |
| 77 |
exponential notation (e.g. "3.14e-3"). |
| 78 |
|
| 79 |
`Integer` |
| 80 |
As per `Int` from Types::Standard. |
| 81 |
|
| 82 |
`NonPositiveInteger` |
| 83 |
An `Integer` 0 or below. |
| 84 |
|
| 85 |
`NegativeInteger` |
| 86 |
An `Integer` -1 or below. |
| 87 |
|
| 88 |
`Long` |
| 89 |
An `Integer` between -9223372036854775808 and 9223372036854775807 |
| 90 |
(inclusive). |
| 91 |
|
| 92 |
`Int` |
| 93 |
An `Integer` between -2147483648 and 2147483647 (inclusive). |
| 94 |
|
| 95 |
`Short` |
| 96 |
An `Integer` between -32768 and 32767 (inclusive). |
| 97 |
|
| 98 |
`Byte` |
| 99 |
An `Integer` between -128 and 127 (inclusive). |
| 100 |
|
| 101 |
`NonNegativeInteger` |
| 102 |
An `Integer` 0 or above. |
| 103 |
|
| 104 |
`PositiveInteger` |
| 105 |
An `Integer` 1 or above. |
| 106 |
|
| 107 |
`UnsignedLong` |
| 108 |
A `NonNegativeInteger` between 0 and 18446744073709551615 (inclusive). |
| 109 |
|
| 110 |
`UnsignedInt` |
| 111 |
A `NonNegativeInteger` between 0 and 4294967295 (inclusive). |
| 112 |
|
| 113 |
`UnsignedShort` |
| 114 |
A `NonNegativeInteger` between 0 and 65535 (inclusive). |
| 115 |
|
| 116 |
`UnsignedByte` |
| 117 |
A `NonNegativeInteger` between 0 and 255 (inclusive). |
| 118 |
|
| 119 |
Parameters |
| 120 |
Datatypes can be parameterized using the facets defined by XML Schema. For |
| 121 |
example: |
| 122 |
|
| 123 |
use Types::XSD::Lite qw( String Decimal PositiveInteger Token ); |
| 124 |
|
| 125 |
my @sizes = qw( XS S M L XL XXL ); |
| 126 |
|
| 127 |
has name => (is => "ro", isa => String[ minLength => 1 ]); |
| 128 |
has price => (is => "ro", isa => Decimal[ fractionDigits => 2 ]); |
| 129 |
has rating => (is => "ro", isa => PositiveInteger[ maxInclusive => 5 ]); |
| 130 |
has size => (is => "ro", isa => Token[ enumeration => \@sizes ]); |
| 131 |
|
| 132 |
The following facets exist, but not all facets are supported for all |
| 133 |
datatypes. (The module will croak if you try to use an unsupported facet.) |
| 134 |
|
| 135 |
`enumeration` |
| 136 |
An arrayref of allowable values. You should probably use |
| 137 |
Type::Tiny::Enum instead. |
| 138 |
|
| 139 |
`pattern` |
| 140 |
A regular expression that the value is expected to conform to. Use a |
| 141 |
normal Perl quoted regexp: |
| 142 |
|
| 143 |
Token[ pattern => qr{^[a-z]+$} ] |
| 144 |
|
| 145 |
`whiteSpace` |
| 146 |
The `whiteSpace` facet is ignored as I'm not entirely sure what it |
| 147 |
should do. It perhaps makes sense for coercions, but this module |
| 148 |
doesn't define any coercions. |
| 149 |
|
| 150 |
`assertions` |
| 151 |
An arrayref of arbitrary additional restrictions, expressed as strings |
| 152 |
of Perl code or coderefs operating on $_. |
| 153 |
|
| 154 |
For example: |
| 155 |
|
| 156 |
Integer[ |
| 157 |
assertions => [ |
| 158 |
'$_ % 3 == 0', # multiple of three, and... |
| 159 |
sub { is_nice($_) }, # is nice (whatever that means) |
| 160 |
], |
| 161 |
], |
| 162 |
|
| 163 |
Strings of Perl code will result in faster-running type constraints. |
| 164 |
|
| 165 |
`length`, `maxLength`, `minLength` |
| 166 |
Restrict the length of a value. For example `Integer[length=>2]` |
| 167 |
allows 10, 99 and -1, but not 100, 9 or -10. |
| 168 |
|
| 169 |
Types::XSD::Lite won't prevent you from making ridiculous constraints |
| 170 |
such as `String[ maxLength => 1, minLength => 2 ]`. |
| 171 |
|
| 172 |
Note that on `HexBinary` and `Base64Binary` types, the lengths apply |
| 173 |
to the decoded string. Length restrictions are silently ignored for |
| 174 |
`QName` and `Notation` because the W3C doesn't think you should care |
| 175 |
what length these datatypes are. |
| 176 |
|
| 177 |
`maxInclusive`, `minInclusive`, `maxExclusive`, `minExclusive` |
| 178 |
Supported for numeric types and datetime/duration-related types. |
| 179 |
|
| 180 |
Note that to be super-correct, the `{max,min}{Inclusive,Exclusive}` |
| 181 |
facets for numeric types are performed by passing the numbers through |
| 182 |
Math::BigInt or Math::BigFloat, so may be a little slow. |
| 183 |
|
| 184 |
`totalDigits` |
| 185 |
For a decimal (or type derived from decimals) specifies that the total |
| 186 |
number of digits for the value must be at most this number. Given |
| 187 |
`Decimal[ totalDigits => 3 ]`, 1.23, 12.3, 123, 1.2 and 1 are all |
| 188 |
allowable; 1.234 is not. 1.230 is also not, but this may change in a |
| 189 |
future version. |
| 190 |
|
| 191 |
`fractionDigits` |
| 192 |
Like `totalDigits` but ignores digits before the decimal point. |
| 193 |
|
| 194 |
CAVEATS |
| 195 |
This distribution has virtually no test suite, in the hope that |
| 196 |
Types::XSD's test suite will shake out any bugs in this module. |
| 197 |
|
| 198 |
BUGS |
| 199 |
Please report any bugs to |
| 200 |
<http://rt.cpan.org/Dist/Display.html?Queue=Types-XSD-Lite>. |
| 201 |
|
| 202 |
SEE ALSO |
| 203 |
Type::Tiny, Types::Standard, Types::XSD. |
| 204 |
|
| 205 |
* <http://www.w3.org/TR/xmlschema-2/> Datatypes in XML Schema 1.0 |
| 206 |
|
| 207 |
* <http://www.w3.org/TR/xmlschema11-2/> Datatypes in XML Schema 1.1 |
| 208 |
|
| 209 |
AUTHOR |
| 210 |
Toby Inkster <[email protected]>. |
| 211 |
|
| 212 |
COPYRIGHT AND LICENCE |
| 213 |
This software is copyright (c) 2013-2014 by Toby Inkster. |
| 214 |
|
| 215 |
This is free software; you can redistribute it and/or modify it under the |
| 216 |
same terms as the Perl 5 programming language system itself. |
| 217 |
|
| 218 |
DISCLAIMER OF WARRANTIES |
| 219 |
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 220 |
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 221 |
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 222 |
|