JSV::Compiler - Translates JSON-Schema validation rules (draft-06) into perl code
use JSV::Compiler; use Module::Load; my $jsv = JSV::Compiler->new; $jsv->load_schema({ type => "object", properties => { foo => { type => "integer" }, bar => { type => "string" } }, required => [ "foo" ] }); my ($vcode, %load) = $jsv->compile(); for my $m (keys %load) { load $m, @{$load{$m}} ? @{$load{$m}} : (); } my $test_sub_txt = <<"SUB"; sub { my \$errors = []; $vcode; print "\@\$errors\\n" if \@\$errors; print "valid\n" if \@\$errors == 0; \@\$errors == 0; } SUB my $test_sub = eval $test_sub_txt; $test_sub->({}); # foo is required $test_sub->({ foo => 1 }); # valid $test_sub->({ foo => 10, bar => "xyz" }); # valid $test_sub->({ foo => 1.2, bar => "xyz" }); # foo does not look like integer number
JSV::Compiler makes validation subroutine body in perl. You can then use it to embed in your own validation functions.
Loads and registers schema. In list context returns list of URLs of unresolved schemas. You should load all unresolved schemas and then load this one more time. In scalar context returns $self.
$self
my ($vcode, %load) = $jsv->compile(); for my $m (keys %load) { load $m, @{$load{$m}} ? @{$load{$m}} : (); }
Returns compiled perl text. In list context it adds list of required modules with array of their required import symbols.
Following keywords are supported:
It doesn't support all features of draft-06. For example, it doesn't support array of types and some type checks work in a little bit another way: every number in Perl is also string and type => "string" will be true for numbers.
type => "string"
It doesn't support contains schema keyword. Almost everything else should be working.
Copyright (C) Anton Petrusevich
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Anton Petrusevich <antonpetr@cpan.org>
To install JSV::Compiler, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSV::Compiler
CPAN shell
perl -MCPAN -e shell install JSV::Compiler
For more information on module installation, please visit the detailed CPAN module installation guide.