Skip to content

Commit 7522176

Browse files
authored
changed from isIntegralNumber to canConvertToExactIntegral to support numbers with 0 decimal value as valid integers for max/min lengths. (networknt#450)
1 parent a03b92c commit 7522176

12 files changed

+236
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class MaxItemsValidator extends BaseJsonValidator implements JsonValidato
3333

3434
public MaxItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3535
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MAX_ITEMS, validationContext);
36-
if (schemaNode.isIntegralNumber()) {
36+
if (schemaNode.canConvertToExactIntegral()) {
3737
max = schemaNode.intValue();
3838
}
3939
this.validationContext = validationContext;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class MaxLengthValidator extends BaseJsonValidator implements JsonValidat
3333
public MaxLengthValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3434
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MAX_LENGTH, validationContext);
3535
maxLength = Integer.MAX_VALUE;
36-
if (schemaNode != null && schemaNode.isIntegralNumber()) {
36+
if (schemaNode != null && schemaNode.canConvertToExactIntegral()) {
3737
maxLength = schemaNode.intValue();
3838
}
3939
this.validationContext = validationContext;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class MaxPropertiesValidator extends BaseJsonValidator implements JsonVal
3131
public MaxPropertiesValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
3232
ValidationContext validationContext) {
3333
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MAX_PROPERTIES, validationContext);
34-
if (schemaNode.isIntegralNumber()) {
34+
if (schemaNode.canConvertToExactIntegral()) {
3535
max = schemaNode.intValue();
3636
}
3737

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class MinItemsValidator extends BaseJsonValidator implements JsonValidato
3232

3333
public MinItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3434
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MIN_ITEMS, validationContext);
35-
if (schemaNode.isIntegralNumber()) {
35+
if (schemaNode.canConvertToExactIntegral()) {
3636
min = schemaNode.intValue();
3737
}
3838
this.validationContext = validationContext;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class MinLengthValidator extends BaseJsonValidator implements JsonValidat
3333
public MinLengthValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3434
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MIN_LENGTH, validationContext);
3535
minLength = Integer.MIN_VALUE;
36-
if (schemaNode != null && schemaNode.isIntegralNumber()) {
36+
if (schemaNode != null && schemaNode.canConvertToExactIntegral()) {
3737
minLength = schemaNode.intValue();
3838
}
3939
this.validationContext = validationContext;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class MinPropertiesValidator extends BaseJsonValidator implements JsonVal
3131
public MinPropertiesValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
3232
ValidationContext validationContext) {
3333
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MIN_PROPERTIES, validationContext);
34-
if (schemaNode.isIntegralNumber()) {
34+
if (schemaNode.canConvertToExactIntegral()) {
3535
min = schemaNode.intValue();
3636
}
3737

src/test/resources/draft7/maxItems.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,42 @@
3535
"valid": true
3636
}
3737
]
38+
},
39+
{
40+
"description": "maxItems decimal validation",
41+
"schema": {
42+
"maxItems": 2.0
43+
},
44+
"tests": [
45+
{
46+
"description": "shorter is valid",
47+
"data": [
48+
1
49+
],
50+
"valid": true
51+
},
52+
{
53+
"description": "exact length is valid",
54+
"data": [
55+
1,
56+
2
57+
],
58+
"valid": true
59+
},
60+
{
61+
"description": "too long is invalid",
62+
"data": [
63+
1,
64+
2,
65+
3
66+
],
67+
"valid": false
68+
},
69+
{
70+
"description": "ignores non-arrays",
71+
"data": "foobar",
72+
"valid": true
73+
}
74+
]
3875
}
3976
]

src/test/resources/draft7/maxLength.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,38 @@
3131
"valid": true
3232
}
3333
]
34+
},
35+
{
36+
"description": "maxLength decimal validation",
37+
"schema": {
38+
"maxLength": 2.0
39+
},
40+
"tests": [
41+
{
42+
"description": "shorter is valid",
43+
"data": "f",
44+
"valid": true
45+
},
46+
{
47+
"description": "exact length is valid",
48+
"data": "fo",
49+
"valid": true
50+
},
51+
{
52+
"description": "too long is invalid",
53+
"data": "foo",
54+
"valid": false
55+
},
56+
{
57+
"description": "ignores non-strings",
58+
"data": 100,
59+
"valid": true
60+
},
61+
{
62+
"description": "two supplementary Unicode code points is long enough",
63+
"data": "\uD83D\uDCA9\uD83D\uDCA9",
64+
"valid": true
65+
}
66+
]
3467
}
3568
]

