Skip to content

Commit afcd64e

Browse files
authored
override default EmailValidator, if set custom email format (#391)
* override default EmailValidator, if you set custom email format * can also be overridden by UUID, date, date-time validator * add test and doc
1 parent 26b28b1 commit afcd64e

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

doc/validators.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,27 @@ You can use GroovyKeyword like below:
8080
}
8181
}
8282
````
83+
84+
### Override Email/UUID/DateTime Validator
85+
86+
In this library, if the format keyword is "email", "uuid", "date", "date-time", default validator provided by the library will be used.
87+
88+
If you want to override this behaivor, do as belows.
89+
90+
```
91+
public JsonSchemaFactory mySchemaFactory() {
92+
// base on JsonMetaSchema.V201909 copy code below
93+
String URI = "https://json-schema.org/draft/2019-09/schema";
94+
String ID = "$id";
95+
96+
JsonMetaSchema overrideEmailValidatorMetaSchema = new JsonMetaSchema.Builder(URI)
97+
.idKeyword(ID)
98+
// Override EmailValidator
99+
.addFormat(new PatternFormat("email", "^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"))
100+
.build();
101+
102+
return new JsonSchemaFactory.Builder().defaultMetaSchemaURI(overrideEmailValidatorMetaSchema.getUri())
103+
.addMetaSchema(overrideEmailValidatorMetaSchema)
104+
.build();
105+
}
106+
```

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
4848
if (schemaNode != null && schemaNode.isTextual()) {
4949
String formatName = schemaNode.textValue();
5050
format = formats.get(formatName);
51+
// if you set custom format, override default Email/DateTime/UUID Validator
52+
if (format != null) {
53+
return new FormatValidator(schemaPath, schemaNode, parentSchema, validationContext, format);
54+
}
5155
// Validate date and time separately
5256
if (formatName.equals(DATE) || formatName.equals(DATE_TIME)) {
5357
return new DateTimeValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2016 Network New Technologies Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.networknt.schema;
18+
19+
import com.fasterxml.jackson.core.JsonProcessingException;
20+
import com.fasterxml.jackson.databind.JsonNode;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import org.junit.Test;
23+
24+
import java.io.IOException;
25+
import java.text.MessageFormat;
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
import java.util.Set;
29+
30+
import static org.junit.Assert.assertEquals;
31+
32+
public class OverrideValidatorTest {
33+
34+
@Test
35+
public void overrideDefaultValidator() throws JsonProcessingException, IOException {
36+
ObjectMapper objectMapper = new ObjectMapper();
37+
final String URI = "https://github.com/networknt/json-schema-validator/tests/schemas/example01";
38+
final String schema = "{\n" +
39+
" \"$schema\":\n" +
40+
" \"https://github.com/networknt/json-schema-validator/tests/schemas/example01\",\n" +
41+
" \"properties\": {\"mailaddress\": {\"type\": \"string\", \"format\": \"email\"}}\n" +
42+
"}";
43+
final JsonNode targetNode = objectMapper.readTree("{\"mailaddress\": \"a-zA-Z0-9.!#$%&'*[email protected]\"}");
44+
// Use Default EmailValidator
45+
final JsonMetaSchema validatorMetaSchema = JsonMetaSchema
46+
.builder(URI, JsonMetaSchema.getV201909())
47+
.build();
48+
49+
final JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(validatorMetaSchema).build();
50+
final JsonSchema validatorSchema = validatorFactory.getSchema(schema);
51+
52+
Set<ValidationMessage> messages = validatorSchema.validate(targetNode);
53+
assertEquals(1, messages.size());
54+
55+
// Override EmailValidator
56+
final JsonMetaSchema overrideValidatorMetaSchema = JsonMetaSchema
57+
.builder(URI, JsonMetaSchema.getV201909())
58+
.addFormat(new PatternFormat("email", "^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"))
59+
.build();
60+
61+
final JsonSchemaFactory overrideValidatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(overrideValidatorMetaSchema).build();
62+
final JsonSchema overrideValidatorSchema = overrideValidatorFactory.getSchema(schema);
63+
64+
messages = overrideValidatorSchema.validate(targetNode);
65+
assertEquals(0, messages.size());
66+
67+
68+
}
69+
}

0 commit comments

Comments
 (0)