table of contents
JSON::Validator::Joi(3pm) | User Contributed Perl Documentation | JSON::Validator::Joi(3pm) |
NAME¶
JSON::Validator::Joi - Joi validation sugar for JSON::Validator
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 => "jhthorsen@cpan.org", }); 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 <https://json-schema.org/> 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;
- 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¶
my $joi = $joi->min(10); my $int = $joi->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¶
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
2023-03-06 | perl v5.36.0 |