Types::XSD::Lite(3pm) | User Contributed Perl Documentation | Types::XSD::Lite(3pm) |
Types::XSD::Lite - type constraints based on a subset of XML schema datatypes
package Person; use Moo; use Types::XSD::Lite qw( PositiveInteger String ); has name => (is => "ro", isa => String[ minLength => 1 ]); has age => (is => "ro", isa => PositiveInteger);
These are all the type constraints from XML Schema that could be implemented without introducing extra runtime dependencies (above Type::Tiny). That's basically all of the XSD types, except datetime-related ones, and XML-specific ones (QNames, IDRefs, etc).
If you want the full set of XML Schema types, see Types::XSD.
I've added some quick explanations of what each type is, but for details, see the XML Schema specification.
Gotcha: The string "false" evaluates to true in Perl. You probably want to use "Bool" from Types::Standard instead.
Gotcha: If you parameterize this with "length", "maxLength" or "minLength", it is the length of the decoded string which will be checked.
Gotcha: If you parameterize this with "length", "maxLength" or "minLength", it is the length of the decoded string which will be checked.
Datatypes can be parameterized using the facets defined by XML Schema. For example:
use Types::XSD::Lite qw( String Decimal PositiveInteger Token ); my @sizes = qw( XS S M L XL XXL ); has name => (is => "ro", isa => String[ minLength => 1 ]); has price => (is => "ro", isa => Decimal[ fractionDigits => 2 ]); has rating => (is => "ro", isa => PositiveInteger[ maxInclusive => 5 ]); has size => (is => "ro", isa => Token[ enumeration => \@sizes ]);
The following facets exist, but not all facets are supported for all datatypes. (The module will croak if you try to use an unsupported facet.)
Token[ pattern => qr{^[a-z]+$} ]
For example:
Integer[ assertions => [ '$_ % 3 == 0', # multiple of three, and... sub { is_nice($_) }, # is nice (whatever that means) ], ],
Strings of Perl code will result in faster-running type constraints.
Types::XSD::Lite won't prevent you from making ridiculous constraints such as "String[ maxLength => 1, minLength => 2 ]".
Note that on "HexBinary" and "Base64Binary" types, the lengths apply to the decoded string. Length restrictions are silently ignored for "QName" and "Notation" because the W3C doesn't think you should care what length these datatypes are.
Note that to be super-correct, the "{max,min}{Inclusive,Exclusive}" facets for numeric types are performed by passing the numbers through Math::BigInt or Math::BigFloat, so may be a little slow.
This distribution has virtually no test suite, in the hope that Types::XSD's test suite will shake out any bugs in this module.
Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=Types-XSD-Lite>.
Type::Tiny, Types::Standard, Types::XSD.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013-2014 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2014-04-12 | perl v5.20.1 |