1
- package com .google .bos .iot .core .proxy ;
1
+ package com .google .udmi .util ;
2
+
3
+ import static com .google .common .base .Preconditions .checkArgument ;
4
+ import static com .google .common .collect .ImmutableList .toImmutableList ;
5
+ import static java .lang .String .format ;
2
6
3
7
import com .fasterxml .jackson .databind .JsonNode ;
4
8
import com .fasterxml .jackson .databind .ObjectMapper ;
5
9
import com .github .fge .jsonschema .core .exceptions .ProcessingException ;
6
10
import com .github .fge .jsonschema .core .load .configuration .LoadingConfiguration ;
7
11
import com .github .fge .jsonschema .core .load .download .URIDownloader ;
12
+ import com .github .fge .jsonschema .core .report .LogLevel ;
13
+ import com .github .fge .jsonschema .core .report .ProcessingMessage ;
8
14
import com .github .fge .jsonschema .core .report .ProcessingReport ;
9
15
import com .github .fge .jsonschema .main .JsonSchema ;
10
16
import com .github .fge .jsonschema .main .JsonSchemaFactory ;
11
17
import com .google .common .collect .ImmutableList ;
12
18
import com .google .common .collect .Maps ;
13
- import com .google .daq .mqtt .util .ValidationException ;
14
- import com .google .daq .mqtt .validator .Validator ;
15
19
import java .io .File ;
16
20
import java .io .FileInputStream ;
17
21
import java .io .IOException ;
20
24
import java .net .URL ;
21
25
import java .util .List ;
22
26
import java .util .Map ;
27
+ import java .util .regex .Pattern ;
28
+ import java .util .stream .StreamSupport ;
23
29
24
30
/**
25
31
* Validation wrapper for processing individual messages.
26
32
*/
27
33
public class MessageValidator {
28
34
29
35
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
36
+ @ SuppressWarnings ("checkstyle:linelength" )
37
+ private static final List <String > IGNORE_LIST = ImmutableList .of (
38
+ "instance type \\ (string\\ ) does not match any allowed primitive type \\ (allowed: \\ [.*\" number\" \\ ]\\ )" );
39
+ private static final List <Pattern > IGNORE_PATTERNS = IGNORE_LIST .stream ().map (Pattern ::compile )
40
+ .toList ();
30
41
31
42
private final Map <String , JsonSchema > schemaMap = Maps .newConcurrentMap ();
32
43
private final File schemaRoot ;
@@ -44,6 +55,39 @@ public MessageValidator(String schemaRootPath) {
44
55
}
45
56
}
46
57
58
+ /**
59
+ * From an external processing report.
60
+ *
61
+ * @param report Report to convert
62
+ * @return Converted exception.
63
+ */
64
+ public static ValidationException fromProcessingReport (ProcessingReport report ) {
65
+ checkArgument (!report .isSuccess (), "Report must not be successful" );
66
+ ImmutableList <ValidationException > causingExceptions =
67
+ StreamSupport .stream (report .spliterator (), false )
68
+ .filter (MessageValidator ::errorOrWorse )
69
+ .filter (MessageValidator ::notOnIgnoreList )
70
+ .map (MessageValidator ::convertMessage ).collect (toImmutableList ());
71
+ return causingExceptions .isEmpty () ? null : new ValidationException (
72
+ format ("%d schema violations found" , causingExceptions .size ()), causingExceptions );
73
+ }
74
+
75
+ private static boolean notOnIgnoreList (ProcessingMessage processingMessage ) {
76
+ return IGNORE_PATTERNS .stream ()
77
+ .noneMatch (p -> p .matcher (processingMessage .getMessage ()).matches ());
78
+ }
79
+
80
+ private static boolean errorOrWorse (ProcessingMessage processingMessage ) {
81
+ return processingMessage .getLogLevel ().compareTo (LogLevel .ERROR ) >= 0 ;
82
+ }
83
+
84
+ private static ValidationException convertMessage (ProcessingMessage processingMessage ) {
85
+ String pointer = processingMessage .asJson ().get ("instance" ).get ("pointer" ).asText ();
86
+ String prefix =
87
+ com .google .api .client .util .Strings .isNullOrEmpty (pointer ) ? "" : (pointer + ": " );
88
+ return new ValidationException (prefix + processingMessage .getMessage ());
89
+ }
90
+
47
91
/**
48
92
* Validate the indicated message.
49
93
*
@@ -58,7 +102,7 @@ public List<String> validateMessage(String subFolder, String data) {
58
102
if (report .isSuccess ()) {
59
103
return ImmutableList .of ();
60
104
}
61
- throw Validator . fromProcessingReport (report );
105
+ throw fromProcessingReport (report );
62
106
} catch (ValidationException e ) {
63
107
return e .getAllMessages ();
64
108
} catch (IOException | ProcessingException ex ) {
0 commit comments