Types::XSD::Lite(3pm)
π NAME π
Types::XSD::Lite β type constraints based on a subset of XML schema datatypes. It's the Types::XSD::Lite manual page bestie, and it's lowkey iconic β¨
πΈ SYNOPSIS πΈ
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);
π¦ DESCRIPTION π¦
Okay so these are all the type constraints from XML Schema that could be implemented without dragging in any extra runtime dependencies (above Type::Tiny obviously π
). That's basically all of the XSD types, except the datetime-related ones, and the XML-specific ones (QNames, IDRefs, etc). Not those ones, they're not invited to the party π
If you want the full set of XML Schema types, go see Types::XSD.
π Type Constraints π
I've added some quick explanations of what each type is, but for the deets, see the XML Schema specification. iykyk π
AnyTypeβ As perAnyfromTypes::Standard.AnySimpleTypeβ As perValuefromTypes::Standard.Stringβ As perStrfromTypes::Standard.NormalizedStringβ A string containing no line breaks, no carriage returns, no tabs.Tokenβ LikeNormalizedString, but also no leading or trailing space, and no doubled spaces (i.e. not/\s{2,}/).Languageβ An RFC 3066 language code.Booleanβ Allowstrue,false,1and0(case-insensitively).
Gotcha π the stringfalseevaluates to true in Perl. You probably wantBoolfromTypes::Standardinstead bestie, fr fr.
Base64Binaryβ Strings which are valid Base64 data. Allows whitespace.
Gotcha π if you parameterize this withlength,maxLengthorminLength, it is the length of the decoded string which will be checked.
HexBinaryβ Strings which are valid hexadecimal data. Disallows whitespace; disallows leading0x.
Gotcha π if you parameterize this withlength,maxLengthorminLength, it is the length of the decoded string which will be checked.
Floatβ As perNumfromTypes::Standard.Doubleβ As perNumfromTypes::Standard.AnyURIβ Any absolute or relative URI. Effectively, any string at all! πDecimalβ Numbers possibly including a decimal point, but not allowing exponential notation (e.g.3.14e-3).Integerβ As perIntfromTypes::Standard.NonPositiveIntegerβ AnInteger0 or below.NegativeIntegerβ AnInteger-1 or below.Longβ AnIntegerbetween -9223372036854775808 and 9223372036854775807 (inclusive).Intβ AnIntegerbetween -2147483648 and 2147483647 (inclusive).Shortβ AnIntegerbetween -32768 and 32767 (inclusive).Byteβ AnIntegerbetween -128 and 127 (inclusive).NonNegativeIntegerβ AnInteger0 or above.PositiveIntegerβ AnInteger1 or above.UnsignedLongβ ANonNegativeIntegerbetween 0 and 18446744073709551615 (inclusive).UnsignedIntβ ANonNegativeIntegerbetween 0 and 4294967295 (inclusive).UnsignedShortβ ANonNegativeIntegerbetween 0 and 65535 (inclusive).UnsignedByteβ ANonNegativeIntegerbetween 0 and 255 (inclusive).
π Parameters π
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.) π€
enumerationβ An arrayref of allowable values. You should probably useType::Tiny::Enuminstead.patternβ A regular expression that the value is expected to conform to. Use a normal Perl quoted regexp:
Token[ pattern => qr{^[a-z]+$} ]
whiteSpaceβ ThewhiteSpacefacet is ignored as I'm not entirely sure what it should do. It perhaps makes sense for coercions, but this module doesn't define any coercions. π€·assertionsβ An arrayref of arbitrary additional restrictions, expressed as strings of Perl code or coderefs operating on$_.
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. β‘
length,maxLength,minLengthβ Restrict the length of a value. For exampleInteger[length=>2]allows 10, 99 and-1, but not 100, 9 or-10.
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. π
maxInclusive,minInclusive,maxExclusive,minExclusiveβ Supported for numeric types and datetime/duration-related types.
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. π
totalDigitsβ For a decimal (or type derived from decimals) specifies that the total number of digits for the value must be at most this number. GivenDecimal[ totalDigits => 3 ], 1.23, 12.3, 123, 1.2 and 1 are all allowable; 1.234 is not. 1.230 is also not, but this may change in a future version.fractionDigitsβ LiketotalDigitsbut ignores digits before the decimal point.
π CAVEATS π
This distribution has virtually no test suite, in the hope that Types::XSD's test suite will shake out any bugs in this module. living dangerously bestie πβ¨
π BUGS π
Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=Types-XSD-Lite>. do it, bestie, the author is literally asking π«Ά
π SEE ALSO π
Type::Tiny, Types::Standard, Types::XSD.
- <http://www.w3.org/TR/xmlschema-2/> Datatypes in XML Schema 1.0
- <http://www.w3.org/TR/xmlschema11-2/> Datatypes in XML Schema 1.1
π AUTHOR π
Toby Inkster <tobyink@cpan.org>. π
β COPYRIGHT AND LICENCE β
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. slay π
π§ DISCLAIMER OF WARRANTIES π§
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.
okay that's the whole vibe check, we ate and left no crumbs π This was Types::XSD::Lite(3pm) β part of perl v5.36.0, dated 2022-09-27 β and that's the tea. bye bestie, go parameterize responsibly π¦β¨π
π¦ where this page comes from
- ubuntu noble: libtypes-xsd-lite-perl 0.007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtypes-xsd-lite-perl 0.007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtypes-xsd-lite-perl 0.007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms