Skip to content

Commit 2822c69

Browse files
authored
Issue #627 custom message for format (#632)
* Issue #627 custom message for format * issue 627 updated document for custom message
1 parent 8b91c28 commit 2822c69

File tree

10 files changed

+77
-43
lines changed

10 files changed

+77
-43
lines changed

doc/cust-msg.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
This document explains how users can create their custom message for schema validation.
1+
# Custom Message Support
2+
Users can add their own custom messages for schema validation using the instructions in this page.
23

4+
The json schema itself has a place for the customised message.
35

4-
We can provide the custom message in the json schema itself.
5-
6-
<b> Example of schema with default message: </b>
7-
6+
## Examples
7+
### Example 1 :
8+
The custom message can be provided outside properties for each type, as shown in the schema below.
89
````json
910
{
1011
"type": "object",
@@ -17,46 +18,48 @@ We can provide the custom message in the json schema itself.
1718
"type": "array",
1819
"maxItems": 3
1920
}
21+
},
22+
"message": {
23+
"maxItems" : "MaxItem must be 3 only",
24+
"type" : "Invalid type"
2025
}
2126
}
2227
````
23-
24-
25-
<b> Example of schema with a custom message: </b>
26-
28+
### Example 2 :
29+
To keep custom messages distinct for each type, one can even give them in each property.
2730
````json
2831
{
2932
"type": "object",
3033
"properties": {
31-
"firstName": {
34+
"dateTime": {
3235
"type": "string",
33-
"description": "The person's first name."
36+
"format": "date",
37+
"message": {
38+
"format": "Keep date format yyyy-mm-dd"
39+
}
3440
},
35-
"foo": {
36-
"type": "array",
37-
"maxItems": 3
41+
"uuid": {
42+
"type": "string",
43+
"format": "uuid",
44+
"message": {
45+
"format": "Input should be uuid"
46+
}
3847
}
39-
},
40-
"message": {
41-
"maxItems" : "MaxItem must be 3 only",
42-
"type" : "Invalid type"
4348
}
4449
}
4550
````
4651

47-
48-
52+
## Format
4953
````json
5054
"message": {
5155
[validationType] : [customMessage]
5256
}
5357
````
58+
Users can express custom message in the **'message'** field.
59+
The **'validation type'** should be the key and the **'custom message'** should be the value.
5460

55-
In the message field users can declare their custom message. The key should be the <b>validation type</b>, and the value should be the <b>custom message</b>.
56-
57-
58-
Also, we can make format the dynamic message with properties returned from [ValidationMessage.java](https://github.com/networknt/json-schema-validator/blob/master/src/main/java/com/networknt/schema/ValidationMessage.java) class such as <b>arguments, path e.t.c.</b>
61+
Also, we can make format the dynamic message with properties returned from [ValidationMessage.java](https://github.com/networknt/json-schema-validator/blob/master/src/main/java/com/networknt/schema/ValidationMessage.java) class such as **arguments, path e.t.c.**
5962

6063

6164

62-
Take a look at the [PR](https://github.com/networknt/json-schema-validator/pull/438)
65+
Take a look at the [PR1](https://github.com/networknt/json-schema-validator/pull/438) and [PR2](https://github.com/networknt/json-schema-validator/pull/632)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class DateTimeValidator extends BaseJsonValidator implements JsonValidato
3434
private final String DATE = "date";
3535
private final String DATETIME = "date-time";
3636

37-
public DateTimeValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName) {
38-
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.DATETIME, validationContext);
37+
public DateTimeValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName, ValidatorTypeCode type) {
38+
super(schemaPath, schemaNode, parentSchema, type, validationContext);
3939
this.formatName = formatName;
4040
this.validationContext = validationContext;
4141
parseErrorCode(getValidatorType().getErrorCodeKey());

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,27 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
5252
format = formats.get(formatName);
5353
// if you set custom format, override default Email/DateTime/UUID Validator
5454
if (format != null) {
55-
return new FormatValidator(schemaPath, schemaNode, parentSchema, validationContext, format);
55+
return new FormatValidator(schemaPath, schemaNode, parentSchema, validationContext, format, type);
5656
}
5757
// Validate date and time separately
5858
if (formatName.equals(DATE) || formatName.equals(DATE_TIME)) {
59-
return new DateTimeValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName);
59+
ValidatorTypeCode typeCode = ValidatorTypeCode.DATETIME;
60+
// Set custom error message
61+
typeCode.setCustomMessage(type.getCustomMessage());
62+
return new DateTimeValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName, typeCode);
6063
} else if (formatName.equals(UUID)) {
61-
return new UUIDValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName);
64+
ValidatorTypeCode typeCode = ValidatorTypeCode.UUID;
65+
// Set custom error message
66+
typeCode.setCustomMessage(type.getCustomMessage());
67+
return new UUIDValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName, typeCode);
6268
} else if (formatName.equals(EMAIL)) {
63-
return new EmailValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName);
69+
return new EmailValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName, type);
6470
}
6571
else if (formatName.equals(DURATION)) {
66-
return new DurationValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName);
72+
return new DurationValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName, type);
6773
}
6874
}
69-
return new FormatValidator(schemaPath, schemaNode, parentSchema, validationContext, format);
75+
return new FormatValidator(schemaPath, schemaNode, parentSchema, validationContext, format, type);
7076
}
7177

