| 1 | package JSV::Validator; | ||||||||||||
| 2 | |||||||||||||
| 3 | 47 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 47 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 47 | ▶ | use Class::Accessor::Lite ( | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 47 | new => 0, | |||||||||||
| 8 | rw => [qw/ | ||||||||||||
| 9 | reference | ||||||||||||
| 10 | environment | ||||||||||||
| 11 | environment_keywords | ||||||||||||
| 12 | enable_format | ||||||||||||
| 13 | enable_history | ||||||||||||
| 14 | throw_error | ||||||||||||
| 15 | throw_immediate | ||||||||||||
| 16 | formats | ||||||||||||
| 17 | /] | ||||||||||||
| 18 | 47 | ); | |||||||||||
| 19 | 47 | ▶ | use Clone qw(clone); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 47 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 47 | ▶ | use JSV::Keyword qw(:constants); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 47 | ▶ | use JSV::Reference; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 47 | ▶ | use JSV::Context; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | 47 | ▶ | use Module::Pluggable::Object; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 25 | |||||||||||||
| 26 | 47 | our $VERSION = "0.08"; | |||||||||||
| 27 | |||||||||||||
| 28 | 47 | my %supported_environments = ( | |||||||||||
| 29 | draft4 => "Draft4" | ||||||||||||
| 30 | ); | ||||||||||||
| 31 | 47 | my %environment_keywords = (); | |||||||||||
| 32 | |||||||||||||
| 33 | 47 | ▶ | sub load_environments { | ||||||||||
|
Subroutine
load_environments: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||
| 34 | 47 | my ($class, @environments) = @_; | |||||||||||
| 35 | |||||||||||||
| 36 | 47 | for my $environment (@environments) { | |||||||||||
| 37 | 47 | ▶ | next unless (exists $supported_environments{$environment}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | my $finder = Module::Pluggable::Object->new( | ||||||||||||
| 39 | 47 | search_path => ["JSV::Keyword::" . $supported_environments{$environment}], | |||||||||||
| 40 | require => 1, | ||||||||||||
| 41 | ); | ||||||||||||
| 42 | |||||||||||||
| 43 | 47 | $environment_keywords{$environment} = { | |||||||||||
| 44 | INSTANCE_TYPE_NUMERIC() => [], | ||||||||||||
| 45 | INSTANCE_TYPE_STRING() => [], | ||||||||||||
| 46 | INSTANCE_TYPE_ARRAY() => [], | ||||||||||||
| 47 | INSTANCE_TYPE_OBJECT() => [], | ||||||||||||
| 48 | INSTANCE_TYPE_ANY() => [], | ||||||||||||
| 49 | }; | ||||||||||||
| 50 | my @keywords = | ||||||||||||
| 51 | 47 | sort { $a->keyword_priority <=> $b->keyword_priority } | |||||||||||
| 52 | $finder->plugins; | ||||||||||||
| 53 | |||||||||||||
| 54 | 47 | for my $keyword (@keywords) { | |||||||||||
| 55 | 1128 | my $type = $keyword->instance_type; | |||||||||||
| 56 | 1128 | push(@{$environment_keywords{$environment}{$type}}, $keyword); | |||||||||||
| 57 | } | ||||||||||||
| 58 | } | ||||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 62 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 62 | 62 | my $class = shift; | |||||||||||
| 63 | 62 | my %args = @_; | |||||||||||
| 64 | %args = ( | ||||||||||||
| 65 | environment => 'draft4', | ||||||||||||
| 66 | enable_format => 1, | ||||||||||||
| 67 | enable_history => 0, | ||||||||||||
| 68 | reference => JSV::Reference->new, | ||||||||||||
| 69 | formats => +{ | ||||||||||||
| 70 | 6 | ▶ | 'date' => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 71 | 6 | ($_[0] =~ /\A\d{4}-\d{2}-\d{2}\z/); | |||||||||||
| 72 | }, | ||||||||||||
| 73 | 10 | ▶ | 'date-time' => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 74 | # RFC3339 | ||||||||||||
| 75 | 10 | ($_[0] =~ /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})/); | |||||||||||
| 76 | }, | ||||||||||||
| 77 | 2 | ▶ | uri => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 78 | 2 | require Data::Validate::URI; | |||||||||||
| 79 | 2 | Data::Validate::URI::is_uri($_[0]); | |||||||||||
| 80 | }, | ||||||||||||
| 81 | 4 | ▶ | email => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 82 | 4 | require Email::Valid::Loose; | |||||||||||
| 83 | 4 | Email::Valid::Loose->address($_[0]); | |||||||||||
| 84 | }, | ||||||||||||
| 85 | 10 | ▶ | ipv4 => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 86 | 10 | require Data::Validate::IP; | |||||||||||
| 87 | 10 | Data::Validate::IP::is_ipv4($_[0]); | |||||||||||
| 88 | }, | ||||||||||||
| 89 | 8 | ▶ | ipv6 => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 90 | 8 | require Data::Validate::IP; | |||||||||||
| 91 | 8 | Data::Validate::IP::is_ipv6($_[0]); | |||||||||||
| 92 | }, | ||||||||||||
| 93 | 8 | ▶ | hostname => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 94 | 8 | require Data::Validate::Domain; | |||||||||||
| 95 | 8 | Data::Validate::Domain::is_domain($_[0]); | |||||||||||
| 96 | }, | ||||||||||||
| 97 | }, | ||||||||||||
| 98 | 62 | %args, | |||||||||||
| 99 | ); | ||||||||||||
| 100 | |||||||||||||
| 101 | ### RECOMMENDED: you should do to preloading environment before calling constructor | ||||||||||||
| 102 | 62 | ▶ | unless (exists $environment_keywords{$args{environment}}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | 47 | $class->load_environments($args{environment}); | |||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | bless { | ||||||||||||
| 107 | 62 | environment_keywords => \%environment_keywords, | |||||||||||
| 108 | %args, | ||||||||||||
| 109 | } => $class; | ||||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 768 | ▶ | sub validate { | ||||||||||
|
Subroutine
validate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 113 | 768 | my ($self, $schema, $instance, $opts) = @_; | |||||||||||
| 114 | |||||||||||||
| 115 | 768 | ▶ | $opts ||= +{}; | ||||||||||
|
Condition100%2/2
$opts ||= {}
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||
| 116 | 768 | %$opts = ( | |||||||||||
| 117 | loose_type => 0, | ||||||||||||
| 118 | %$opts, | ||||||||||||
| 119 | ); | ||||||||||||
| 120 | |||||||||||||
| 121 | my $context = JSV::Context->new( | ||||||||||||
| 122 | keywords => +{ | ||||||||||||
| 123 | INSTANCE_TYPE_ANY() => $self->instance_type_keywords(INSTANCE_TYPE_ANY), | ||||||||||||
| 124 | INSTANCE_TYPE_NUMERIC() => $self->instance_type_keywords(INSTANCE_TYPE_NUMERIC), | ||||||||||||
| 125 | INSTANCE_TYPE_STRING() => $self->instance_type_keywords(INSTANCE_TYPE_STRING), | ||||||||||||
| 126 | INSTANCE_TYPE_ARRAY() => $self->instance_type_keywords(INSTANCE_TYPE_ARRAY), | ||||||||||||
| 127 | INSTANCE_TYPE_OBJECT() => $self->instance_type_keywords(INSTANCE_TYPE_OBJECT), | ||||||||||||
| 128 | }, | ||||||||||||
| 129 | reference => $self->reference, | ||||||||||||
| 130 | environment => $self->environment, | ||||||||||||
| 131 | original_schema => $schema, | ||||||||||||
| 132 | throw_error => $self->throw_error, | ||||||||||||
| 133 | throw_immediate => $self->throw_immediate, | ||||||||||||
| 134 | enable_history => $self->enable_history, | ||||||||||||
| 135 | enable_format => $self->enable_format, | ||||||||||||
| 136 | formats => $self->formats, | ||||||||||||
| 137 | history => [], | ||||||||||||
| 138 | errors => [], | ||||||||||||
| 139 | current_pointer => "", | ||||||||||||
| 140 | current_schema_pointer => "", | ||||||||||||
| 141 | schema_pointer_history => [], | ||||||||||||
| 142 | json => JSON->new->allow_nonref, | ||||||||||||
| 143 | loose_type => $opts->{loose_type}, | ||||||||||||
| 144 | 768 | ); | |||||||||||
| 145 | |||||||||||||
| 146 | 768 | return $context->validate($schema, $instance); | |||||||||||
| 147 | } | ||||||||||||
| 148 | |||||||||||||
| 149 | 3840 | ▶ | sub instance_type_keywords { | ||||||||||
|
Subroutine
instance_type_keywords: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 150 | 3840 | my ($self, $instance_type) = @_; | |||||||||||
| 151 | 3840 | return $self->environment_keywords->{$self->environment}{$instance_type}; | |||||||||||
| 152 | } | ||||||||||||
| 153 | |||||||||||||
| 154 | 4 | ▶ | sub register_schema { | ||||||||||
|
Subroutine
register_schema: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 155 | 4 | shift->reference->register_schema(@_); | |||||||||||
| 156 | } | ||||||||||||
| 157 | |||||||||||||
| 158 | 0 | ▶ | sub unregister_schema { | ||||||||||
|
Subroutine
unregister_schema: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 159 | 0 | shift->reference->unregister_schema(@_); | |||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | 2 | ▶ | sub register_format { | ||||||||||
|
Subroutine
register_format: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 163 | 2 | my ($self, $format, $format_validator) = @_; | |||||||||||
| 164 | 2 | shift->formats->{$format} = $format_validator; | |||||||||||
| 165 | } | ||||||||||||
| 166 | |||||||||||||
| 167 | 47 | 1; | |||||||||||
| 168 | |||||||||||||
| 169 | __END__ | ||||||||||||