JSON::Validator::Joi - Joi adapter for JSON::Validator
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;
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.
$self = $self->format("email"); $str = $self->format;
Used to set the format of the "string". See also "iso_date", "email" and "uri".
$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.
$self = $self->min(10); $int = $self->min;
Defines the minimum number of items in the array.
Defined the minimum value.
Defines the minimum number of items in the object.
Defines how short the string can be.
$self = $self->multiple_of(3); $int = $self->multiple_of;
Used by "integer" and "number" to define what the number must be a multiple of.
$self = $self->regex("^\w+$"); $str = $self->regex;
Defines a pattern that "string" will be validated against.
$str = $self->type;
Set by "array", "integer", "object" or "string".
Alias for "compile".
$self = $self->alphanum;
Sets "regex" to "^\w*$".
$self = $self->array;
Sets "type" to "array".
$self = $self->boolean;
Sets "type" to "boolean".
$hash_ref = $self->compile;
Will convert this object into a JSON-Schema data structure that "schema" in JSON::Validator understands.
$self = $self->date_time;
Sets "format" to date-time.
$self = $self->email;
Sets "format" to email.
$new_self = $self->extend($joi);
Will extend $self with the definitions in $joi and return a new object.
$self
$joi
Alias for "date_time".
$self = $self->integer;
Sets "type" to "integer".
$self = $self->items($joi); $self = $self->items([$joi, ...]);
Defines a list of items for the "array" type.
$self = $self->length(10);
Sets both "min" and "max" to the number provided.
$self = $self->lowercase;
Will set "regex" to only match lower case strings.
$self = $self->negative;
Sets "max" to 0.
0
$self = $self->number;
Sets "type" to "number".
$self = $self->object;
Sets "type" to "object".
Alias for "regex".
$self = $self->positive;
Sets "min" to 0.
$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.
$self = $self->required;
Marks the current property as required.
$self = $self->strict;
Sets "array" and "object" to not allow any more items/keys than what is defined.
$self = $self->string;
Sets "type" to "string".
$self = $self->token;
Sets "regex" to ^[a-zA-Z0-9_]+$.
^[a-zA-Z0-9_]+$
@errors = $self->validate($data);
Used to validate $data using "validate" in JSON::Validator. Returns a list of JSON::Validator::Error objects on invalid input.
$data
$self = $self->unique;
Used to force the "array" to only contain unique items.
$self = $self->uppercase;
Will set "regex" to only match upper case strings.
$self = $self->uri;
Sets "format" to uri.
JSON::Validator
To install JSON::Validator, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSON::Validator
CPAN shell
perl -MCPAN -e shell install JSON::Validator
For more information on module installation, please visit the detailed CPAN module installation guide.