7278
@Override
@@ -78,6 +84,4 @@ public String getValue() {
7884
public void setCustomMessage(String message) {
7985
type.setCustomMessage(message);
8086
}
81-
82-
8387
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public class FormatValidator extends BaseJsonValidator implements JsonValidator
3030

3131
private final Format format;
3232

33-
public FormatValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, Format format) {
34-
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.FORMAT, validationContext);
33+
public FormatValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, Format format, ValidatorTypeCode type) {
34+
super(schemaPath, schemaNode, parentSchema, type, validationContext);
3535
this.format = format;
3636
this.validationContext = validationContext;
3737
parseErrorCode(getValidatorType().getErrorCodeKey());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public class UUIDValidator extends BaseJsonValidator implements JsonValidator {
3232

3333
private static final String regex = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
3434

35-
public UUIDValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName) {
36-
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.UUID, validationContext);
35+
public UUIDValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName, ValidatorTypeCode type) {
36+
super(schemaPath, schemaNode, parentSchema, type, validationContext);
3737
this.formatName = formatName;
3838
parseErrorCode(getValidatorType().getErrorCodeKey());
3939
this.validationContext = validationContext;

src/main/java/com/networknt/schema/format/DurationValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public boolean isValid(String duration) {
6363
return true;
6464
}
6565

66-
public DurationValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName) {
67-
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.FORMAT, validationContext);
66+
public DurationValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName, ValidatorTypeCode type) {
67+
super(schemaPath, schemaNode, parentSchema, type, validationContext);
6868
this.formatName = formatName;
6969
this.validationContext = validationContext;
7070
parseErrorCode(getValidatorType().getErrorCodeKey());

src/main/java/com/networknt/schema/format/EmailValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ protected boolean isValidUser(String user) {
137137
return USER_PATTERN.matcher(user).matches();
138138
}
139139

140-
public EmailValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName) {
141-
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.FORMAT, validationContext);
140+
public EmailValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName, ValidatorTypeCode type) {
141+
super(schemaPath, schemaNode, parentSchema, type, validationContext);
142142
this.formatName = formatName;
143143
this.validationContext = validationContext;
144144
parseErrorCode(getValidatorType().getErrorCodeKey());
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package com.networknt.schema;public class Issue627Test {
2+
}

src/test/resources/data/issue627.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dateTime": "2022-07-33",
3+
"email": "inValidEmail",
4+
"uuid": "inValidUUID"
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"properties": {
5+
"dateTime": {
6+
"type": "string",
7+
"format": "date",
8+
"message": {
9+
"format": "Keep date format yyyy-mm-dd"
10+
}
11+
},
12+
"uuid": {
13+
"type": "string",
14+
"format": "uuid",
15+
"message": {
16+
"format": "Input should be uuid"
17+
}
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)