NAME

Swagger2::SchemaValidator - Validate JSON schemas

DESCRIPTION

Swagger2::SchemaValidator is a class for validating JSON schemas.

The validation process is supposed to be compatible with draft 4 of the JSON schema specification. Please submit a bug report if it is not.

SYNOPSIS

use Swagger2::SchemaValidator;
my $validator = Swagger2::SchemaValidator->new;

@errors = $validator->validate($data, $schema);

Example:

warn $validator->validate(
  {
    nick => "batman",
  },
  {
    type => "object",
    properties => {
      nick => {type => "string", minLength => 3, maxLength => 10, pattern => qr{^\w+$} }
    },
  },
);

SEE ALSO

ATTRIBUTES

coerce

$bool = $self->coerce;

Set this to true if you want to coerce numbers into string and the other way around.

This is EXPERIMENTAL and could be removed without notice!

formats

$hash_ref = $self->formats;
$self = $self->formats(\%hash);

Holds a hash-ref, where the keys are supported JSON type "formats", and the values holds a code block which can validate a given format.

Note! The modules mentioned below are optional.

  • byte

    A padded, base64-encoded string of bytes, encoded with a URL and filename safe alphabet. Defined by RFC4648.

  • date

    An RFC3339 date in the format YYYY-MM-DD

  • date-time

    An RFC3339 timestamp in UTC time. This is formatted as "YYYY-MM-DDThh:mm:ss.fffZ". The milliseconds portion (".fff") is optional

  • double

    Cannot test double values with higher precision then what the "number" type already provides.

  • email

    Validated against the RFC5322 spec.

  • float

    Will always be true if the input is a number, meaning there is no difference between "float" and "double". Patches are welcome.

  • hostname

    Will be validated using Data::Validate::Domain if installed.

  • int32

    A signed 32 bit integer.

  • int64

    A signed 64 bit integer. Note: This check is only available if Perl is compiled to use 64 bit integers.

  • ipv4

    Will be validated using Data::Validate::IP if installed or fall back to a plain IPv4 IP regex.

  • ipv6

    Will be validated using Data::Validate::IP if installed.

  • uri

    Validated against the RFC3986 spec.

METHODS

validate

@errors = $self->validate($data, $schema);

Validates $data against a given JSON $schema. @errors will contain objects with containing the validation errors. It will be empty on success.

Example error element:

bless {
  message => "Some description",
  path => "/json/path/to/node",
}, "Swagger2::SchemaValidator::Error"

The error objects are always true in boolean context and will stringify. The stringification format is subject to change.

COPYRIGHT AND LICENSE

Copyright (C) 2014-2015, Jan Henning Thorsen

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

AUTHOR

Jan Henning Thorsen - jhthorsen@cpan.org