Thanks
1nickt,
This looks like it will work - I've been able to figure out most from the docs (I really wish module creators would have real-life examples, but anyway).
Is there a way to do an array count validation?:
use strict; use warnings;
package JSONTypes {
use parent 'Type::Library';
use Type::Utils;
use Types::Standard qw/ Dict Enum Str Tuple ArrayRef Optional Rege
+xpRef Int /;
declare JSONType => as Dict[
method => Enum[qw(xxx yyy)],
backend => Dict[
num => Int,
other => Optional[Str],
StrMatch[qr/^[a-z0-9]+$/]
],
# following works, but how to check that the array has at least
+ (say) 2 elements - {2,}
clientArr => ArrayRef[
Dict[ num => Int, ],
]
];
};
package main {
use JSON;
JSONTypes->import(qw/ +JSONType /);
my $data = from_json q|{
"method": "xxx",
"backend": {
"num": 1,
"client": "mytest009b"
},
"clientArr": [{ "num": "1" }, { "num": "2" }]
}|;
my $ret = eval { assert_JSONType( $data ); };
if ($@) {
print "ERROR\n$@\n";
}
else {
print "OK\n";
}
};
I'd appreciate any pointers.
Thanks
Edit: Figured out regex - use StrMatch[]
Edit 2: Validating array count can be done with an anon sub, but it then kills the other check:
clientArr => ArrayRef[
Dict[ num => Int, ],
],
clientArr => sub {
if (scalar @$_ < 2) {
print "ERROR: clientArr too small\n";
exit 1;
}
else {
}
return 1;
}
So I guess the question is how to combine those two checks (without having to hand code the num=>Int checks in pure Perl of course).
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.