Skip to content

Commit ee0b21c

Browse files
committed
-fixed max validator when have an integer type but with a double max value
-added test cases
1 parent 278533e commit ee0b21c

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

src/main/java/com/networknt/schema/MaximumValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public MaximumValidator(String schemaPath, JsonNode schemaNode, JsonSchema paren
4646
}
4747

4848
parseErrorCode(getValidatorType().getErrorCodeKey());
49-
50-
if (!JsonType.INTEGER.toString().equals(getNodeFieldType())) {
49+
//if the schema type is not integer, or the maximum value is not an integer, try to compare using double values;
50+
if (!JsonType.INTEGER.toString().equals(getNodeFieldType()) || !JsonType.INTEGER.toString().equals(schemaNode.getNodeType())) {
5151
// "number" or no type
5252
// by default treat value as double: compatible with previous behavior
5353
final double dm = schemaNode.doubleValue();

src/test/java/com/networknt/schema/MaximumValidatorTest.java

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void doubleValueOverflow() throws IOException {
6363
JsonNode doc = mapper.readTree(value);
6464

6565
Set<ValidationMessage> messages = v.validate(doc);
66-
assertFalse(format("Expecing validation error with maximum %s and value %s", maximum, value), messages.isEmpty());
66+
assertFalse(format("Expecting validation error with maximum %s and value %s", maximum, value), messages.isEmpty());
6767
}
6868
}
6969

@@ -86,7 +86,7 @@ public void documentParsedWithBigDecimal() throws IOException {
8686
JsonNode doc = bigDecimalMapper.readTree(value);
8787

8888
Set<ValidationMessage> messages = v.validate(doc);
89-
assertFalse(format("Expecing validation error with maximum %s and value %s", maximum, value), messages.isEmpty());
89+
assertFalse(format("Expecting validation error with maximum %s and value %s", maximum, value), messages.isEmpty());
9090
}
9191
}
9292

@@ -109,7 +109,7 @@ public void documentAndSchemaParsedWithBigDecimal() throws IOException {
109109
JsonNode doc = bigDecimalMapper.readTree(value);
110110

111111
Set<ValidationMessage> messages = v.validate(doc);
112-
assertFalse(format("Expecing validation error with maximum %s and value %s", maximum, value), messages.isEmpty());
112+
assertFalse(format("Expecting validation error with maximum %s and value %s", maximum, value), messages.isEmpty());
113113
}
114114
}
115115

@@ -198,7 +198,7 @@ public void longUnderMaxValueOverflow() throws IOException {
198198
JsonNode doc = mapper.readTree(value);
199199

200200
Set<ValidationMessage> messages = v.validate(doc);
201-
assertFalse(format("Expecing validation error with maximum %s and value %s", maximum, value), messages.isEmpty());
201+
assertFalse(format("Expecting validation error with maximum %s and value %s", maximum, value), messages.isEmpty());
202202
}
203203
}
204204

@@ -218,7 +218,7 @@ public void longValueOverflowWithInverseEffect() throws IOException {
218218
JsonNode doc = mapper.readTree(value);
219219

220220
Set<ValidationMessage> messages = v.validate(doc);
221-
assertTrue(format("Expecing no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
221+
assertTrue(format("Expecting no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
222222
}
223223
}
224224

@@ -238,7 +238,7 @@ public void BigIntegerBothWithinLongRangePositive() throws IOException {
238238
JsonNode doc = bigIntegerMapper.readTree(value);
239239

240240
Set<ValidationMessage> messages = v.validate(doc);
241-
assertTrue(format("Expecing no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
241+
assertTrue(format("Expecting no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
242242
}
243243
}
244244

@@ -298,7 +298,7 @@ public void BigIntegerNotOverflow() throws IOException {
298298
JsonNode doc = bigIntegerMapper.readTree(value);
299299

300300
Set<ValidationMessage> messages = v.validate(doc);
301-
assertTrue(format("Expecing no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
301+
assertTrue(format("Expecting no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
302302
}
303303
}
304304

@@ -318,7 +318,7 @@ public void BigIntegerBothAboveLongRangePositive() throws IOException {
318318
JsonNode doc = bigIntegerMapper.readTree(value);
319319

320320
Set<ValidationMessage> messages = v.validate(doc);
321-
assertTrue(format("Expecing no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
321+
assertTrue(format("Expecting no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
322322
}
323323
}
324324

@@ -358,7 +358,7 @@ public void BigIntegerNotOverflowOnLongRangeEdge() throws IOException {
358358
JsonNode doc = bigIntegerMapper.readTree(value);
359359

360360
Set<ValidationMessage> messages = v.validate(doc);
361-
assertTrue(format("Expecing no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
361+
assertTrue(format("Expecting no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
362362
}
363363
}
364364

@@ -378,7 +378,47 @@ public void BigIntegerOverflowOnLongRangeEdge() throws IOException {
378378
JsonNode doc = bigIntegerMapper.readTree(value);
379379

380380
Set<ValidationMessage> messages = v.validate(doc);
381-
assertTrue(format("Expecing no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
381+
assertTrue(format("Expecting no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
382+
}
383+
}
384+
385+
@Test
386+
public void testIntegerTypeWithFloatMaxPositive() throws IOException {
387+
String[][] values = {
388+
// maximum, value
389+
{"37.7", "37"}
390+
};
391+
392+
for(String[] aTestCycle : values) {
393+
String maximum = aTestCycle[0];
394+
String value = aTestCycle[1];
395+
String schema = format("{ \"$schema\":\"http://json-schema.org/draft-04/schema#\", \"type\": \"integer\", \"maximum\": %s, \"exclusiveMaximum\": false}", maximum);
396+
397+
JsonSchema v = factory.getSchema(mapper.readTree(schema));
398+
JsonNode doc = bigIntegerMapper.readTree(value);
399+
400+
Set<ValidationMessage> messages = v.validate(doc);
401+
assertTrue(format("Expecting no validation errors as maximum %s is greater than value %s", maximum, value), messages.isEmpty());
402+
}
403+
}
404+
405+
@Test
406+
public void testIntegerTypeWithFloatMaxNegative() throws IOException {
407+
String[][] values = {
408+
// maximum, value
409+
{"37.7", "38"}
410+
};
411+
412+
for(String[] aTestCycle : values) {
413+
String maximum = aTestCycle[0];
414+
String value = aTestCycle[1];
415+
String schema = format("{ \"$schema\":\"http://json-schema.org/draft-04/schema#\", \"type\": \"integer\", \"maximum\": %s, \"exclusiveMaximum\": false}", maximum);
416+
417+
JsonSchema v = factory.getSchema(mapper.readTree(schema));
418+
JsonNode doc = bigIntegerMapper.readTree(value);
419+
420+
Set<ValidationMessage> messages = v.validate(doc);
421+
assertFalse(format("Expecting validation error with maximum %s and value %s", maximum, value), messages.isEmpty());
382422
}
383423
}
384424
}

0 commit comments

Comments
 (0)