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 🌈


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