Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Validating a JSON structure

by sherab (Scribe)
on Sep 04, 2012 at 18:17 UTC ( #991672=perlquestion: print w/ replies, xml ) Need Help??
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......
#!/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.

Comment on Validating a JSON structure
Download Code
Re: Validating a JSON structure
by rpnoble419 (Pilgrim) on Sep 04, 2012 at 22:41 UTC

    What are you testing for?

    Are you testing to see if the JSON object is valid?

    Are you testing to see if certain data elements are present?

    Does the JSON object you are looking for have to match exactly a given construct?

    Each case in the above requires a different method to test for...

      Well formed JSON testing is easy enough to check for (Test::JSON, thanks Poe!) but what I am after is if the JSON matches a very strict definition.
      - Lack of elements, fail
      - Additional elements, fail

      If the test doesn't pass then we can put some eyeballs on it.
Re: Validating a JSON structure
by remiah (Hermit) on Sep 05, 2012 at 07:06 UTC

    Maybe this will work.

    my $json_model ={ type => 'object', properties => { year => {type=>'number', minimum=>0, maximum=>9999, required=>1 +}, month => {type=>'number', minimum=>1, maximum=>12, required=>1}, }, additionalProperties =>0 };
    The author of this module is around here, Mr.Tobyink.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991672]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (18)
As of 2014-09-19 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (142 votes), past polls