NAME
JSV::Compiler - Translates JSON-Schema validation rules (draft-06) into perl code
SYNOPSIS
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
DESCRIPTION
JSV::Compiler makes validation subroutine body in perl. You can then use it to embed in your own validation functions.
METHODS
load_schema($file|$hash)
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
.
new
compile(%opts)
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.
- coersion => true|false
- to_json => true|false
- input_symbole => string to use for rood data structure access
SUPPORTED KEYWORDS
Following keywords are supported:
- multipleOf
- maximum
- exclusiveMaximum
- minimum
- exclusiveMinimum
- maxLength
- minLength
- pattern
- items
- maxItems
- minItems
- uniqueItems
- maxProperties
- minProperties
- required
- properties
- patternProperties
- additionalProperties
- enum
- const
- type (single value)
- allOf
- anyOf
- oneOf
- not
- default
SEE ALSO
BUGS
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.
It doesn't support contains schema keyword. Almost everything else should be working.
NOT YET SUPPORTED KEYWORDS
- additionalItems
- contains
- propertyNames
LICENSE
Copyright (C) Anton Petrusevich
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHOR
Anton Petrusevich <antonpetr@cpan.org>