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).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.