NAME
JSON::Schema::AsType - generates Type::Tiny types out of JSON schemas
VERSION
version 0.2.0
SYNOPSIS
use JSON::Schema::AsType;
my $schema = JSON::Schema::AsType->new( schema => {
properties => {
foo => { type => 'integer' },
bar => { type => 'object' },
},
});
print 'valid' if $schema->check({ foo => 1, bar => { two => 2 } }); # prints 'valid'
print $schema->validate_explain({ foo => 'potato', bar => { two => 2 } });
DESCRIPTION
This module takes in a JSON Schema (http://json-schema.org/) and turns it into a Type::Tiny type.
METHODS
new( %args )
my $schema = JSON::Schema::AsType->new( schema => $my_schema );
The class constructor. Accepts the following arguments.
- schema => \%schema
-
The JSON schema to compile, as a hashref.
If not given, will be retrieved from
uri
.An error will be thrown is neither
schema
noruri
is given. - uri => $uri
-
Optional uri associated with the schema.
If provided, the schema will also be added to a schema cache. There is currently no way to prevent this. If this is an issue for you, you can manipulate the cache by accessing
%JSON::Schema::AsType::EXTERNAL_SCHEMAS
directly. - specification => $version
-
The version of the JSON-Schema specification to use. Defaults to 'draft4' (and doesn't accept anything else at the moment).
type
Returns the compiled Type::Tiny type.
check( $struct )
Returns true
if $struct
is valid as per the schema.
validate( $struct )
Returns a short explanation if $struct
didn't validate, nothing otherwise.
validate_explain( $struct )
Returns a log explanation if $struct
didn't validate, nothing otherwise.
validate_schema
Like validate
, but validates the schema itself against its specification.
print $schema->validate_schema;
# equivalent to
print $schema->specification_schema->validate($schema);
validate_explain_schema
Like validate_explain
, but validates the schema itself against its specification.
schema
Returns the JSON schema, as a hashref.
parent_schema
Returns the JSON::Schema::AsType object for the parent schema, or undef
is the current schema is the top-level one.
fetch( $url )
Fetches the schema at the given $url
. If already present, it will use the schema in the cache. If not, the newly fetched schema will be added to the cache.
uri
Returns the uri associated with the schema, if any.
specification
Returns the JSON Schema specification used by the object.
specification_schema
Returns the JSON::Schema::AsType object representing the schema of the current object's specification.
root_schema
Returns the top-level schema including this schema.
is_root_schema
Returns true
if this schema is a top-level schema.
resolve_reference( $ref )
my $sub_schema = $schema->resolve_reference( '#/properties/foo' );
print $sub_schema->check( $struct );
Returns the JSON::Schema::AsType object associated with the type referenced by $ref
.
SEE ALSO
AUTHOR
Yanick Champoux <yanick@babyl.dyndns.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by Yanick Champoux.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.