JSON::Schema::AsType - generates Type::Tiny types out of JSON schemas
version 0.4.3
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.
By default, JSON::Schema::AsType follows the JSON schema specs and distinguish between strings and numbers.
JSON::Schema::AsType
value String? Number? "a" yes no 1 no yes "1" yes no
If you want the usual Perl behavior and considers the JSON schema type String to be a superset of Number. That is:
String
Number
value String? Number? "a" yes no 1 yes yes "1" yes yes
Then you can set the object's attribute strict_string to 0. Setting the global variable $JSON::Schema::AsType::strict_string to 0 will work too, but that's deprecated and will eventually go away.
strict_string
0
$JSON::Schema::AsType::strict_string
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. Accepts 3 or 4, defaults to '4'.
3
4
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 draft version used by the object.
Returns the JSON::Schema::AsType object associated with the specs of this object's schema.
I.e., if the current object is a draft4 schema, spec will return the schema definining draft4.
spec
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) 2017, 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.