@@ -75,44 +75,37 @@ public YamlElement cloneWithoutId() {
75
75
public boolean isValid (@ Nullable List <@ NonNull String > errors , @ Nullable List <@ NonNull String > warnings ) {
76
76
// Check that uid is present
77
77
if (uid == null || uid .isBlank ()) {
78
- if (errors != null ) {
79
- errors .add ("thing uid is missing" );
80
- }
78
+ addToList (errors , "invalid thing: uid is missing while mandatory" );
81
79
return false ;
82
80
}
83
81
boolean ok = true ;
84
82
// Check that uid has at least 3 segments and each segment respects the expected syntax
85
83
String [] segments = uid .split (AbstractUID .SEPARATOR );
86
84
if (segments .length < 3 ) {
87
- if (errors != null ) {
88
- errors .add ("thing %s: uid contains insufficient segments" .formatted (uid ));
89
- }
85
+ addToList (errors , "invalid thing \" %s\" : not enough segments in uid; minimum 3 is expected" .formatted (uid ));
90
86
ok = false ;
91
87
}
92
88
for (String segment : segments ) {
93
89
if (!THING_UID_SEGMENT_PATTERN .matcher (segment ).matches ()) {
94
- if (errors != null ) {
95
- errors .add ("thing %s: segment \" %s\" in uid is not matching the expected syntax %s" .formatted (uid ,
96
- segment , THING_UID_SEGMENT_PATTERN .pattern ()));
97
- }
90
+ addToList (errors , "invalid thing \" %s\" : segment \" %s\" in uid not matching the expected syntax %s"
91
+ .formatted (uid , segment , THING_UID_SEGMENT_PATTERN .pattern ()));
98
92
ok = false ;
99
93
}
100
94
}
101
95
if (bridge != null && !bridge .isBlank ()) {
102
96
// Check that bridge has at least 3 segments and each segment respects the expected syntax
103
97
segments = bridge .split (AbstractUID .SEPARATOR );
104
98
if (segments .length < 3 ) {
105
- if (errors != null ) {
106
- errors . add ( " thing %s: bridge \" %s\" contains insufficient segments" . formatted ( uid , bridge ));
107
- }
99
+ addToList (errors ,
100
+ "invalid thing \" %s \" : not enough segments in value \" %s\" for \" bridge\" field; minimum 3 is expected"
101
+ . formatted ( uid , bridge ));
108
102
ok = false ;
109
103
}
110
104
for (String segment : segments ) {
111
105
if (!THING_UID_SEGMENT_PATTERN .matcher (segment ).matches ()) {
112
- if (errors != null ) {
113
- errors .add ("thing %s: segment \" %s\" in bridge is not matching the expected syntax %s"
114
- .formatted (uid , segment , THING_UID_SEGMENT_PATTERN .pattern ()));
115
- }
106
+ addToList (errors ,
107
+ "invalid thing \" %s\" : segment \" %s\" in \" bridge\" field not matching the expected syntax %s"
108
+ .formatted (uid , segment , THING_UID_SEGMENT_PATTERN .pattern ()));
116
109
ok = false ;
117
110
}
118
111
}
@@ -121,30 +114,30 @@ public boolean isValid(@Nullable List<@NonNull String> errors, @Nullable List<@N
121
114
for (Map .Entry <@ NonNull String , @ NonNull YamlChannelDTO > entry : channels .entrySet ()) {
122
115
String channelId = entry .getKey ();
123
116
if (!CHANNEL_ID_PATTERN .matcher (channelId ).matches ()) {
124
- if (errors != null ) {
125
- errors .add ("thing %s: channel id \" %s\" is not matching the expected syntax %s" .formatted (uid ,
126
- channelId , CHANNEL_ID_PATTERN .pattern ()));
127
- }
117
+ addToList (errors , "invalid thing \" %s\" : channel id \" %s\" not matching the expected syntax %s"
118
+ .formatted (uid , channelId , CHANNEL_ID_PATTERN .pattern ()));
128
119
ok = false ;
129
120
}
130
121
List <String > channelErrors = new ArrayList <>();
131
122
List <String > channelWarnings = new ArrayList <>();
132
123
ok &= entry .getValue ().isValid (channelErrors , channelWarnings );
133
- if (errors != null ) {
134
- channelErrors .forEach (error -> {
135
- errors .add ("thing %s channel %s: %s" .formatted (uid , channelId , error ));
136
- });
137
- }
138
- if (warnings != null ) {
139
- channelWarnings .forEach (warning -> {
140
- warnings .add ("thing %s channel %s: %s" .formatted (uid , channelId , warning ));
141
- });
142
- }
124
+ channelErrors .forEach (error -> {
125
+ addToList (errors , "invalid thing \" %s\" : channel \" %s\" : %s" .formatted (uid , channelId , error ));
126
+ });
127
+ channelWarnings .forEach (warning -> {
128
+ addToList (warnings , "thing \" %s\" : channel \" %s\" : %s" .formatted (uid , channelId , warning ));
129
+ });
143
130
}
144
131
}
145
132
return ok ;
146
133
}
147
134
135
+ private void addToList (@ Nullable List <@ NonNull String > list , String value ) {
136
+ if (list != null ) {
137
+ list .add (value );
138
+ }
139
+ }
140
+
148
141
public boolean isBridge () {
149
142
return isBridge == null ? false : isBridge .booleanValue ();
150
143
}
0 commit comments