|
| 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 | +} |
0 commit comments