|
| 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.core.JsonProcessingException; |
| 20 | +import com.fasterxml.jackson.databind.JsonNode; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.text.MessageFormat; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | + |
| 32 | +public class OverrideValidatorTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + public void overrideDefaultValidator() throws JsonProcessingException, IOException { |
| 36 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 37 | + final String URI = "https://github.com/networknt/json-schema-validator/tests/schemas/example01"; |
| 38 | + final String schema = "{\n" + |
| 39 | + " \"$schema\":\n" + |
| 40 | + " \"https://github.com/networknt/json-schema-validator/tests/schemas/example01\",\n" + |
| 41 | + " \"properties\": {\"mailaddress\": {\"type\": \"string\", \"format\": \"email\"}}\n" + |
| 42 | + "}"; |
| 43 | + final JsonNode targetNode = objectMapper. readTree( "{\"mailaddress\": \"a-zA-Z0-9.!#$%&'*[email protected]\"}"); |
| 44 | + // Use Default EmailValidator |
| 45 | + final JsonMetaSchema validatorMetaSchema = JsonMetaSchema |
| 46 | + .builder(URI, JsonMetaSchema.getV201909()) |
| 47 | + .build(); |
| 48 | + |
| 49 | + final JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(validatorMetaSchema).build(); |
| 50 | + final JsonSchema validatorSchema = validatorFactory.getSchema(schema); |
| 51 | + |
| 52 | + Set<ValidationMessage> messages = validatorSchema.validate(targetNode); |
| 53 | + assertEquals(1, messages.size()); |
| 54 | + |
| 55 | + // Override EmailValidator |
| 56 | + final JsonMetaSchema overrideValidatorMetaSchema = JsonMetaSchema |
| 57 | + .builder(URI, JsonMetaSchema.getV201909()) |
| 58 | + .addFormat(new PatternFormat("email", "^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$")) |
| 59 | + .build(); |
| 60 | + |
| 61 | + final JsonSchemaFactory overrideValidatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(overrideValidatorMetaSchema).build(); |
| 62 | + final JsonSchema overrideValidatorSchema = overrideValidatorFactory.getSchema(schema); |
| 63 | + |
| 64 | + messages = overrideValidatorSchema.validate(targetNode); |
| 65 | + assertEquals(0, messages.size()); |
| 66 | + |
| 67 | + |
| 68 | + } |
| 69 | +} |
0 commit comments