| 1 | package JSON::Validator::Joi; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use List::Util qw(uniq); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Mojo::JSON qw(false true); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Mojo::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our @EXPORT_OK = qw(joi); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Avoid "Subroutine redefined" warnings | ||||||||||||||||||||||||||||
| 14 | 1 | require JSON::Validator::Schema::Draft7; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | has enum => sub { +[] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 17 | 1 | has type => 'object'; | |||||||||||||||||||||||||||
| 18 | 9 | ▶ | has validator => sub { JSON::Validator::Schema::Draft7->new->coerce('booleans,numbers,strings') }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | has [qw(format max min multiple_of regex)]; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | for my $attr (qw(required strict unique)) { | |||||||||||||||||||||||||||
| 23 | 16 | ▶ | Mojo::Util::monkey_patch(__PACKAGE__, $attr => sub { $_[0]->{$attr} = $_[1] // 1; $_[0]; }); | ||||||||||||||||||||||||||
|
Subroutine
strict: called CC=2 SCAR=7.1
Condition50%1/2
$_[1] // 1
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | sub alphanum { shift->_type('string')->regex('^\w*$') } | ||||||||||||||||||||||||||
|
Subroutine
alphanum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | sub boolean { shift->type('boolean') } | ||||||||||||||||||||||||||
|
Subroutine
boolean: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 54 | ▶ | sub compile { | ||||||||||||||||||||||||||
|
Subroutine
compile: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 54 | my $self = shift; | |||||||||||||||||||||||||||
| 31 | 54 | my $merged = {}; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 54 | ▶ | for (ref $self->type eq 'ARRAY' ? @{$self->type} : $self->type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 55 | my $method = "_compile_$_"; | |||||||||||||||||||||||||||
| 35 | 55 | my $compiled = $self->$method; | |||||||||||||||||||||||||||
| 36 | 55 | @$merged{keys %$compiled} = values %$compiled; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 54 | return $merged; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | sub date_time { shift->_type('string')->format('date-time') } | ||||||||||||||||||||||||||
|
Subroutine
date_time: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | sub email { shift->_type('string')->format('email') } | ||||||||||||||||||||||||||
|
Subroutine
email: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 6 | ▶ | sub extend { | ||||||||||||||||||||||||||
|
Subroutine
extend: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 6 | my ($self, $by) = @_; | |||||||||||||||||||||||||||
| 47 | 6 | ▶ | die "Cannot extend joi '@{[$self->type]}' by '@{[$by->type]}'" unless $self->type eq $by->type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 5 | my $clone = shift->new(dclone($self)); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 5 | for my $key (keys %$by) { | |||||||||||||||||||||||||||
| 52 | 11 | my $ref = ref $by->{$key}; | |||||||||||||||||||||||||||
| 53 | 11 | ▶ | $clone->{$key} = $by->{$key} unless $ref eq 'ARRAY' or $ref eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ref eq "ARRAY" or $ref eq "HASH"
MC/DC100%2/2
$ref eq "ARRAY" or $ref eq "HASH"
$ref eq "ARRAY"
$ref eq "HASH"
Truth table100%3/3
A or B
A= $ref eq "ARRAY"B= $ref eq "HASH"
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 5 | ▶ | if ($self->type eq 'array') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 57 | 2 | ▶ | $clone->{items} = dclone($by->{items}) if $by->{items}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | elsif ($self->type eq 'object') { | ||||||||||||||||||||||||||||
| 60 | 1 | $clone->{required_props} = [uniq @{$clone->{required_props}}, @{$by->{required_props}}] | |||||||||||||||||||||||||||
| 61 | 2 | ▶ | if defined $by->{required_props}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | $clone->{properties}{$_} = dclone($by->{properties}{$_}) for keys %{$by->{properties} || {}}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$by->{'properties'} || {}
MC/DC0%0/1
$by->{'properties'} || {}
$by->{'properties'}
Truth table50%1/2
A
A= $by->{'properties'}
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 5 | return $clone; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 7 | ▶ | sub array { shift->type('array') } | ||||||||||||||||||||||||||
|
Subroutine
array: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 16 | ▶ | sub integer { shift->type('integer') } | ||||||||||||||||||||||||||
|
Subroutine
integer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | sub iso_date { shift->date_time } | ||||||||||||||||||||||||||
|
Subroutine
iso_date: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 5 | ▶ | sub items { $_[0]->{items} = $_[1]; $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
items: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 62 | ▶ | sub joi { __PACKAGE__->new(@_) } | ||||||||||||||||||||||||||
|
Subroutine
joi: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 1 | ▶ | sub length { shift->min($_[0])->max($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
length: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 1 | ▶ | sub lowercase { shift->_type('string')->regex('^\p{Lowercase}*$') } | ||||||||||||||||||||||||||
|
Subroutine
lowercase: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | sub negative { shift->_type('number')->max(0) } | ||||||||||||||||||||||||||
|
Subroutine
negative: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 4 | ▶ | sub number { shift->type('number') } | ||||||||||||||||||||||||||
|
Subroutine
number: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 11 | ▶ | sub object { shift->type('object') } | ||||||||||||||||||||||||||
|
Subroutine
object: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | sub pattern { shift->regex(@_) } | ||||||||||||||||||||||||||
|
Subroutine
pattern: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | sub positive { shift->number->min(0) } | ||||||||||||||||||||||||||
|
Subroutine
positive: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 13 | ▶ | sub props { | ||||||||||||||||||||||||||
|
Subroutine
props: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 13 | my $self = shift->type('object'); | |||||||||||||||||||||||||||
| 83 | 13 | ▶ | my %properties = ref $_[0] ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 13 | ▶ | while (my ($name, $property) = each %properties) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 33 | ▶ | push @{$self->{required_props}}, $name if $property->{required}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 33 | $self->{properties}{$name} = $property->compile; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 13 | return $self; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 12 | ▶ | sub string { shift->type('string') } | ||||||||||||||||||||||||||
|
Subroutine
string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | sub token { shift->_type('string')->regex('^[a-zA-Z0-9_]+$') } | ||||||||||||||||||||||||||
|
Subroutine
token: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | sub uppercase { shift->_type('string')->regex('^\p{Uppercase}*$') } | ||||||||||||||||||||||||||
|
Subroutine
uppercase: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | sub uri { shift->_type('string')->format('uri') } | ||||||||||||||||||||||||||
|
Subroutine
uri: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 7 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 7 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 100 | 7 | return $self->validator->data($self->compile)->validate($data); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 5 | ▶ | sub _compile_array { | ||||||||||||||||||||||||||
|
Subroutine
_compile_array: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||
| 104 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 105 | 5 | my $json = {type => $self->type}; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 5 | ▶ | $json->{additionalItems} = false if $self->{strict}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 5 | ▶ | $json->{maxItems} = $self->{max} if defined $self->{max}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 5 | ▶ | $json->{minItems} = $self->{min} if defined $self->{min}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 5 | ▶ | $json->{uniqueItems} = true if $self->{unique}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 5 | ▶ | if ($self->{items}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 4 | $json->{items} = $self->{items}; | |||||||||||||||||||||||||||
| 114 | 4 | ▶ | for my $item (ref $json->{items} eq 'ARRAY' ? @{$json->{items}} : $json->{items}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 4 | ▶ | $item = $item->compile if blessed $item and $item->can('compile'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($item) and $item->can("compile")
MC/DC50%1/2
builtin::blessed($item) and $item->can("compile")
builtin::blessed($item)
$item->can("compile")
Truth table66%2/3
A and B
A= builtin::blessed($item)B= $item->can("compile")
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 5 | return $json; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | ▶ | sub _compile_boolean { +{type => 'boolean'} } | ||||||||||||||||||||||||||
|
Subroutine
_compile_boolean: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 13 | ▶ | sub _compile_integer { shift->_compile_number } | ||||||||||||||||||||||||||
|
Subroutine
_compile_integer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | ▶ | sub _compile_null { {type => shift->type} } | ||||||||||||||||||||||||||
|
Subroutine
_compile_null: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 16 | ▶ | sub _compile_number { | ||||||||||||||||||||||||||
|
Subroutine
_compile_number: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 129 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 130 | 16 | my $json = {type => $self->type}; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 16 | ▶ | $json->{enum} = $self->{enum} if defined $self->{enum} and @{$self->{enum}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"enum"} and @{$self->{"enum"};}
MC/DC50%1/2
defined $self->{"enum"} and @{$self->{"enum"};}
defined $self->{"enum"}
@{$self->{"enum"};}
Truth table66%2/3
A and B
A= defined $self->{"enum"}B= @{$self->{"enum"};}
| |||||||||||||||||||||||||||||
| 133 | 16 | ▶ | $json->{maximum} = $self->{max} if defined $self->{max}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 16 | ▶ | $json->{minimum} = $self->{min} if defined $self->{min}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 16 | ▶ | $json->{multipleOf} = $self->{multiple_of} if defined $self->{multiple_of}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 16 | return $json; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 13 | ▶ | sub _compile_object { | ||||||||||||||||||||||||||
|
Subroutine
_compile_object: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||
| 141 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 142 | 13 | my $json = {type => $self->type}; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 13 | ▶ | $json->{additionalProperties} = false if $self->{strict}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 13 | ▶ | $json->{maxProperties} = $self->{max} if defined $self->{max}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 13 | ▶ | $json->{minProperties} = $self->{min} if defined $self->{min}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 13 | ▶ | $json->{patternProperties} = $self->{regex} if $self->{regex}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 13 | ▶ | $json->{properties} = $self->{properties} if ref $self->{properties} eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 13 | ▶ | $json->{required} = $self->{required_props} if defined $self->{required_props}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 13 | return $json; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 19 | ▶ | sub _compile_string { | ||||||||||||||||||||||||||
|
Subroutine
_compile_string: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 155 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 156 | 19 | my $json = {type => $self->type}; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 19 | ▶ | $json->{enum} = $self->{enum} if defined $self->{enum} and @{$self->{enum}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"enum"} and @{$self->{"enum"};}
MC/DC50%1/2
defined $self->{"enum"} and @{$self->{"enum"};}
defined $self->{"enum"}
@{$self->{"enum"};}
Truth table66%2/3
A and B
A= defined $self->{"enum"}B= @{$self->{"enum"};}
| |||||||||||||||||||||||||||||
| 159 | 19 | ▶ | $json->{format} = $self->{format} if defined $self->{format}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 19 | ▶ | $json->{maxLength} = $self->{max} if defined $self->{max}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 19 | ▶ | $json->{minLength} = $self->{min} if defined $self->{min}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 19 | ▶ | $json->{pattern} = $self->{regex} if defined $self->{regex}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 19 | return $json; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 10 | ▶ | sub _type { | ||||||||||||||||||||||||||
|
Subroutine
_type: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 168 | 10 | ▶ | $_[0]->{type} = $_[1] unless $_[0]->{type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 10 | return $_[0]; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 10 | ▶ | sub TO_JSON { shift->compile } | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 1 | 1; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =encoding utf8 | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 NAME | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | JSON::Validator::Joi - Joi validation sugar for JSON::Validator | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | use JSON::Validator::Joi "joi"; | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | my @errors = joi->object->props( | ||||||||||||||||||||||||||||
| 187 | age => joi->integer->min(0)->max(200), | ||||||||||||||||||||||||||||
| 188 | email => joi->regex(".@.")->required, | ||||||||||||||||||||||||||||
| 189 | name => joi->string->min(1), | ||||||||||||||||||||||||||||
| 190 | )->validate({ | ||||||||||||||||||||||||||||
| 191 | name => "Jan Henning", | ||||||||||||||||||||||||||||
| 192 | age => 34, | ||||||||||||||||||||||||||||
| 193 | email => "jhthorsen@cpan.org", | ||||||||||||||||||||||||||||
| 194 | }); | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | die "@errors" if @errors; | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head2 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head2 joi | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | $joi = joi(%attrs); | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Same as: | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | JSON::Validator::Joi->new(%attrs); | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | L<JSON::Validator::Joi> is an elegant DSL schema-builder. The main purpose is | ||||||||||||||||||||||||||||
| 211 | to build a L<JSON Schema|https://json-schema.org/> for L<JSON::Validator>, but | ||||||||||||||||||||||||||||
| 212 | it can also validate data directly with sane defaults. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head2 enum | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | my $joi = $joi->enum(["foo", "bar"]); | ||||||||||||||||||||||||||||
| 219 | my $array_ref = $joi->enum; | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | Defines a list of enum values for L</integer>, L</number> and L</string>. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head2 format | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | my $joi = $joi->format("email"); | ||||||||||||||||||||||||||||
| 226 | my $str = $joi->format; | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Used to set the format of the L</string>. | ||||||||||||||||||||||||||||
| 229 | See also L</iso_date>, L</email> and L</uri>. | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head2 max | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | my $joi = $joi->max(10); | ||||||||||||||||||||||||||||
| 234 | my $int = $joi->max; | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =over 2 | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =item * array | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | Defines the max number of items in the array. | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =item * integer, number | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | Defined the max value. | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =item * object | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | Defines the max number of items in the object. | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =item * string | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | Defines how long the string can be. | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =back | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | =head2 min | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | my $joi = $joi->min(10); | ||||||||||||||||||||||||||||
| 259 | my $int = $joi->min; | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =over 2 | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =item * array | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | Defines the minimum number of items in the array. | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =item * integer, number | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | Defined the minimum value. | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =item * object | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | Defines the minimum number of items in the object. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =item * string | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | Defines how short the string can be. | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =back | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =head2 multiple_of | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | my $joi = $joi->multiple_of(3); | ||||||||||||||||||||||||||||
| 284 | my $int = $joi->multiple_of; | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | Used by L</integer> and L</number> to define what the number must be a multiple | ||||||||||||||||||||||||||||
| 287 | of. | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | =head2 regex | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | my $joi = $joi->regex("^\w+$"); | ||||||||||||||||||||||||||||
| 292 | my $str = $joi->regex; | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | Defines a pattern that L</string> will be validated against. | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =head2 type | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | my $joi = $joi->type("string"); | ||||||||||||||||||||||||||||
| 299 | my $joi = $joi->type([qw(null integer)]); | ||||||||||||||||||||||||||||
| 300 | my $any = $joi->type; | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | Sets the required type. This attribute is set by the convenience methods | ||||||||||||||||||||||||||||
| 303 | L</array>, L</integer>, L</object> and L</string>, but can be set manually if | ||||||||||||||||||||||||||||
| 304 | you need to check against a list of type. | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =head2 validator | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | my $joi = $joi->validator(JSON::Validator::Schema::Draft7->new); | ||||||||||||||||||||||||||||
| 309 | my $jv = $joi->validator; | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | Defaults to a L<JSON::Validator> object. This object is used by L</validate>. | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | Note: This might change to L<JSON::Validator::Schema::Draft7> or a later | ||||||||||||||||||||||||||||
| 314 | schema in the future. | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =head1 METHODS | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =head2 TO_JSON | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | Alias for L</compile>. | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | =head2 alphanum | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | my $joi = $joi->alphanum; | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | Sets L</regex> to "^\w*$". | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =head2 array | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | my $joi = $joi->array; | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | Sets L</type> to "array". | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =head2 boolean | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | my $joi = $joi->boolean; | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | Sets L</type> to "boolean". | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =head2 compile | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | my $hash_ref = $joi->compile; | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | Will convert this object into a JSON-Schema data structure that | ||||||||||||||||||||||||||||
| 345 | L<JSON::Validator/schema> understands. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =head2 date_time | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | my $joi = $joi->date_time; | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | Sets L</format> to L<date-time|JSON::Validator/date-time>. | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | =head2 email | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | my $joi = $joi->email; | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | Sets L</format> to L<email|JSON::Validator/email>. | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =head2 extend | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | my $new_joi = $joi->extend($other_joi_object); | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | Will extend C<$joi> with the definitions in C<$other_joi_object> and return a | ||||||||||||||||||||||||||||
| 364 | new object. | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =head2 iso_date | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | Alias for L</date_time>. | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | =head2 integer | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | my $joi = $joi->integer; | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | Sets L</type> to "integer". | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =head2 items | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | my $joi = $joi->items($joi); | ||||||||||||||||||||||||||||
| 379 | my $joi = $joi->items([$joi, ...]); | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | Defines a list of items for the L</array> type. | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | =head2 length | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | my $joi = $joi->length(10); | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | Sets both L</min> and L</max> to the number provided. | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =head2 lowercase | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | my $joi = $joi->lowercase; | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | Will set L</regex> to only match lower case strings. | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =head2 negative | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | my $joi = $joi->negative; | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | Sets L</max> to C<0>. | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =head2 number | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | my $joi = $joi->number; | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | Sets L</type> to "number". | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =head2 object | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | my $joi = $joi->object; | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | Sets L</type> to "object". | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | =head2 pattern | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | Alias for L</regex>. | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =head2 positive | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | my $joi = $joi->positive; | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | Sets L</min> to C<0>. | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | =head2 props | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | my $joi = $joi->props(name => JSON::Validator::Joi->new->string, ...); | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | Used to define properties for an L</object> type. Each key is the name of the | ||||||||||||||||||||||||||||
| 428 | parameter and the values must be a L<JSON::Validator::Joi> object. | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =head2 required | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | my $joi = $joi->required; | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | Marks the current property as required. | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | =head2 strict | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | my $joi = $joi->strict; | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | Sets L</array> and L</object> to not allow any more items/keys than what is defined. | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =head2 string | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | my $joi = $joi->string; | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | Sets L</type> to "string". | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | =head2 token | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | my $joi = $joi->token; | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | Sets L</regex> to C<^[a-zA-Z0-9_]+$>. | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | =head2 validate | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | my @errors = $joi->validate($data); | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | Used to validate C<$data> using L<JSON::Validator/validate>. Returns a list of | ||||||||||||||||||||||||||||
| 459 | L<JSON::Validator::Error|JSON::Validator/ERROR OBJECT> objects on invalid | ||||||||||||||||||||||||||||
| 460 | input. | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =head2 unique | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | my $joi = $joi->unique; | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | Used to force the L</array> to only contain unique items. | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | =head2 uppercase | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | my $joi = $joi->uppercase; | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | Will set L</regex> to only match upper case strings. | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | =head2 uri | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | my $joi = $joi->uri; | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | Sets L</format> to L<uri|JSON::Validator/uri>. | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | L<JSON::Validator> | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | L<https://github.com/hapijs/joi>. | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | =cut | ||||||||||||||||||||||||||||