File Coverage

blib/lib/JSON/Schema/Modern/Vocabulary/FormatAnnotation.pm
Criterion Covered Total %
statement 102 103 99.0
branch 16 18 88.8
condition 12 15 80.0
subroutine 26 26 100.0
pod 0 3 0.0
total 156 165 94.5


line stmt bran cond sub pod time code
1 38     38   1182 use strict;
  38         77  
  38         1391  
2 38     38   155 use warnings;
  38         68  
  38         3124  
3             package JSON::Schema::Modern::Vocabulary::FormatAnnotation;
4             # vim: set ts=8 sts=2 sw=2 tw=100 et :
5             # ABSTRACT: Implementation of the JSON Schema Format-Annotation vocabulary
6              
7             our $VERSION = '0.635';
8              
9 38     38   661 use 5.020;
  38         140  
10 38     38   146 use Moo;
  38         64  
  38         293  
11 38     38   12731 use strictures 2;
  38         361  
  38         1358  
12 38     38   14207 use stable 0.031 'postderef';
  38         561  
  38         396  
13 38     38   6971 use experimental 'signatures';
  38         101  
  38         127  
14 38     38   1932 no autovivification warn => qw(fetch store exists delete);
  38         64  
  38         349  
15 38     38   2609 use if "$]" >= 5.022, experimental => 're_strict';
  38         77  
  38         930  
16 38     38   2677 no if "$]" >= 5.031009, feature => 'indirect';
  38         74  
  38         2147  
17 38     38   158 no if "$]" >= 5.033001, feature => 'multidimensional';
  38         85  
  38         1721  
18 38     38   178 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  38         74  
  38         1662  
19 38     38   155 no if "$]" >= 5.041009, feature => 'smartmatch';
  38         76  
  38         1365  
20 38     38   148 no feature 'switch';
  38         77  
  38         1013  
21 38     38   150 use Feature::Compat::Try;
  38         62  
  38         441  
22 38     38   1967 use JSON::Schema::Modern::Utilities qw(A E assert_keyword_type get_type);
  38         72  
  38         2812  
23 38     38   18828 use JSON::Schema::Modern::Vocabulary::FormatAssertion;
  38         104  
  38         2179  
24 38     38   242 use if "$]" < 5.041010, 'List::Util' => 'any';
  38         67  
  38         1622  
25 38     38   154 use if "$]" >= 5.041010, experimental => 'keyword_any';
  38         65  
  38         545  
26 38     38   2658 use Scalar::Util 'looks_like_number';
  38         84  
  38         1932  
27 38     38   165 use namespace::clean;
  38         62  
  38         202  
28              
29             with 'JSON::Schema::Modern::Vocabulary';
30              
31 22     22 0 48 sub vocabulary ($class) {
  22         41  
  22         31  
32 22         68 'https://json-schema.org/draft/2019-09/vocab/format' => 'draft2019-09',
33             'https://json-schema.org/draft/2020-12/vocab/format-annotation' => 'draft2020-12';
34             }
35              
36 1     1 0 2 sub evaluation_order ($class) { 2 }
  1         1  
  1         3  
  1         11  
37              
38 160     160 0 1105 sub keywords ($class, $spec_version) {
  160         296  
  160         244  
  160         252  
39 160         1371 qw(format);
40             }
41              
42 1461     1461   2131 sub _traverse_keyword_format ($class, $schema, $state) {
  1461         2221  
  1461         1825  
  1461         1857  
  1461         1780  
43 1461 50       4118 return if not assert_keyword_type($state, $schema, 'string');
44 1461         3532 return 1;
45             }
46              
47 1987     1987   3394 sub _eval_keyword_format ($class, $data, $schema, $state) {
  1987         3048  
  1987         3435  
  1987         2714  
  1987         2555  
  1987         2527  
48 1987         7809 A($state, $schema->{format});
49 1987 100       6519 return 1 if not $state->{validate_formats};
50              
51             # { type => .., sub => .. }
52             my $spec = $state->{evaluator}->_get_format_validation($schema->{format})
53 938   100     4021 // JSON::Schema::Modern::Vocabulary::FormatAssertion->_get_default_format_validation($state, $schema->{format});
54              
55             # §7.2.1 (draft2020-12) "Specifying the Format-Annotation vocabulary and enabling validation in an
56             # implementation should not be viewed as being equivalent to specifying the Format-Assertion
57             # vocabulary since implementations are not required to provide full validation support when the
58             # Format-Assertion vocabulary is not specified."
59             # §7.2.3 (draft2019-09) "An implementation MUST NOT fail validation or cease processing due to an
60             # unknown format attribute."
61 938 100       2098 return 1 if not $spec;
62              
63 781         1803 my $type = get_type($data);
64 781 100       1736 $type = 'number' if $type eq 'integer';
65              
66             return 1 if
67 67         195 not ref $spec->{type} eq 'ARRAY' ? any { $type eq $_ } $spec->{type}->@* : $type eq $spec->{type}
68             and not ($state->{stringy_numbers} and $type eq 'string'
69 781 100 66     15304 and ref $spec->{type} eq 'ARRAY' ? any { $_ eq 'number' } $spec->{type}->@* : $spec->{type} eq 'number'
  0 50 66     0  
    100 66        
      100        
70             and looks_like_number($data));
71              
72 429         614 my $valid;
73 429         794 try {
74 429         1255 $valid = $spec->{sub}->($data);
75             }
76             catch ($e) {
77             # we treat a missing optional prereq as if the format is not implemented, therefore it is valid
78 43         1302 $valid = 1;
79             }
80              
81             return E($state, 'not a valid %s %s', $schema->{format},
82             ref $spec->{type} eq 'ARRAY' ? join(', ', $spec->{type}->@*) : $spec->{type})
83 429 100       11779 if not $valid;
    100          
84 230         914 return 1;
85             }
86              
87             1;
88              
89             __END__