Skip to content

Commit 6041dd7

Browse files
authored
issue_563 Support adding custom message at attribute level and custom validator for all schema versions (#608)
1 parent cbc03f8 commit 6041dd7

9 files changed

+23
-16
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public Builder(String uri) {
104104
}
105105

106106
private static Map<String, Keyword> createKeywordsMap(Map<String, Keyword> kwords, Map<String, Format> formats) {
107-
final Map<String, Keyword> map = new HashMap<String, Keyword>();
107+
Map<String, Keyword> map = new HashMap<String, Keyword>();
108108
for (Map.Entry<String, Keyword> type : kwords.entrySet()) {
109109
String keywordName = type.getKey();
110110
Keyword keyword = type.getValue();
@@ -119,7 +119,7 @@ private static Map<String, Keyword> createKeywordsMap(Map<String, Keyword> kword
119119
}
120120
final FormatKeyword formatKeyword = new FormatKeyword(ValidatorTypeCode.FORMAT, formats);
121121
map.put(formatKeyword.getValue(), formatKeyword);
122-
return Collections.unmodifiableMap(map);
122+
return map;
123123
}
124124

125125
public Builder addKeyword(Keyword keyword) {
@@ -154,14 +154,14 @@ public Builder idKeyword(String idKeyword) {
154154

155155
public JsonMetaSchema build() {
156156
// create builtin keywords with (custom) formats.
157-
final Map<String, Keyword> kwords = createKeywordsMap(keywords, formats);
157+
Map<String, Keyword> kwords = createKeywordsMap(keywords, formats);
158158
return new JsonMetaSchema(uri, idKeyword, kwords);
159159
}
160160
}
161161

162162
private final String uri;
163163
private final String idKeyword;
164-
private final Map<String, Keyword> keywords;
164+
private Map<String, Keyword> keywords;
165165

166166
private JsonMetaSchema(String uri, String idKeyword, Map<String, Keyword> keywords) {
167167
if (StringUtils.isBlank(uri)) {
@@ -267,6 +267,9 @@ public String getUri() {
267267
return uri;
268268
}
269269

270+
public Map<String, Keyword> getKeywords() {
271+
return keywords;
272+
}
270273

271274
public JsonValidator newValidator(ValidationContext validationContext, String schemaPath, String keyword /* keyword */, JsonNode schemaNode,
272275
JsonSchema parentSchema, String customMessage) {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,19 +238,23 @@ private Map<String, JsonValidator> read(JsonNode schemaNode) {
238238

239239
private String getCustomMessage(JsonNode schemaNode, String pname) {
240240
final JsonSchema parentSchema = getParentSchema();
241-
final JsonNode message = getMessageNode(schemaNode, parentSchema);
241+
final JsonNode message = getMessageNode(schemaNode, parentSchema, pname);
242242
if (message != null && message.get(pname) != null) {
243243
return message.get(pname).asText();
244244
}
245245
return null;
246246
}
247247

248-
private JsonNode getMessageNode(JsonNode schemaNode, JsonSchema parentSchema) {
248+
private JsonNode getMessageNode(JsonNode schemaNode, JsonSchema parentSchema, String pname) {
249+
if (schemaNode.get("message") != null && schemaNode.get("message").get(pname) != null) {
250+
return schemaNode.get("message");
251+
}
249252
JsonNode nodeContainingMessage;
250-
if (parentSchema == null)
253+
if (parentSchema == null) {
251254
nodeContainingMessage = schemaNode;
252-
else
255+
} else {
253256
nodeContainingMessage = parentSchema.schemaNode;
257+
}
254258
return nodeContainingMessage.get("message");
255259
}
256260

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private JsonSchemaFactory(
223223
*
224224
* @return a builder instance without any keywords or formats - usually not what one needs.
225225
*/
226-
static Builder builder() {
226+
public static Builder builder() {
227227
return new Builder();
228228
}
229229

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
abstract class JsonSchemaVersion {
6+
public abstract class JsonSchemaVersion {
77
protected static String URI;
88
protected static String ID;
99
public static final List<Format> BUILTIN_FORMATS = new ArrayList<Format>(JsonMetaSchema.COMMON_BUILTIN_FORMATS);
1010
static {
1111
// add version specific formats here.
1212
//BUILTIN_FORMATS.add(pattern("phone", "^\\+(?:[0-9] ?){6,14}[0-9]$"));
1313
}
14-
abstract JsonMetaSchema getInstance();
14+
public abstract JsonMetaSchema getInstance();
1515
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Version201909 extends JsonSchemaVersion{
1111
//BUILTIN_FORMATS.add(pattern("phone", "^\\+(?:[0-9] ?){6,14}[0-9]$"));
1212
}
1313
@Override
14-
JsonMetaSchema getInstance() {
14+
public JsonMetaSchema getInstance() {
1515
return new JsonMetaSchema.Builder(URI)
1616
.idKeyword(ID)
1717
.addFormats(BUILTIN_FORMATS)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Version202012 extends JsonSchemaVersion {
1212
}
1313

1414
@Override
15-
JsonMetaSchema getInstance() {
15+
public JsonMetaSchema getInstance() {
1616
return new JsonMetaSchema.Builder(URI)
1717
.idKeyword(ID)
1818
.addFormats(BUILTIN_FORMATS)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Version4 extends JsonSchemaVersion{
1111
//BUILTIN_FORMATS.add(pattern("phone", "^\\+(?:[0-9] ?){6,14}[0-9]$"));
1212
}
1313

14-
JsonMetaSchema getInstance() {
14+
public JsonMetaSchema getInstance() {
1515
return new JsonMetaSchema.Builder(URI)
1616
.idKeyword(ID)
1717
.addFormats(BUILTIN_FORMATS)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Version6 extends JsonSchemaVersion{
1212
//BUILTIN_FORMATS.add(pattern("phone", "^\\+(?:[0-9] ?){6,14}[0-9]$"));
1313
}
1414

15-
JsonMetaSchema getInstance() {
15+
public JsonMetaSchema getInstance() {
1616
return new JsonMetaSchema.Builder(URI)
1717
.idKeyword(ID)
1818
.addFormats(BUILTIN_FORMATS)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Version7 extends JsonSchemaVersion{
1111
//BUILTIN_FORMATS.add(pattern("phone", "^\\+(?:[0-9] ?){6,14}[0-9]$"));
1212
}
1313
@Override
14-
JsonMetaSchema getInstance() {
14+
public JsonMetaSchema getInstance() {
1515
return new JsonMetaSchema.Builder(URI)
1616
.idKeyword(ID)
1717
.addFormats(BUILTIN_FORMATS)

0 commit comments

Comments
 (0)