Hey folks,
I am writing tests for perl and what I am after is validating a JSON structure. I stumbled across the module JSON::Schema but it doesn't actually seem to check the Schema. Just be sure I am seeing what I am seeing I submit to you enlightened monks......
#!/usr/bin/perl
use JSON;
use JSON::Schema;
# Here's a structure to validate against...
my $json_model ={
type => 'object',
properties => {
year => {type=>'number', minimum=>0, maximum=>9999}
+,
month => {type=>'number', minimum=>1, maximum=>12},
}
};
#And now here's some bad data. monkeys do not belong (silly monkeys!)
my $test = to_json({
year => '1999',
monkeys => '10'
});
my $validator = JSON::Schema->new($json_model);
# Validate:
my $valid = $validator->validate($test);
if ($valid)
{
print "Yay!\n";
exit;
}
# But it's not valid...
foreach my $e ($valid->errors)
{
print "Naughty! $e\n";
}
I have defined the structure to validate against but clearly $test, with its "monkeys" element clearly does not fit but it seems to validate just fine.
Can anyone verify this? Also do you have a suggestion for validating a JSON structure since JSON::Schema seems to be
..... errrr .... unreliable? reliable and I'm missing something.
Many many thanks!
JC
PS - In cases where the structures do match, JSON::Schema seems to be excellent catching it.
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.