19
19
20
20
package com .github .fge .jsonschema .processors .validation ;
21
21
22
- import com .fasterxml .jackson .databind .JsonNode ;
23
- import com .fasterxml .jackson .databind .node .ArrayNode ;
24
- import com .github .fge .jackson .JacksonUtils ;
25
- import com .github .fge .jackson .jsonpointer .JsonPointer ;
26
22
import com .github .fge .jsonschema .cfg .ValidationConfiguration ;
27
- import com .github .fge .jsonschema .core .exceptions .InvalidSchemaException ;
28
23
import com .github .fge .jsonschema .core .exceptions .ProcessingException ;
29
- import com .github .fge .jsonschema .core .processing .CachingProcessor ;
30
24
import com .github .fge .jsonschema .core .processing .Processor ;
31
- import com .github .fge .jsonschema .core .report .ProcessingMessage ;
32
25
import com .github .fge .jsonschema .core .report .ProcessingReport ;
33
- import com .github .fge .jsonschema .core .tree .JsonTree ;
34
- import com .github .fge .jsonschema .core .tree .SchemaTree ;
35
- import com .github .fge .jsonschema .keyword .validator .KeywordValidator ;
36
26
import com .github .fge .jsonschema .processors .data .FullData ;
37
27
import com .github .fge .jsonschema .processors .data .SchemaContext ;
38
28
import com .github .fge .jsonschema .processors .data .ValidatorList ;
39
29
import com .github .fge .msgsimple .bundle .MessageBundle ;
40
- import com .google .common .cache .CacheLoader ;
41
- import com .google .common .collect .Lists ;
42
-
43
- import java .util .Collections ;
44
- import java .util .List ;
45
30
46
31
/**
47
32
* Main validation processor
@@ -66,156 +51,9 @@ public FullData process(final ProcessingReport report,
66
51
final FullData input )
67
52
throws ProcessingException
68
53
{
69
- /*
70
- * Build a validation context, attach a report to it
71
- */
72
- final SchemaContext context = new SchemaContext (input );
73
-
74
- /*
75
- * Get the full context from the cache. Inject the messages into the
76
- * main report.
77
- */
78
- final ValidatorList fullContext = processor .process (report , context );
79
-
80
- if (fullContext == null ) {
81
- /*
82
- * OK, that's for issue #99 but that's ugly nevertheless.
83
- *
84
- * We want syntax error messages to appear in the exception text.
85
- */
86
- final String msg = syntaxMessages .getMessage ("core.invalidSchema" );
87
- final ArrayNode arrayNode = JacksonUtils .nodeFactory ().arrayNode ();
88
- JsonNode node ;
89
- for (final ProcessingMessage message : report ) {
90
- node = message .asJson ();
91
- if ("syntax" .equals (node .path ("domain" ).asText ()))
92
- arrayNode .add (node );
93
- }
94
- final StringBuilder sb = new StringBuilder (msg );
95
- sb .append ("\n Syntax errors:\n " );
96
- sb .append (JacksonUtils .prettyPrint (arrayNode ));
97
- final ProcessingMessage message = new ProcessingMessage ()
98
- .setMessage (sb .toString ());
99
- throw new InvalidSchemaException (message );
100
- }
101
-
102
- /*
103
- * Get the calculated context. Build the data.
104
- */
105
- final SchemaContext newContext = fullContext .getContext ();
106
- final FullData data = new FullData (newContext .getSchema (),
107
- input .getInstance (), input .isDeepCheck ());
108
-
109
- /*
110
- * Validate against all keywords.
111
- */
112
- for (final KeywordValidator validator : fullContext )
113
- validator .validate (this , report , validationMessages , data );
114
-
115
- /*
116
- * At that point, if the report is a failure, we quit: there is no
117
- * reason to go any further. Unless the user has asked to continue even
118
- * in this case.
119
- */
120
- if (!(report .isSuccess () || data .isDeepCheck ()))
121
- return input ;
122
-
123
- /*
124
- * Now check whether this is a container node with a size greater than
125
- * 0. If not, no need to go see the children.
126
- */
127
- final JsonNode node = data .getInstance ().getNode ();
128
- if (node .size () == 0 )
129
- return input ;
130
-
131
- if (node .isArray ())
132
- processArray (report , data );
133
- else
134
- processObject (report , data );
135
-
136
- return input ;
137
- }
138
-
139
- private void processArray (final ProcessingReport report ,
140
- final FullData input )
141
- throws ProcessingException
142
- {
143
- final SchemaTree tree = input .getSchema ();
144
- final JsonTree instance = input .getInstance ();
145
-
146
- final JsonNode schema = tree .getNode ();
147
- final JsonNode node = instance .getNode ();
148
-
149
- final JsonNode digest = ArraySchemaDigester .getInstance ().digest (schema );
150
- final ArraySchemaSelector selector = new ArraySchemaSelector (digest );
151
-
152
- final int size = node .size ();
153
-
154
- FullData data ;
155
- JsonTree newInstance ;
156
-
157
- for (int index = 0 ; index < size ; index ++) {
158
- newInstance = instance .append (JsonPointer .of (index ));
159
- data = input .withInstance (newInstance );
160
- for (final JsonPointer ptr : selector .selectSchemas (index )) {
161
- data = data .withSchema (tree .append (ptr ));
162
- process (report , data );
163
- }
164
- }
165
- }
166
-
167
- private void processObject (final ProcessingReport report ,
168
- final FullData input )
169
- throws ProcessingException
170
- {
171
- final SchemaTree tree = input .getSchema ();
172
- final JsonTree instance = input .getInstance ();
173
-
174
- final JsonNode schema = tree .getNode ();
175
- final JsonNode node = instance .getNode ();
176
-
177
- final JsonNode digest = ObjectSchemaDigester .getInstance ()
178
- .digest (schema );
179
- final ObjectSchemaSelector selector = new ObjectSchemaSelector (digest );
180
-
181
- final List <String > fields = Lists .newArrayList (node .fieldNames ());
182
- Collections .sort (fields );
183
-
184
- FullData data ;
185
- JsonTree newInstance ;
186
-
187
- for (final String field : fields ) {
188
- newInstance = instance .append (JsonPointer .of (field ));
189
- data = input .withInstance (newInstance );
190
- for (final JsonPointer ptr : selector .selectSchemas (field )) {
191
- data = data .withSchema (tree .append (ptr ));
192
- process (report , data );
193
- }
194
- }
195
- }
196
-
197
- private static CacheLoader <JsonNode , ArraySchemaSelector > arrayLoader ()
198
- {
199
- return new CacheLoader <JsonNode , ArraySchemaSelector >()
200
- {
201
- @ Override
202
- public ArraySchemaSelector load (final JsonNode key )
203
- {
204
- return new ArraySchemaSelector (key );
205
- }
206
- };
207
- }
208
-
209
- private static CacheLoader <JsonNode , ObjectSchemaSelector > objectLoader ()
210
- {
211
- return new CacheLoader <JsonNode , ObjectSchemaSelector >()
212
- {
213
- @ Override
214
- public ObjectSchemaSelector load (final JsonNode key )
215
- {
216
- return new ObjectSchemaSelector (key );
217
- }
218
- };
54
+ final InstanceValidator validator = new InstanceValidator (
55
+ syntaxMessages , validationMessages , processor );
56
+ return validator .process (report , input );
219
57
}
220
58
221
59
@ Override
0 commit comments