The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

JSON::Validator::Joi - Joi adapter for JSON::Validator

SYNOPSIS

  use JSON::Validator "joi";

  my @errors = joi(
    {
      name  => "Jan Henning",
      age   => 34,
      email => "jhthorsen@cpan.org",
    },
    joi->object->props(
      age   => joi->integer->min(0)->max(200),
      email => joi->regex(".@.")->required,
      name  => joi->string->min(1),
    )
  );

  die "@errors" if @errors;

DESCRIPTION

JSON::Validator::Joi tries to mimic the JavaScript library https://github.com/hapijs/joi.

This module is EXPERIMENTAL and can change without warning. Let me know if you find it useful.

ATTRIBUTES

format

  $self = $self->format("email");
  $str = $self->format;

Used to set the format of the "string". See also "iso_date", "email" and "uri".

max

  $self = $self->max(10);
  $int = $self->max;
  • array

    Defines the max number of items in the array.

  • integer, number

    Defined the max value.

  • object

    Defines the max number of items in the object.

  • string

    Defines how long the string can be.

min

  $self = $self->min(10);
  $int = $self->min;
  • array

    Defines the minimum number of items in the array.

  • integer, number

    Defined the minimum value.

  • object

    Defines the minimum number of items in the object.

  • string

    Defines how short the string can be.

multiple_of

  $self = $self->multiple_of(3);
  $int = $self->multiple_of;

Used by "integer" and "number" to define what the number must be a multiple of.

regex

  $self = $self->regex("^\w+$");
  $str = $self->regex;

Defines a pattern that "string" will be validated against.

type

  $str = $self->type;

Set by "array", "integer", "object" or "string".

METHODS

TO_JSON

Alias for "compile".

alphanum

  $self = $self->alphanum;

Sets "regex" to "^\w*$".

array

  $self = $self->array;

Sets "type" to "array".

boolean

  $self = $self->boolean;

Sets "type" to "boolean".

compile

  $hash_ref = $self->compile;

Will convert this object into a JSON-Schema data structure that "schema" in JSON::Validator understands.

date_time

  $self = $self->date_time;

Sets "format" to date-time.

email

  $self = $self->email;

Sets "format" to email.

extend

  $new_self = $self->extend($joi);

Will extend $self with the definitions in $joi and return a new object.

iso_date

Alias for "date_time".

integer

  $self = $self->integer;

Sets "type" to "integer".

items

  $self = $self->items($joi);
  $self = $self->items([$joi, ...]);

Defines a list of items for the "array" type.

length

  $self = $self->length(10);

Sets both "min" and "max" to the number provided.

lowercase

  $self = $self->lowercase;

Will set "regex" to only match lower case strings.

negative

  $self = $self->negative;

Sets "max" to 0.

number

  $self = $self->number;

Sets "type" to "number".

object

  $self = $self->object;

Sets "type" to "object".

pattern

Alias for "regex".

positive

  $self = $self->positive;

Sets "min" to 0.

props

  $self = $self->props(name => JSON::Validator::Joi->new->string, ...);

Used to define properties for an "object" type. Each key is the name of the parameter and the values must be a JSON::Validator::Joi object.

required

  $self = $self->required;

Marks the current property as required.

strict

  $self = $self->strict;

Sets "array" and "object" to not allow any more items/keys than what is defined.

string

  $self = $self->string;

Sets "type" to "string".

token

  $self = $self->token;

Sets "regex" to ^[a-zA-Z0-9_]+$.

validate

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

Used to validate $data using "validate" in JSON::Validator. Returns a list of JSON::Validator::Error objects on invalid input.

unique

  $self = $self->unique;

Used to force the "array" to only contain unique items.

uppercase

  $self = $self->uppercase;

Will set "regex" to only match upper case strings.

uri

  $self = $self->uri;

Sets "format" to uri.

SEE ALSO

JSON::Validator