|
NAMEJSON::Validator::Joi - Joi validation sugar for JSON::ValidatorSYNOPSISuse 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 FUNCTIONSjoi$joi = joi(%attrs); Same as: JSON::Validator::Joi->new(%attrs); DESCRIPTIONJSON::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.ATTRIBUTESenummy $joi = $joi->enum(["foo", "bar"]); my $array_ref = $joi->enum; Defines a list of enum values for "integer", "number" and "string". formatmy $joi = $joi->format("email"); my $str = $joi->format; Used to set the format of the "string". See also "iso_date", "email" and "uri". maxmy $joi = $joi->max(10); my $int = $joi->max;
minmy $joi = $joi->min(10); my $int = $joi->min;
multiple_ofmy $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. regexmy $joi = $joi->regex("^\w+$"); my $str = $joi->regex; Defines a pattern that "string" will be validated against. typemy $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. validatormy $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. METHODSTO_JSONAlias for "compile".alphanummy $joi = $joi->alphanum; Sets "regex" to "^\w*$". arraymy $joi = $joi->array; Sets "type" to "array". booleanmy $joi = $joi->boolean; Sets "type" to "boolean". compilemy $hash_ref = $joi->compile; Will convert this object into a JSON-Schema data structure that "schema" in JSON::Validator understands. date_timemy $joi = $joi->date_time; Sets "format" to date-time. my $joi = $joi->email; Sets "format" to email. extendmy $new_joi = $joi->extend($other_joi_object); Will extend $joi with the definitions in $other_joi_object and return a new object. iso_dateAlias for "date_time".integermy $joi = $joi->integer; Sets "type" to "integer". itemsmy $joi = $joi->items($joi); my $joi = $joi->items([$joi, ...]); Defines a list of items for the "array" type. lengthmy $joi = $joi->length(10); Sets both "min" and "max" to the number provided. lowercasemy $joi = $joi->lowercase; Will set "regex" to only match lower case strings. negativemy $joi = $joi->negative; Sets "max" to 0. numbermy $joi = $joi->number; Sets "type" to "number". objectmy $joi = $joi->object; Sets "type" to "object". patternAlias for "regex".positivemy $joi = $joi->positive; Sets "min" to 0. propsmy $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. requiredmy $joi = $joi->required; Marks the current property as required. strictmy $joi = $joi->strict; Sets "array" and "object" to not allow any more items/keys than what is defined. stringmy $joi = $joi->string; Sets "type" to "string". tokenmy $joi = $joi->token; Sets "regex" to "^[a-zA-Z0-9_]+$". validatemy @errors = $joi->validate($data); Used to validate $data using "validate" in JSON::Validator. Returns a list of JSON::Validator::Error objects on invalid input. uniquemy $joi = $joi->unique; Used to force the "array" to only contain unique items. uppercasemy $joi = $joi->uppercase; Will set "regex" to only match upper case strings. urimy $joi = $joi->uri; Sets "format" to uri. SEE ALSOJSON::Validator<https://github.com/hapijs/joi>.
Visit the GSP FreeBSD Man Page Interface. |