JSON::Schema::AsType - generates Type::Tiny types out of JSON schemas
version 0.2.0
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 } });
This module takes in a JSON Schema (http://json-schema.org/) and turns it into a Type::Tiny type.
my $schema = JSON::Schema::AsType->new( schema => $my_schema );
The class constructor. Accepts the following arguments.
The JSON schema to compile, as a hashref.
If not given, will be retrieved from uri.
uri
An error will be thrown is neither schema nor uri is given.
schema
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.
%JSON::Schema::AsType::EXTERNAL_SCHEMAS
The version of the JSON-Schema specification to use. Defaults to 'draft4' (and doesn't accept anything else at the moment).
Returns the compiled Type::Tiny type.
Returns true if $struct is valid as per the schema.
true
$struct
Returns a short explanation if $struct didn't validate, nothing otherwise.
Returns a log explanation if $struct didn't validate, nothing otherwise.
Like validate, but validates the schema itself against its specification.
validate
print $schema->validate_schema; # equivalent to print $schema->specification_schema->validate($schema);
Like validate_explain, but validates the schema itself against its specification.
validate_explain
Returns the JSON schema, as a hashref.
Returns the JSON::Schema::AsType object for the parent schema, or undef is the current schema is the top-level one.
undef
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.
$url
Returns the uri associated with the schema, if any.
Returns the JSON Schema specification used by the object.
Returns the JSON::Schema::AsType object representing the schema of the current object's specification.
Returns the top-level schema including this schema.
Returns true if this schema is a top-level schema.
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.
$ref
Yanick Champoux <yanick@babyl.dyndns.org>
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.
To install JSON::Schema::AsType, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSON::Schema::AsType
CPAN shell
perl -MCPAN -e shell install JSON::Schema::AsType
For more information on module installation, please visit the detailed CPAN module installation guide.