Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #627 custom message for format #632

Merged
merged 2 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions doc/cust-msg.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
This document explains how users can create their custom message for schema validation.
# Custom Message Support
Users can add their own custom messages for schema validation using the instructions in this page.

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

We can provide the custom message in the json schema itself.

<b> Example of schema with default message: </b>

## Examples
### Example 1 :
The custom message can be provided outside properties for each type, as shown in the schema below.
````json
{
"type": "object",
Expand All @@ -17,46 +18,48 @@ We can provide the custom message in the json schema itself.
"type": "array",
"maxItems": 3
}
},
"message": {
"maxItems" : "MaxItem must be 3 only",
"type" : "Invalid type"
}
}
````


<b> Example of schema with a custom message: </b>

### Example 2 :
To keep custom messages distinct for each type, one can even give them in each property.
````json
{
"type": "object",
"properties": {
"firstName": {
"dateTime": {
"type": "string",
"description": "The person's first name."
"format": "date",
"message": {
"format": "Keep date format yyyy-mm-dd"
}
},
"foo": {
"type": "array",
"maxItems": 3
"uuid": {
"type": "string",
"format": "uuid",
"message": {
"format": "Input should be uuid"
}
}
},
"message": {
"maxItems" : "MaxItem must be 3 only",
"type" : "Invalid type"
}
}
````



## Format
````json
"message": {
[validationType] : [customMessage]
}
````
Users can express custom message in the **'message'** field.
The **'validation type'** should be the key and the **'custom message'** should be the value.

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>.


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>
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.**



Take a look at the [PR](https://github.com/networknt/json-schema-validator/pull/438)
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)
4 changes: 2 additions & 2 deletions src/main/java/com/networknt/schema/DateTimeValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class DateTimeValidator extends BaseJsonValidator implements JsonValidato
private final String DATE = "date";
private final String DATETIME = "date-time";

public DateTimeValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.DATETIME, validationContext);
public DateTimeValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName, ValidatorTypeCode type) {
super(schemaPath, schemaNode, parentSchema, type, validationContext);
this.formatName = formatName;
this.validationContext = validationContext;
parseErrorCode(getValidatorType().getErrorCodeKey());
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/com/networknt/schema/FormatKeyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,27 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
format = formats.get(formatName);
// if you set custom format, override default Email/DateTime/UUID Validator
if (format != null) {
return new FormatValidator(schemaPath, schemaNode, parentSchema, validationContext, format);
return new FormatValidator(schemaPath, schemaNode, parentSchema, validationContext, format, type);
}
// Validate date and time separately
if (formatName.equals(DATE) || formatName.equals(DATE_TIME)) {
return new DateTimeValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName);
ValidatorTypeCode typeCode = ValidatorTypeCode.DATETIME;
// Set custom error message
typeCode.setCustomMessage(type.getCustomMessage());
return new DateTimeValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName, typeCode);
} else if (formatName.equals(UUID)) {
return new UUIDValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName);
ValidatorTypeCode typeCode = ValidatorTypeCode.UUID;
// Set custom error message
typeCode.setCustomMessage(type.getCustomMessage());
return new UUIDValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName, typeCode);
} else if (formatName.equals(EMAIL)) {
return new EmailValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName);
return new EmailValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName, type);
}
else if (formatName.equals(DURATION)) {
return new DurationValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName);
return new DurationValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName, type);
}
}
return new FormatValidator(schemaPath, schemaNode, parentSchema, validationContext, format);
return new FormatValidator(schemaPath, schemaNode, parentSchema, validationContext, format, type);
}

@Override
Expand All @@ -78,6 +84,4 @@ public String getValue() {
public void setCustomMessage(String message) {
type.setCustomMessage(message);
}


}
4 changes: 2 additions & 2 deletions src/main/java/com/networknt/schema/FormatValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class FormatValidator extends BaseJsonValidator implements JsonValidator

private final Format format;

public FormatValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, Format format) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.FORMAT, validationContext);
public FormatValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, Format format, ValidatorTypeCode type) {
super(schemaPath, schemaNode, parentSchema, type, validationContext);
this.format = format;
this.validationContext = validationContext;
parseErrorCode(getValidatorType().getErrorCodeKey());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/networknt/schema/UUIDValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class UUIDValidator extends BaseJsonValidator implements JsonValidator {

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}$";

public UUIDValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.UUID, validationContext);
public UUIDValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName, ValidatorTypeCode type) {
super(schemaPath, schemaNode, parentSchema, type, validationContext);
this.formatName = formatName;
parseErrorCode(getValidatorType().getErrorCodeKey());
this.validationContext = validationContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public boolean isValid(String duration) {
return true;
}

public DurationValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.FORMAT, validationContext);
public DurationValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName, ValidatorTypeCode type) {
super(schemaPath, schemaNode, parentSchema, type, validationContext);
this.formatName = formatName;
this.validationContext = validationContext;
parseErrorCode(getValidatorType().getErrorCodeKey());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/networknt/schema/format/EmailValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ protected boolean isValidUser(String user) {
return USER_PATTERN.matcher(user).matches();
}

public EmailValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.FORMAT, validationContext);
public EmailValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName, ValidatorTypeCode type) {
super(schemaPath, schemaNode, parentSchema, type, validationContext);
this.formatName = formatName;
this.validationContext = validationContext;
parseErrorCode(getValidatorType().getErrorCodeKey());
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/networknt/schema/Issue627Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.networknt.schema;public class Issue627Test {
}
5 changes: 5 additions & 0 deletions src/test/resources/data/issue627.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dateTime": "2022-07-33",
"email": "inValidEmail",
"uuid": "inValidUUID"
}
20 changes: 20 additions & 0 deletions src/test/resources/schema/issue627-v7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"dateTime": {
"type": "string",
"format": "date",
"message": {
"format": "Keep date format yyyy-mm-dd"
}
},
"uuid": {
"type": "string",
"format": "uuid",
"message": {
"format": "Input should be uuid"
}
}
}
}