The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

JSV::Compiler - Translates JSON-Schema validation rules (draft-06) into perl code

SYNOPSIS

  use JSV::Compiler;
 
  my $jsv = JSV::Compiler->new;
  $jsv->load_schema({
    type => "object",
    properties => {
      foo => { type => "integer" },
      bar => { type => "string" }
    },
    required => [ "foo" ]
  });
  my $vcode = $jsv->compile();
  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)

new

compile(%opts)

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

http://json-schema.org/
https://github.com/json-schema/JSON-Schema-Test-Suite

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>