xref: /aosp_15_r20/external/json-schema-validator/doc/cust-msg.md (revision 78c4dd6aa35290980cdcd1623a7e337e8d021c7c)
1# Custom Message Support
2Users can add their own custom messages for schema validation using the instructions in this page.
3
4The json schema itself has a place for the customised message.
5
6## Examples
7### Example 1 :
8The custom message can be provided outside properties for each type, as shown in the schema below.
9```json
10{
11  "type": "object",
12  "properties": {
13    "firstName": {
14      "type": "string",
15      "description": "The person's first name."
16    },
17    "foo": {
18      "type": "array",
19      "maxItems": 3
20    }
21  },
22  "message": {
23    "maxItems" : "MaxItem must be 3 only",
24    "type" : "Invalid type"
25  }
26}
27```
28### Example 2 :
29To keep custom messages distinct for each type, one can even give them in each property.
30```json
31{
32  "type": "object",
33  "properties": {
34    "dateTime": {
35      "type": "string",
36      "format": "date",
37      "message": {
38        "format": "Keep date format yyyy-mm-dd"
39      }
40    },
41    "uuid": {
42      "type": "string",
43      "format": "uuid",
44      "message": {
45        "format": "Input should be uuid"
46      }
47    }
48  }
49}
50```
51### Example 3 :
52For the keywords `required` and `dependencies`, different messages can be specified for different properties.
53
54```json
55{
56  "type": "object",
57  "properties": {
58    "foo": {
59      "type": "number"
60    },
61    "bar": {
62      "type": "string"
63    }
64  },
65  "required": ["foo", "bar"],
66  "message": {
67    "type" : "should be an object",
68    "required": {
69      "foo" : "'foo' is required",
70      "bar" : "'bar' is required"
71    }
72  }
73}
74```
75### Example 4 :
76The message can use arguments but note that single quotes need to be escaped as `java.text.MessageFormat` will be used to format the message.
77
78```json
79{
80  "type": "object",
81  "properties": {
82    "foo": {
83      "type": "number"
84    },
85    "bar": {
86      "type": "string"
87    }
88  },
89  "required": ["foo", "bar"],
90  "message": {
91    "type" : "should be an object",
92    "required": {
93      "foo" : "{0}: ''foo'' is required",
94      "bar" : "{0}: ''bar'' is required"
95    }
96  }
97}
98```
99
100## Format
101```json
102"message": {
103    [validationType] : [customMessage]
104  }
105```
106Users can express custom message in the **'message'** field.
107The **'validation type'** should be the key and the **'custom message'** should be the value.
108
109Also, 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.**
110
111
112
113Take 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)
114