@@ -27,7 +27,7 @@ Add the following to your `pom.xml`:
27
27
<dependency >
28
28
<groupId >org.everit.json</groupId >
29
29
<artifactId >org.everit.json.schema</artifactId >
30
- <version >1.1.1 </version >
30
+ <version >1.2.0 </version >
31
31
</dependency >
32
32
```
33
33
@@ -136,3 +136,58 @@ This will print the following output:
136
136
#/rectangle/b: expected type: Number, found: String
137
137
```
138
138
139
+
140
+
141
+ Format validators
142
+ -----------------
143
+
144
+ Starting from version ` 1.2.0 ` the library supports the ` "format" ` keyword (which is an optional part of the specification),
145
+ so you can use the following formats in the schemas:
146
+
147
+ * date-time
148
+ * email
149
+ * hostname
150
+ * ipv4
151
+ * ipv6
152
+ * uri
153
+
154
+ The library also supports adding custom format validators. To use a custom validator basically you have to
155
+
156
+ * create your own validation in a class implementing the ` org.everit.json.schema.FormatValidator ` interface
157
+ * bind your validator to a name in a ` org.everit.json.schema.loader.SchemaLoader.SchemaLoaderBuilder ` instance before loading the actual schema
158
+
159
+ Example
160
+ -------
161
+
162
+
163
+ Lets assume the task is to create a custom validator which accepts strings with an even number of characters.
164
+
165
+ The custom ` FormatValidator ` will look something like this:
166
+
167
+ ``` java
168
+ public class EvenCharNumValidator implements FormatValidator {
169
+
170
+ @Override
171
+ public Optional<String > validate (final String subject ) {
172
+ if (subject. length() % 2 == 0 ) {
173
+ return Optional . empty();
174
+ } else {
175
+ return Optional . of(String . format(" the length of srtring [%s] is odd" , subject));
176
+ }
177
+ }
178
+
179
+ }
180
+ ```
181
+
182
+ To bind the ` EvenCharNumValidator ` to a ` "format" ` value (for example ` "evenlength" ` ) you have to bind a validator instance
183
+ to the keyword in the schema loader configuration:
184
+
185
+ ``` java
186
+ JSONObject rawSchema = new JSONObject (new JSONTokener (inputStream));
187
+ SchemaLoader schemaLoader = SchemaLoader . builder()
188
+ .schemaJson(rawSchema) // rawSchema is the JSON representation of the schema utilizing the "evenlength" non-standard format
189
+ .addFormatValidator(" evenlength" , new EvenCharNumValidator ()) // the EvenCharNumValidator gets bound to the "evenlength" keyword
190
+ .build();
191
+ Schema schema = schemaLoader. load(). build(); // the schema is created using the above created configuration
192
+ schema. validate(jsonDcoument); // the document validation happens here
193
+ ```
0 commit comments