Skip to content

Commit 92af629

Browse files
authored
Merge pull request #152 from networknt/issue_151
fixes issue #151
2 parents 48ca6ec + 8dd4ab5 commit 92af629

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class FormatKeyword implements Keyword {
2828

2929
private final String DATE = "date";
3030
private final String DATE_TIME = "date-time";
31+
private final String UUID = "uuid";
3132

3233
public FormatKeyword(ValidatorTypeCode type, Map<String, Format> formats) {
3334
this.type = type;
@@ -48,6 +49,8 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
4849
// Validate date and time separately
4950
if (formatName.equals(DATE) || formatName.equals(DATE_TIME)) {
5051
return new DateTimeValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName);
52+
} else if (formatName.equals(UUID)) {
53+
return new UUIDValidator(schemaPath, schemaNode, parentSchema, validationContext, formatName);
5154
}
5255
}
5356
return new FormatValidator(schemaPath, schemaNode, parentSchema, validationContext, format);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.databind.JsonNode;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import java.text.ParsePosition;
24+
import java.text.SimpleDateFormat;
25+
import java.util.Collections;
26+
import java.util.LinkedHashSet;
27+
import java.util.Set;
28+
import java.util.regex.Matcher;
29+
import java.util.regex.Pattern;
30+
31+
public class UUIDValidator extends BaseJsonValidator implements JsonValidator {
32+
private static final Logger logger = LoggerFactory.getLogger(UUIDValidator.class);
33+
34+
private final String formatName;
35+
36+
37+
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}$";
38+
39+
public UUIDValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, String formatName) {
40+
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.DATETIME, validationContext);
41+
this.formatName = formatName;
42+
parseErrorCode(getValidatorType().getErrorCodeKey());
43+
}
44+
45+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
46+
debug(logger, node, rootNode, at);
47+
48+
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
49+
50+
JsonType nodeType = TypeFactory.getValueNodeType(node);
51+
if (nodeType != JsonType.STRING) {
52+
return errors;
53+
}
54+
if (!isUUID(node.textValue())) {
55+
errors.add(buildValidationMessage(at, node.textValue(), formatName));
56+
}
57+
return Collections.unmodifiableSet(errors);
58+
}
59+
60+
public boolean isUUID(String s){
61+
return s.matches(regex);
62+
}
63+
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
6363
TYPE("type", "1029", new MessageFormat("{0}: {1} found, {2} expected")),
6464
UNION_TYPE("unionType", "1030", new MessageFormat("{0}: {1} found, but {2} is required")),
6565
UNIQUE_ITEMS("uniqueItems", "1031", new MessageFormat("{0}: the items in the array must be unique")),
66-
DATETIME("date-time", "1034", new MessageFormat("{0}: {1} is an invalid {2}"));
66+
DATETIME("date-time", "1034", new MessageFormat("{0}: {1} is an invalid {2}")),
67+
UUID("uuid", "1035", new MessageFormat("{0}: {1} is an invalid {2}"));
6768

6869
private static Map<String, ValidatorTypeCode> constants = new HashMap<String, ValidatorTypeCode>();
6970

src/test/java/com/networknt/schema/JsonSchemaTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ public void testRequiredValidator() throws Exception {
270270
runTestFile("tests/required.json");
271271
}
272272

273+
273274
@Test
274275
public void testTypeValidator() throws Exception {
275276
runTestFile("tests/type.json");
@@ -300,4 +301,10 @@ public void testSchemaFromClasspath() throws Exception {
300301
runTestFile("tests/classpath/schema.json");
301302
}
302303

304+
305+
306+
@Test
307+
public void testUUIDValidator() throws Exception {
308+
runTestFile("tests/uuid.json");
309+
}
303310
}

src/test/resources/tests/uuid.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[
2+
{
3+
"description": "uuid format validation",
4+
"schema": {"format": "uuid"},
5+
"tests": [
6+
{
7+
"description": "invalid uuid",
8+
"data": "df984f5c-59bd-48e3sac87-b03d574b37e9",
9+
"valid": false
10+
},
11+
{
12+
"description": "valid uuid string",
13+
"data": "df984f5c-59bd-48e3-ac87-b03d574b37e9",
14+
"valid": true
15+
},
16+
{
17+
"description": "too short, it is invalid",
18+
"data": "5fc03087d-d265-11e7-b8c6-83e29cd24f",
19+
"valid": false
20+
},
21+
{
22+
"description": "two long, length is greater than 36",
23+
"data": "5fc03087d-d265-11e7-b8c6-83e29cd24f42333",
24+
"valid": false
25+
}
26+
]
27+
}
28+
]

0 commit comments

Comments
 (0)