Skip to content

Commit b35d50e

Browse files
committed
fixes issue #151
1 parent 48ca6ec commit b35d50e

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
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);
Lines changed: 63 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

0 commit comments

Comments
 (0)