17
17
package com .networknt .schema ;
18
18
19
19
import com .fasterxml .jackson .databind .JsonNode ;
20
+ import com .fasterxml .jackson .databind .node .ArrayNode ;
20
21
import com .fasterxml .jackson .databind .node .DecimalNode ;
21
22
import com .fasterxml .jackson .databind .node .NullNode ;
22
23
import org .slf4j .Logger ;
23
24
import org .slf4j .LoggerFactory ;
24
25
26
+ import java .math .BigDecimal ;
25
27
import java .util .Collections ;
26
28
import java .util .HashSet ;
27
29
import java .util .Set ;
@@ -45,7 +47,10 @@ public EnumValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath,
45
47
for (JsonNode n : schemaNode ) {
46
48
if (n .isNumber ()) {
47
49
// convert to DecimalNode for number comparison
48
- nodes .add (DecimalNode .valueOf (n .decimalValue ()));
50
+ nodes .add (processNumberNode (n ));
51
+ } else if (n .isArray ()) {
52
+ ArrayNode a = processArrayNode ((ArrayNode ) n );
53
+ nodes .add (a );
49
54
} else {
50
55
nodes .add (n );
51
56
}
@@ -78,7 +83,11 @@ public EnumValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath,
78
83
public Set <ValidationMessage > validate (ExecutionContext executionContext , JsonNode node , JsonNode rootNode , JsonNodePath instanceLocation ) {
79
84
debug (logger , node , rootNode , instanceLocation );
80
85
81
- if (node .isNumber ()) node = DecimalNode .valueOf (node .decimalValue ());
86
+ if (node .isNumber ()) {
87
+ node = processNumberNode (node );
88
+ } else if (node .isArray ()) {
89
+ node = processArrayNode ((ArrayNode ) node );
90
+ }
82
91
if (!nodes .contains (node ) && !( this .validationContext .getConfig ().isTypeLoose () && isTypeLooseContainsInEnum (node ))) {
83
92
return Collections .singleton (message ().instanceLocation (instanceLocation )
84
93
.locale (executionContext .getExecutionConfig ().getLocale ()).arguments (error ).build ());
@@ -105,4 +114,50 @@ private boolean isTypeLooseContainsInEnum(JsonNode node) {
105
114
return false ;
106
115
}
107
116
117
+ /**
118
+ * Processes the number and ensures trailing zeros are stripped.
119
+ *
120
+ * @param n the node
121
+ * @return the node
122
+ */
123
+ protected JsonNode processNumberNode (JsonNode n ) {
124
+ return DecimalNode .valueOf (new BigDecimal (n .decimalValue ().toPlainString ()));
125
+ }
126
+
127
+ /**
128
+ * Processes the array and ensures that numbers within have trailing zeroes stripped.
129
+ *
130
+ * @param node the node
131
+ * @return the node
132
+ */
133
+ protected ArrayNode processArrayNode (ArrayNode node ) {
134
+ if (!hasNumber (node )) {
135
+ return node ;
136
+ }
137
+ ArrayNode a = (ArrayNode ) node .deepCopy ();
138
+ for (int x = 0 ; x < a .size (); x ++) {
139
+ JsonNode v = a .get (x );
140
+ if (v .isNumber ()) {
141
+ v = processNumberNode (v );
142
+ a .set (x , v );
143
+ }
144
+ }
145
+ return a ;
146
+ }
147
+
148
+ /**
149
+ * Determines if the array node contains a number.
150
+ *
151
+ * @param node the node
152
+ * @return the node
153
+ */
154
+ protected boolean hasNumber (ArrayNode node ) {
155
+ for (int x = 0 ; x < node .size (); x ++) {
156
+ JsonNode v = node .get (x );
157
+ if (v .isNumber ()) {
158
+ return true ;
159
+ }
160
+ }
161
+ return false ;
162
+ }
108
163
}
0 commit comments