JSON::Validator::Joi(3pm) | User Contributed Perl Documentation | JSON::Validator::Joi(3pm) |
JSON::Validator::Joi - Joi validation sugar for JSON::Validator
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 => "jhthorsen@cpan.org", }); die "@errors" if @errors;
$joi = joi(%attrs);
Same as:
JSON::Validator::Joi->new(%attrs);
JSON::Validator::Joi is an elegant DSL schema-builder. The main purpose is to build a JSON Schema <https://json-schema.org/> for JSON::Validator, but it can also validate data directly with sane defaults.
my $joi = $joi->enum(["foo", "bar"]); my $array_ref = $joi->enum;
Defines a list of enum values for "integer", "number" and "string".
my $joi = $joi->format("email"); my $str = $joi->format;
Used to set the format of the "string". See also "iso_date", "email" and "uri".
my $joi = $joi->max(10); my $int = $joi->max;
Defines the max number of items in the array.
Defined the max value.
Defines the max number of items in the object.
Defines how long the string can be.
my $joi = $joi->min(10); my $int = $joi->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.
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.
my $joi = $joi->regex("^\w+$"); my $str = $joi->regex;
Defines a pattern that "string" will be validated against.
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.
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.
Alias for "compile".
my $joi = $joi->alphanum;
Sets "regex" to "^\w*$".
my $joi = $joi->array;
Sets "type" to "array".
my $joi = $joi->boolean;
Sets "type" to "boolean".
my $hash_ref = $joi->compile;
Will convert this object into a JSON-Schema data structure that "schema" in JSON::Validator understands.
my $joi = $joi->date_time;
Sets "format" to date-time.
my $joi = $joi->email;
Sets "format" to email.
my $new_joi = $joi->extend($other_joi_object);
Will extend $joi with the definitions in $other_joi_object and return a new object.
Alias for "date_time".
my $joi = $joi->integer;
Sets "type" to "integer".
my $joi = $joi->items($joi); my $joi = $joi->items([$joi, ...]);
Defines a list of items for the "array" type.
my $joi = $joi->length(10);
Sets both "min" and "max" to the number provided.
my $joi = $joi->lowercase;
Will set "regex" to only match lower case strings.
my $joi = $joi->negative;
Sets "max" to 0.
my $joi = $joi->number;
Sets "type" to "number".
my $joi = $joi->object;
Sets "type" to "object".
Alias for "regex".
my $joi = $joi->positive;
Sets "min" to 0.
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.
my $joi = $joi->required;
Marks the current property as required.
my $joi = $joi->strict;
Sets "array" and "object" to not allow any more items/keys than what is defined.
my $joi = $joi->string;
Sets "type" to "string".
my $joi = $joi->token;
Sets "regex" to "^[a-zA-Z0-9_]+$".
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.
my $joi = $joi->unique;
Used to force the "array" to only contain unique items.
my $joi = $joi->uppercase;
Will set "regex" to only match upper case strings.
my $joi = $joi->uri;
Sets "format" to uri.
JSON::Validator
<https://github.com/hapijs/joi>.
2023-03-06 | perl v5.36.0 |