Skip to content

Commit 9d6d94e

Browse files
committed
-Revert those files are only changed imports by IDE
-clean up a legacy constructor
1 parent 27eaa02 commit 9d6d94e

File tree

8 files changed

+94
-91
lines changed

8 files changed

+94
-91
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.networknt.schema;
22

3-
import com.fasterxml.jackson.databind.JsonNode;
4-
53
import java.util.Collection;
64
import java.util.Collections;
75
import java.util.Map;
86

7+
import com.fasterxml.jackson.databind.JsonNode;
8+
99
public class FormatKeyword implements Keyword {
1010
private final ValidatorTypeCode type;
1111
private final Map<String, Format> formats;

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@
1616

1717
package com.networknt.schema;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
19+
import java.lang.reflect.InvocationTargetException;
20+
import java.util.ArrayList;
21+
import java.util.Arrays;
22+
import java.util.Collection;
23+
import java.util.Collections;
24+
import java.util.HashMap;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.concurrent.ConcurrentHashMap;
28+
2029
import org.apache.commons.lang3.StringUtils;
2130
import org.slf4j.Logger;
2231
import org.slf4j.LoggerFactory;
2332

24-
import java.lang.reflect.InvocationTargetException;
25-
import java.util.*;
26-
import java.util.concurrent.ConcurrentHashMap;
33+
import com.fasterxml.jackson.databind.JsonNode;
2734

2835
public class JsonMetaSchema {
2936

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ public class JsonSchema extends BaseJsonValidator {
3434
protected final Map<String, JsonValidator> validators;
3535
private final ValidationContext validationContext;
3636

37-
3837
public JsonSchema(ValidationContext validationContext, JsonNode schemaNode) {
39-
this(validationContext, schemaNode, new HashMap<String, Object>());
40-
}
41-
42-
public JsonSchema(ValidationContext validationContext, JsonNode schemaNode, Map<String, Object> option) {
4338
this(validationContext, "#", schemaNode, null);
4439
}
4540

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public JsonSchema getSchema(URL schemaURL, ValidatorConfig config) {
201201
return new JsonSchema(new ValidationContext(jsonMetaSchema, this), schemaNode, true /*retrieved via id, resolving will not change anything*/);
202202
}
203203

204-
return newJsonSchema(schemaNode, null);
204+
return newJsonSchema(schemaNode, config);
205205
} finally {
206206
if (inputStream != null) {
207207
inputStream.close();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.networknt.schema;
22

3-
import com.fasterxml.jackson.databind.JsonNode;
4-
53
import java.util.Collections;
64
import java.util.Set;
75

6+
import com.fasterxml.jackson.databind.JsonNode;
7+
88
/**
99
* Used for Keywords that have no validation aspect, but are part of the metaschema.
1010
*/
@@ -27,7 +27,7 @@ public NonValidationKeyword(String keyword) {
2727

2828
@Override
2929
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
30-
ValidationContext validationContext) throws JsonSchemaException, Exception {
30+
ValidationContext validationContext) throws JsonSchemaException, Exception {
3131
return new Validator(getValue());
3232
}
3333
}

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

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@
1616

1717
package com.networknt.schema;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.networknt.schema.url.URLFactory;
21-
import org.slf4j.Logger;
22-
import org.slf4j.LoggerFactory;
23-
2419
import java.io.InputStream;
2520
import java.net.MalformedURLException;
2621
import java.net.URL;
2722
import java.text.MessageFormat;
2823
import java.util.Collections;
2924
import java.util.Set;
3025

26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
import com.fasterxml.jackson.databind.JsonNode;
30+
import com.networknt.schema.url.URLFactory;
31+
3132
public class RefValidator extends BaseJsonValidator implements JsonValidator {
3233
private static final Logger logger = LoggerFactory.getLogger(RefValidator.class);
3334

3435
protected JsonSchema schema;
35-
36+
3637
private static final String REF_DOMAIN = "/";
3738
private static final String REF_CURRENT = "#";
3839
private static final String REF_RELATIVE = "../";
@@ -51,14 +52,14 @@ static JsonSchema getRefSchema(JsonSchema parentSchema, ValidationContext valida
5152
if (!refValue.startsWith(REF_CURRENT)) {
5253
// handle remote ref
5354
String schemaUrl = refValue;
54-
int index = refValue.indexOf(REF_CURRENT);
55+
int index = refValue.indexOf(REF_CURRENT);
5556
if (index > 0) {
5657
schemaUrl = schemaUrl.substring(0, index);
5758
}
5859
if(isRelativePath(schemaUrl)){
5960
schemaUrl = obtainAbsolutePath(parentSchema, schemaUrl);
6061
}
61-
62+
6263
try {
6364
URL url = URLFactory.toURL(schemaUrl);
6465
parentSchema = validationContext.getJsonSchemaFactory().getSchema(url);
@@ -82,36 +83,36 @@ static JsonSchema getRefSchema(JsonSchema parentSchema, ValidationContext valida
8283
}
8384
return null;
8485
}
85-
86+
8687
private static boolean isRelativePath(String schemaUrl) {
87-
return !schemaUrl.startsWith("http");
88+
return !schemaUrl.startsWith("http");
8889
}
89-
90+
9091
private static String obtainAbsolutePath(JsonSchema parentSchema, String schemaUrl) {
91-
String baseSchemaUrl = parentSchema.findAncestor().getSchemaNode().get("id").textValue();
92-
int index = baseSchemaUrl.lastIndexOf("/");
93-
baseSchemaUrl = baseSchemaUrl.substring(0, index);
94-
95-
String schemaRef = schemaUrl;
96-
97-
if(schemaRef.startsWith(REF_DOMAIN)){
98-
// from domain add ref
99-
try {
100-
URL url = URLFactory.toURL(baseSchemaUrl);
101-
baseSchemaUrl = url.getProtocol()+"//"+url.getHost();
102-
} catch (MalformedURLException e) {
103-
e.printStackTrace();
104-
}
105-
}else if(schemaRef.startsWith(REF_RELATIVE)){
106-
// relative from schema
107-
while(schemaRef.startsWith(REF_RELATIVE)){
108-
index = baseSchemaUrl.lastIndexOf("/");
109-
baseSchemaUrl = baseSchemaUrl.substring(0, index);
110-
schemaRef = schemaRef.replaceFirst(REF_RELATIVE, "");
111-
}
112-
}
113-
schemaRef = baseSchemaUrl +"/"+ schemaRef;
114-
return schemaRef;
92+
String baseSchemaUrl = parentSchema.findAncestor().getSchemaNode().get("id").textValue();
93+
int index = baseSchemaUrl.lastIndexOf("/");
94+
baseSchemaUrl = baseSchemaUrl.substring(0, index);
95+
96+
String schemaRef = schemaUrl;
97+
98+
if(schemaRef.startsWith(REF_DOMAIN)){
99+
// from domain add ref
100+
try {
101+
URL url = URLFactory.toURL(baseSchemaUrl);
102+
baseSchemaUrl = url.getProtocol()+"//"+url.getHost();
103+
} catch (MalformedURLException e) {
104+
e.printStackTrace();
105+
}
106+
}else if(schemaRef.startsWith(REF_RELATIVE)){
107+
// relative from schema
108+
while(schemaRef.startsWith(REF_RELATIVE)){
109+
index = baseSchemaUrl.lastIndexOf("/");
110+
baseSchemaUrl = baseSchemaUrl.substring(0, index);
111+
schemaRef = schemaRef.replaceFirst(REF_RELATIVE, "");
112+
}
113+
}
114+
schemaRef = baseSchemaUrl +"/"+ schemaRef;
115+
return schemaRef;
115116
}
116117

117118
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
package com.networknt.schema;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
20-
2119
import java.lang.reflect.Constructor;
2220
import java.text.MessageFormat;
2321
import java.util.ArrayList;
2422
import java.util.HashMap;
2523
import java.util.List;
2624
import java.util.Map;
2725

26+
import com.fasterxml.jackson.databind.JsonNode;
27+
2828
public enum ValidatorTypeCode implements Keyword, ErrorMessageType {
2929
ADDITIONAL_PROPERTIES("additionalProperties", "1001", new MessageFormat(
3030
"{0}.{1}: is not defined in the schema and the schema does not allow additional properties")),
@@ -104,21 +104,21 @@ public static ValidatorTypeCode fromValue(String value) {
104104
}
105105

106106
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) throws Exception {
107-
String shortClassName = getValue();
108-
if (shortClassName.startsWith("$")) {
109-
// remove "$" from class name for $ref schema
110-
shortClassName = shortClassName.substring(1);
111-
}
107+
String shortClassName = getValue();
108+
if (shortClassName.startsWith("$")) {
109+
// remove "$" from class name for $ref schema
110+
shortClassName = shortClassName.substring(1);
111+
}
112112

113-
final String className = Character.toUpperCase(shortClassName.charAt(0)) + shortClassName.substring(1)
114-
+ "Validator";
115-
@SuppressWarnings("unchecked")
116-
final Class<JsonValidator> clazz = (Class<JsonValidator>) Class
117-
.forName("com.networknt.schema." + className);
118-
Constructor<JsonValidator> c = null;
119-
c = clazz.getConstructor(
120-
new Class[] { String.class, JsonNode.class, JsonSchema.class, ValidationContext.class });
121-
return c.newInstance(schemaPath + "/" + getValue(), schemaNode, parentSchema, validationContext);
113+
final String className = Character.toUpperCase(shortClassName.charAt(0)) + shortClassName.substring(1)
114+
+ "Validator";
115+
@SuppressWarnings("unchecked")
116+
final Class<JsonValidator> clazz = (Class<JsonValidator>) Class
117+
.forName("com.networknt.schema." + className);
118+
Constructor<JsonValidator> c = null;
119+
c = clazz.getConstructor(
120+
new Class[] { String.class, JsonNode.class, JsonSchema.class, ValidationContext.class });
121+
return c.newInstance(schemaPath + "/" + getValue(), schemaNode, parentSchema, validationContext);
122122
}
123123

124124
@Override

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
package com.networknt.schema;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.JsonNode;
5-
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import org.junit.Test;
7-
83
import java.io.IOException;
94
import java.text.MessageFormat;
105
import java.util.ArrayList;
116
import java.util.List;
127
import java.util.Set;
138

14-
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.*;
10+
import org.junit.Test;
11+
12+
import com.fasterxml.jackson.core.JsonProcessingException;
13+
import com.fasterxml.jackson.databind.JsonNode;
14+
import com.fasterxml.jackson.databind.ObjectMapper;
1515

1616
public class CustomMetaSchemaTest {
1717

1818
/**
1919
* Introduces the keyword "enumNames".
20-
*
20+
*
2121
* The keyword is used together with "enum" and must have the same length.
22-
*
23-
* This keyword always produces a warning during validation -
24-
* so it makes no sense in reality but should be useful for demonstration / testing purposes.
25-
*
22+
*
23+
* This keyword always produces a warning during validation -
24+
* so it makes no sense in reality but should be useful for demonstration / testing purposes.
25+
*
2626
* @author klaskalass
2727
*
2828
*/
@@ -31,7 +31,7 @@ public static class EnumNamesKeyword extends AbstractKeyword {
3131
private static final class Validator extends AbstractJsonValidator {
3232
private final List<String> enumValues;
3333
private final List<String> enumNames;
34-
34+
3535
private Validator(String keyword, List<String> enumValues, List<String> enumNames) {
3636
super(keyword);
3737
if (enumNames.size() != enumValues.size()) {
@@ -53,14 +53,14 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
5353
}
5454
}
5555

56-
56+
5757
public EnumNamesKeyword() {
5858
super("enumNames");
5959
}
60-
60+
6161
@Override
6262
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
63-
ValidationContext validationContext) throws JsonSchemaException, Exception {
63+
ValidationContext validationContext) throws JsonSchemaException, Exception {
6464
/*
6565
* You can access the schema node here to read data from your keyword
6666
*/
@@ -72,10 +72,10 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
7272
throw new JsonSchemaException("Keyword enumNames needs to have a sibling enum keyword");
7373
}
7474
JsonNode enumSchemaNode = parentSchemaNode.get("enum");
75-
75+
7676
return new Validator(getValue(), readStringList(enumSchemaNode), readStringList(schemaNode));
7777
}
78-
78+
7979
private List<String> readStringList(JsonNode node) {
8080
if (!node.isArray()) {
8181
throw new JsonSchemaException("Keyword enum needs to receive an array");
@@ -96,18 +96,18 @@ public void customMetaSchemaWithIgnoredKeyword() throws JsonProcessingException,
9696
// Generated UI uses enumNames to render Labels for enum values
9797
.addKeyword(new EnumNamesKeyword())
9898
.build();
99-
99+
100100
final JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance()).addMetaSchema(metaSchema).build();
101-
final JsonSchema schema = validatorFactory.getSchema("{\n" +
102-
" \"$schema\":\n" +
103-
" \"https://github.com/networknt/json-schema-validator/tests/schemas/example01#\",\n" +
104-
" \"enum\": [\"foo\", \"bar\"],\n" +
105-
" \"enumNames\": [\"Foo !\", \"Bar !\"]\n" +
101+
final JsonSchema schema = validatorFactory.getSchema("{\n" +
102+
" \"$schema\":\n" +
103+
" \"https://github.com/networknt/json-schema-validator/tests/schemas/example01#\",\n" +
104+
" \"enum\": [\"foo\", \"bar\"],\n" +
105+
" \"enumNames\": [\"Foo !\", \"Bar !\"]\n" +
106106
"}");
107-
107+
108108
Set<ValidationMessage> messages = schema.validate(objectMapper.readTree("\"foo\""));
109109
assertEquals(1, messages.size());
110-
110+
111111
ValidationMessage message = messages.iterator().next();
112112
assertEquals("$: enumName is Foo !", message.getMessage());
113113
}

0 commit comments

Comments
 (0)