src/test/resources/draft7/maxProperties.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,56 @@
4949
"valid": true
5050
}
5151
]
52+
},
53+
{
54+
"description": "maxProperties decimal validation",
55+
"schema": {
56+
"maxProperties": 2.0
57+
},
58+
"tests": [
59+
{
60+
"description": "shorter is valid",
61+
"data": {
62+
"foo": 1
63+
},
64+
"valid": true
65+
},
66+
{
67+
"description": "exact length is valid",
68+
"data": {
69+
"foo": 1,
70+
"bar": 2
71+
},
72+
"valid": true
73+
},
74+
{
75+
"description": "too long is invalid",
76+
"data": {
77+
"foo": 1,
78+
"bar": 2,
79+
"baz": 3
80+
},
81+
"valid": false
82+
},
83+
{
84+
"description": "ignores arrays",
85+
"data": [
86+
1,
87+
2,
88+
3
89+
],
90+
"valid": true
91+
},
92+
{
93+
"description": "ignores strings",
94+
"data": "foobar",
95+
"valid": true
96+
},
97+
{
98+
"description": "ignores other non-objects",
99+
"data": 12,
100+
"valid": true
101+
}
102+
]
52103
}
53104
]

src/test/resources/draft7/minItems.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,38 @@
3131
"valid": true
3232
}
3333
]
34+
},
35+
{
36+
"description": "minItems decimal validation",
37+
"schema": {
38+
"minItems": 1.0
39+
},
40+
"tests": [
41+
{
42+
"description": "longer is valid",
43+
"data": [
44+
1,
45+
2
46+
],
47+
"valid": true
48+
},
49+
{
50+
"description": "exact length is valid",
51+
"data": [
52+
1
53+
],
54+
"valid": true
55+
},
56+
{
57+
"description": "too short is invalid",
58+
"data": [],
59+
"valid": false
60+
},
61+
{
62+
"description": "ignores non-arrays",
63+
"data": "",
64+
"valid": true
65+
}
66+
]
3467
}
3568
]

src/test/resources/draft7/minLength.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,38 @@
3131
"valid": false
3232
}
3333
]
34+
},
35+
{
36+
"description": "minLength decimal validation",
37+
"schema": {
38+
"minLength": 2.0
39+
},
40+
"tests": [
41+
{
42+
"description": "longer is valid",
43+
"data": "foo",
44+
"valid": true
45+
},
46+
{
47+
"description": "exact length is valid",
48+
"data": "fo",
49+
"valid": true
50+
},
51+
{
52+
"description": "too short is invalid",
53+
"data": "f",
54+
"valid": false
55+
},
56+
{
57+
"description": "ignores non-strings",
58+
"data": 1,
59+
"valid": true
60+
},
61+
{
62+
"description": "one supplementary Unicode code point is not long enough",
63+
"data": "\uD83D\uDCA9",
64+
"valid": false
65+
}
66+
]
3467
}
3568
]

src/test/resources/draft7/minProperties.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,48 @@
4141
"valid": true
4242
}
4343
]
44+
},
45+
{
46+
"description": "minProperties decimal validation",
47+
"schema": {
48+
"minProperties": 1.0
49+
},
50+
"tests": [
51+
{
52+
"description": "longer is valid",
53+
"data": {
54+
"foo": 1,
55+
"bar": 2
56+
},
57+
"valid": true
58+
},
59+
{
60+
"description": "exact length is valid",
61+
"data": {
62+
"foo": 1
63+
},
64+
"valid": true
65+
},
66+
{
67+
"description": "too short is invalid",
68+
"data": {},
69+
"valid": false
70+
},
71+
{
72+
"description": "ignores arrays",
73+
"data": [],
74+
"valid": true
75+
},
76+
{
77+
"description": "ignores strings",
78+
"data": "",
79+
"valid": true
80+
},
81+
{
82+
"description": "ignores other non-objects",
83+
"data": 12,
84+
"valid": true
85+
}
86+
]
4487
}
4588
]

0 commit comments

Comments
 (0)