NAME
JSON::Schema::AsType - generates Type::Tiny types out of JSON schemas
VERSION
version 0.4.4
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.
Strings and Numbers
By default, JSON::Schema::AsType
follows the JSON schema specs and distinguish between strings and numbers.
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:
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.
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. - draft_version => $version
-
The version of the JSON-Schema specification to use. Accepts
3
or4
, defaults to '4'.
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.
draft_version
Returns the draft version used by the object.
spec
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.
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) 2024 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.