I need to validate json files with json-schama in perl script. But unfortunately perl module JSON::Schema does not support the current version of json schema.

Cool, an opportunity to fix/improve something

Is there any way of validation json with json-schema draft 4 in perl?

Yes, SMOP :D

I have created a sample repo that shows the problem: https://github.com/bessarabov/playing_with_json_schema/

Ah yes, code on github, that uses "doker" that depends on npm ... the perfect and most simple and self contained way to demonstrate a problem ... directest way possible to ask perlmonks for help

http://search.cpan.org/grep?cpanid=TOBYINK&release=JSON-Schema-0.016&string=required&i=1&n=1&C=0

https://metacpan.org/source/TOBYINK/JSON-Schema-0.016/t/03required.t

#!/usr/bin/perl -- use strict; use warnings; use JSON::Schema; use JSON::Hyper; use JSON; use Data::Dump qw/ dd /; #~ http://cpansearch.perl.org/src/TOBYINK/JSON-Schema-0.016/examples/v +alidation.pl $JSON::Hyper::DEBUG = 1; my $schema = q{{ "properties" : { "name" : { "type" : "string" }, "type" : { "enum" : [ "dog", "cat" ], "type" : "string" } }, "required" : [ "name", "type" ], "type" : "object" }}; my $data = q{{ "nickname" : "Buddy", "type" : "dog" }}; ## hack my $validator = JSON::Schema->new( $schema ); for my $prop ( @{ $validator->{schema}{required} } ){ $validator->{schema}{properties}{$prop}{required}++; } my $json = decode_json( $data ); my $result = $validator->validate( $json ); dd( $validator, $json, $result ); print "$_\n" foreach $result->errors; ## wanted #~ [ { keyword: 'required', #~ dataPath: '', #~ params: { missingProperty: 'name' }, #~ message: 'should have required property \'name\'' } ] #~ #~ #~ __END__ ( bless({ format => {}, schema => { properties => { name => { required => 1, type => "string" }, type => { enum => ["dog", "cat"], required => 1, typ +e => "string" }, }, required => ["name", "type"], type => "object", }, }, "JSON::Schema"), { nickname => "Buddy", type => "dog" }, bless({ errors => [ { message => "is missing and it is required", property + => "\$.name" }, ], valid => "", }, "JSON::Schema::Result"), ) $.name: is missing and it is required

In reply to Re: How to validate json with json-schema draft 4? (jokes) by Anonymous Monk
in thread How to validate json with json-schema draft 4? by bessarabov

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.