sherab has asked for the wisdom of the Perl Monks concerning the following question:
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......
..... 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.
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#!/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"; }
Many many thanks!
JC
PS - In cases where the structures do match, JSON::Schema seems to be excellent catching it.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Validating a JSON structure
by remiah (Hermit) on Sep 05, 2012 at 07:06 UTC | |
Re: Validating a JSON structure
by rpnoble419 (Pilgrim) on Sep 04, 2012 at 22:41 UTC | |
by sherab (Scribe) on Sep 05, 2012 at 13:13 UTC |
Back to
Seekers of Perl Wisdom