Debian Codegraph @

t/stringy-numbers.t

raw
1 # vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et :
2 use strictures 2;
3 use 5.020;
4 use stable 0.031 'postderef';
5 use experimental 'signatures';
6 no autovivification warn => qw(fetch store exists delete);
7 use if "$]" >= 5.022, experimental => 're_strict';
8 no if "$]" >= 5.031009, feature => 'indirect';
9 no if "$]" >= 5.033001, feature => 'multidimensional';
10 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
11 no if "$]" >= 5.041009, feature => 'smartmatch';
12 no feature 'switch';
13 use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
14
15 use lib 't/lib';
16 use Helper;
17
18 foreach my $config (0, 1) {
19 note 'stringy_numbers = '.$config;
20 my $js = JSON::Schema::Modern->new(stringy_numbers => $config);
21
22 is_equal(
23 $js->evaluate(1, { $_ => '1' })->TO_JSON,
24 {
25 valid => false,
26 errors => [
27 {
28 keywordLocation => '/'.$_,
29 error => $_.' value is not a number',
30 }
31 ],
32 },
33 'strings cannot be used in place of numbers in schema for '.$_,
34 ) foreach qw(multipleOf maximum exclusiveMaximum minimum exclusiveMinimum);
35
36 my $schema = {
37 allOf => [
38 { type => 'string' },
39 { type => 'number' },
40 { type => 'integer' },
41 { type => [ 'object', 'number' ] },
42 { type => [ 'object', 'integer' ] },
43 ],
44 };
45
46 is_equal(
47 $js->evaluate('blah', $schema)->TO_JSON,
48 {
49 valid => false,
50 errors => my $errors = [
51 {
52 instanceLocation => '',
53 keywordLocation => '/allOf/1/type',
54 error => 'got string, not number',
55 },
56 {
57 instanceLocation => '',
58 keywordLocation => '/allOf/2/type',
59 error => 'got string, not integer',
60 },
61 {
62 instanceLocation => '',
63 keywordLocation => '/allOf/3/type',
64 error => 'got string, not one of object, number',
65 },
66 {
67 instanceLocation => '',
68 keywordLocation => '/allOf/4/type',
69 error => 'got string, not one of object, integer',
70 },
71 {
72 instanceLocation => '',
73 keywordLocation => '/allOf',
74 error => 'subschemas 1, 2, 3, 4 are not valid',
75 },
76 ],
77 },
78 'strings that do not look like numbers are never valid as numbers',
79 );
80
81 is_equal(
82 $js->evaluate('1.1', $schema)->TO_JSON,
83 {
84 valid => false,
85 errors => $errors,
86 },
87 'by default "type": "string" does not accept numbers',
88 ) if not $config;
89
90 is_equal(
91 $js->evaluate('1.1', $schema)->TO_JSON,
92 {
93 valid => false,
94 errors => [
95 {
96 instanceLocation => '',
97 keywordLocation => '/allOf/2/type',
98 error => 'got string, not integer',
99 },
100 {
101 instanceLocation => '',
102 keywordLocation => '/allOf/4/type',
103 error => 'got string, not one of object, integer',
104 },
105 {
106 instanceLocation => '',
107 keywordLocation => '/allOf',
108 error => 'subschemas 2, 4 are not valid',
109 },
110 ],
111 },
112 'using stringy numbers, numeric strings are treated as numbers but are still not always integers',
113 ) if $config;
114
115
116 $schema = {
117 maximum => 5,
118 exclusiveMaximum => 5,
119 minimum => 15,
120 exclusiveMinimum => 15,
121 allOf => [
122 { multipleOf => 2 },
123 { multipleOf => 0.3 },
124 ],
125 };
126
127 $errors = [
128 {
129 instanceLocation => '',
130 keywordLocation => '/maximum',
131 error => 'value is greater than 5',
132 },
133 {
134 instanceLocation => '',
135 keywordLocation => '/exclusiveMaximum',
136 error => 'value is greater than or equal to 5',
137 },
138 {
139 instanceLocation => '',
140 keywordLocation => '/minimum',
141 error => 'value is less than 15',
142 },
143 {
144 instanceLocation => '',
145 keywordLocation => '/exclusiveMinimum',
146 error => 'value is less than or equal to 15',
147 },
148 {
149 instanceLocation => '',
150 keywordLocation => '/allOf/0/multipleOf',
151 error => 'value is not a multiple of 2',
152 },
153 {
154 instanceLocation => '',
155 keywordLocation => '/allOf/1/multipleOf',
156 error => 'value is not a multiple of 0.3',
157 },
158 {
159 instanceLocation => '',
160 keywordLocation => '/allOf',
161 error => 'subschemas 0, 1 are not valid',
162 },
163 ];
164
165 my $data = 11e0;
166
167 is_equal(
168 $js->evaluate($data, $schema)->TO_JSON,
169 {
170 valid => false,
171 errors => $errors,
172 },
173 'real numbers are always evaluated',
174 );
175
176 $data = '11e0';
177
178 is_equal(
179 $js->evaluate($data, $schema)->TO_JSON,
180 { valid => true },
181 'by default, stringy numbers are not evaluated by numeric keywords',
182 ) if $config == 0;
183
184 is_equal(
185 $js->evaluate($data, $schema)->TO_JSON,
186 {
187 valid => false,
188 errors => $errors,
189 },
190 'with the config enabled, stringy numbers are treated as numbers by numeric keywords',
191 ) if $config == 1;
192
193 is(JSON::Schema::Modern::Utilities::get_type($data), 'string', 'data was not mutated');
194
195
196 $schema = {
197 enum => [11, 12],
198 const => 11,
199 };
200
201 is_equal(
202 $js->evaluate($data, $schema)->TO_JSON,
203 {
204 valid => false,
205 errors => [
206 {
207 instanceLocation => '',
208 keywordLocation => '/enum',
209 error => 'value does not match (from enum 0: wrong type: string vs integer; from enum 1: wrong type: string vs integer)',
210 },
211 {
212 instanceLocation => '',
213 keywordLocation => '/const',
214 error => 'value does not match (wrong type: string vs integer)',
215 },
216 ],
217 },
218 'by default, stringy numbers are not the same as numbers using comparison keywords',
219 ) if $config == 0;
220
221 is_equal(
222 $js->evaluate($data, $schema)->TO_JSON,
223 { valid => true },
224 'with the config enabled, stringy numbers are the same as numbers using comparison keywords',
225 ) if $config == 1;
226
227 is(JSON::Schema::Modern::Utilities::get_type($data), 'string', 'data was not mutated');
228 }
229
230 done_testing;