Skip to content

Commit 9df611d

Browse files
EivysesEivydas Senkus
andauthored
PropertyNames to return validator value on error (#375) (#376)
* Fix for issue-375 Co-authored-by: Eivydas Senkus <[email protected]>
1 parent ff0a6fd commit 9df611d

File tree

4 files changed

+109
-7
lines changed

4 files changed

+109
-7
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public PropertyNamesValidator(String schemaPath, JsonNode schemaNode, JsonSchema
3535
for (Iterator<String> it = schemaNode.fieldNames(); it.hasNext(); ) {
3636
String pname = it.next();
3737
schemas.put(pname, new JsonSchema(validationContext, schemaPath + "/" + pname, parentSchema.getCurrentUri(), schemaNode.get(pname), parentSchema)
38-
.initialize());
38+
.initialize());
3939
}
4040
}
4141
}
@@ -53,14 +53,17 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
5353
}
5454
for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) {
5555
String pname = it.next();
56-
if("maxLength".equals(entry.getKey()) && pname.length() > entry.getValue().getSchemaNode().intValue()) {
57-
errors.add(buildValidationMessage(at + "." + pname, "maxLength"));
56+
int maxLength = entry.getValue().getSchemaNode().intValue();
57+
if("maxLength".equals(entry.getKey()) && pname.length() > maxLength) {
58+
errors.add(buildValidationMessage(at + "." + pname, "maxLength " + maxLength));
5859
}
59-
if("minLength".equals(entry.getKey()) && pname.length() < entry.getValue().getSchemaNode().intValue()) {
60-
errors.add(buildValidationMessage(at + "." + pname, "minLength"));
60+
int minLength = entry.getValue().getSchemaNode().intValue();
61+
if("minLength".equals(entry.getKey()) && pname.length() < minLength) {
62+
errors.add(buildValidationMessage(at + "." + pname, "minLength " + minLength));
6163
}
62-
if("pattern".equals(entry.getKey()) && !Pattern.matches(entry.getValue().getSchemaNode().textValue(),pname)) {
63-
errors.add(buildValidationMessage(at + "." + pname, "pattern"));
64+
String pattern = entry.getValue().getSchemaNode().textValue();
65+
if("pattern".equals(entry.getKey()) && !Pattern.matches(pattern,pname)) {
66+
errors.add(buildValidationMessage(at + "." + pname, "pattern " + pattern));
6467
}
6568
}
6669
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2020 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+
package com.networknt.schema;
17+
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.networknt.schema.SpecVersion.VersionFlag;
21+
import java.io.InputStream;
22+
import java.util.ArrayList;
23+
import java.util.Arrays;
24+
import java.util.List;
25+
import java.util.Set;
26+
import org.hamcrest.MatcherAssert;
27+
import org.hamcrest.Matchers;
28+
import org.junit.Test;
29+
30+
public class Issue375Test {
31+
protected JsonSchema getJsonSchemaFromStreamContent(InputStream schemaContent) {
32+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V201909);
33+
return factory.getSchema(schemaContent);
34+
}
35+
36+
protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
37+
ObjectMapper mapper = new ObjectMapper();
38+
return mapper.readTree(content);
39+
}
40+
41+
@Test
42+
public void shouldFailAndShowValidationValuesWithError() throws Exception {
43+
String schemaPath = "/draft2019-09/issue375.json";
44+
String dataPath = "/data/issue375.json";
45+
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
46+
JsonSchema schema = getJsonSchemaFromStreamContent(schemaInputStream);
47+
InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
48+
JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
49+
Set<ValidationMessage> errors = schema.validate(node);
50+
List<String> errorMessages = new ArrayList<String>();
51+
for (ValidationMessage error: errors) {
52+
errorMessages.add(error.getMessage());
53+
}
54+
55+
List<String> expectedMessages = Arrays.asList(
56+
"Property name $.fields.longName123 is not valid for validation: maxLength 5",
57+
"Property name $.fields.longName123 is not valid for validation: pattern ^[a-zA-Z]+$",
58+
"Property name $.fields.a is not valid for validation: minLength 3");
59+
MatcherAssert.assertThat(errorMessages, Matchers.containsInAnyOrder(expectedMessages.toArray()));
60+
}
61+
}

src/test/resources/data/issue375.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"fields": {
3+
"longName123": {
4+
"action": "doStuff"
5+
},
6+
"a": {
7+
"action": "doMagic"
8+
}
9+
}
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2019-09/schema#",
3+
"title": "test",
4+
"description": "asdasdint",
5+
"type": "object",
6+
"properties": {
7+
"fields": {
8+
"type": "object",
9+
"propertyNames": {
10+
"pattern": "^[a-zA-Z]+$",
11+
"maxLength": 5,
12+
"minLength": 3
13+
},
14+
"additionalProperties": {
15+
"type": "object",
16+
"properties": {
17+
"action": {
18+
"type": "string"
19+
}
20+
},
21+
"additionalProperties": false,
22+
"required": [
23+
"action"
24+
]
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)