JSON::V...or::Joi(3)
πΈ NAME πΈ
okay bestie, welcome to the User Contributed Perl Documentation page for JSON::Validator::Joi(3) β the header shortens it to JSON::V...or::Joi(3), which is just the same icon. The module is JSON::Validator::Joi, and it's literally Joi validation sugar for JSON::Validator. That's the whole NAME vibe, no cap. π
π§ SYNOPSIS π§
Here's the demo, exactly as it slays:
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
The exported helper is joi, and its signature is:
$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 β the spec lives at https://json-schema.org/ β but it can also validate data directly with sane defaults. It's giving schema-builder main character energy, fr fr. π
π ATTRIBUTES π
enum
my $joi = $joi->enum(["foo", "bar"]);
my $array_ref = $joi->enum;
Defines a list of enum values for integer, number and string. It's the "pick one of these, bestie" list. πΈ
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β defines 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β defines 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. Regex girlies rise. π§
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 you can set it manually if you need to check against a list of types. π
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. Keep that in mind, bestie. π«Ά
π METHODS π
TO_JSON
Alias for compile. That's it. Periodt. β¨
alphanum
my $joi = $joi->alphanum;
Sets regex to "^\w*$". Note the star, not the plus. π
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. It's giving compiler era. π₯
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. Glow-up via merge. π
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. Two birds, one stone. πͺ
lowercase
my $joi = $joi->lowercase;
Will set regex to only match lower case strings. lowercase supremacy. π
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. This is the object-building era. ποΈ
required
my $joi = $joi->required;
Marks the current property as required. No skipping, bestie. π
strict
my $joi = $joi->strict;
Sets array and object to not allow any more items/keys than what is defined. Strict is strict. π¨
string
my $joi = $joi->string;
Sets type to string. π§΅
token
my $joi = $joi->token;
Sets regex to "^[a-zA-Z0-9_]+$". Token vibes only. π
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. Errors are giving receipts. π
unique
my $joi = $joi->unique;
Used to force the array to only contain unique items. No duplicates allowed. π
uppercase
my $joi = $joi->uppercase;
Will set regex to only match upper case strings. UPPERCASE SUPREMACY. π
uri
my $joi = $joi->uri;
Sets format to uri. π
π SEE ALSO π
JSON::Validatorhttps://github.com/hapijs/joiβ the Joi inspo, iykyk.
mwah π That's the whole page, bestie. It's from perl v5.42.0, dated 2025-03-17, and the footer name is JSON::V...or::Joi(3). Byeee π«Άβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-JSON-Validator 5.150.0-bp160.1.1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms