JSON::Validator::Joi - Joi validation sugar for JSON::Validator

  1. SYNOPSIS
    1. EXPORTED FUNCTIONS
    2. joi
  2. DESCRIPTION
  3. ATTRIBUTES
    1. enum
    2. format
    3. max
    4. min
    5. multiple_of
    6. regex
    7. type
    8. validator
  4. METHODS
    1. TO_JSON
    2. alphanum
    3. array
    4. boolean
    5. compile
    6. date_time
    7. email
    8. extend
    9. iso_date
    10. integer
    11. items
    12. length
    13. lowercase
    14. negative
    15. number
    16. object
    17. pattern
    18. positive
    19. props
    20. required
    21. strict
    22. string
    23. token
    24. validate
    25. unique
    26. uppercase
    27. uri
  5. SEE ALSO

SYNOPSIS

use JSON::Validator::Joi "joi";

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

die "@errors" if @errors;

EXPORTED FUNCTIONS

joi

$joi = joi(%attrs);

Same as:

JSON::Validator::Joi->new(%attrs);

DESCRIPTION

JSON::Validator::Joi is an elegant DSL schema-builder. The main purpose is to build a JSON Schema for JSON::Validator, but it can also validate data directly with sane defaults.

ATTRIBUTES

enum

my $joi       = $joi->enum(["foo", "bar"]);
my $array_ref = $joi->enum;

Defines a list of enum values for "integer", "number" and "string".

format

my $joi = $joi->format("email");
my $str = $joi->format;

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

max

my $joi = $joi->max(10);
my $int = $joi->max;

min

my $joi = $joi->min(10);
my $int = $joi->min;

multiple_of

my $joi = $joi->multiple_of(3);
my $int = $joi->multiple_of;

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

regex

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

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

type

my $joi = $joi->type("string");
my $joi = $joi->type([qw(null integer)]);
my $any = $joi->type;

Sets the required type. This attribute is set by the convenience methods "array", "integer", "object" and "string", but can be set manually if you need to check against a list of type.

validator

my $joi = $joi->validator(JSON::Validator::Schema::Draft7->new);
my $jv  = $joi->validator;

Defaults to a JSON::Validator object. This object is used by "validate".

Note: This might change to JSON::Validator::Schema::Draft7 or a later schema in the future.

METHODS

TO_JSON

Alias for "compile".

alphanum

my $joi = $joi->alphanum;

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

array

my $joi = $joi->array;

Sets "type" to "array".

boolean

my $joi = $joi->boolean;

Sets "type" to "boolean".

compile

my $hash_ref = $joi->compile;

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

date_time

my $joi = $joi->date_time;

Sets "format" to date-time.

email

my $joi = $joi->email;

Sets "format" to email.

extend

my $new_joi = $joi->extend($other_joi_object);

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

iso_date

Alias for "date_time".

integer

my $joi = $joi->integer;

Sets "type" to "integer".

items

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

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

length

my $joi = $joi->length(10);

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

lowercase

my $joi = $joi->lowercase;

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

negative

my $joi = $joi->negative;

Sets "max" to 0.

number

my $joi = $joi->number;

Sets "type" to "number".

object

my $joi = $joi->object;

Sets "type" to "object".

pattern

Alias for "regex".

positive

my $joi = $joi->positive;

Sets "min" to 0.

props

my $joi = $joi->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

my $joi = $joi->required;

Marks the current property as required.

strict

my $joi = $joi->strict;

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

string

my $joi = $joi->string;

Sets "type" to "string".

token

my $joi = $joi->token;

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

validate

my @errors = $joi->validate($data);

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

unique

my $joi = $joi->unique;

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

uppercase

my $joi = $joi->uppercase;

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

uri

my $joi = $joi->uri;

Sets "format" to uri.

SEE ALSO

JSON::Validator

https://github.com/hapijs/joi.