|
| 1 | +package com.networknt.schema; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.text.MessageFormat; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Set; |
| 8 | + |
| 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; |
| 15 | + |
| 16 | +public class CustomMetaSchemaTest { |
| 17 | + |
| 18 | + /** |
| 19 | + * Introduces the keyword "enumNames". |
| 20 | + * |
| 21 | + * 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 | + * |
| 26 | + * @author klaskalass |
| 27 | + * |
| 28 | + */ |
| 29 | + public static class EnumNamesKeyword extends AbstractKeyword { |
| 30 | + |
| 31 | + private static final class Validator extends AbstractJsonValidator { |
| 32 | + private final List<String> enumValues; |
| 33 | + private final List<String> enumNames; |
| 34 | + |
| 35 | + private Validator(String keyword, List<String> enumValues, List<String> enumNames) { |
| 36 | + super(keyword); |
| 37 | + if (enumNames.size() != enumValues.size()) { |
| 38 | + throw new IllegalArgumentException("enum and enumNames need to be of same length"); |
| 39 | + } |
| 40 | + this.enumNames = enumNames; |
| 41 | + this.enumValues = enumValues; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { |
| 46 | + String value = node.asText(); |
| 47 | + int idx = enumValues.indexOf(value); |
| 48 | + if (idx < 0) { |
| 49 | + throw new IllegalArgumentException("value not found in enum. value: " + value + " enum: " + enumValues); |
| 50 | + } |
| 51 | + String valueName = enumNames.get(idx); |
| 52 | + return fail(CustomErrorMessageType.of("tests.example.enumNames", new MessageFormat("{0}: enumName is {1}")), at, valueName); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + public EnumNamesKeyword() { |
| 58 | + super("enumNames"); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, |
| 63 | + ValidationContext validationContext) throws JsonSchemaException, Exception { |
| 64 | + /* |
| 65 | + * You can access the schema node here to read data from your keyword |
| 66 | + */ |
| 67 | + if (!schemaNode.isArray()) { |
| 68 | + throw new JsonSchemaException("Keyword enumNames needs to receive an array"); |
| 69 | + } |
| 70 | + JsonNode parentSchemaNode = parentSchema.getSchemaNode(); |
| 71 | + if (!parentSchemaNode.has("enum")) { |
| 72 | + throw new JsonSchemaException("Keyword enumNames needs to have a sibling enum keyword"); |
| 73 | + } |
| 74 | + JsonNode enumSchemaNode = parentSchemaNode.get("enum"); |
| 75 | + |
| 76 | + return new Validator(getValue(), readStringList(enumSchemaNode), readStringList(schemaNode)); |
| 77 | + } |
| 78 | + |
| 79 | + private List<String> readStringList(JsonNode node) { |
| 80 | + if (!node.isArray()) { |
| 81 | + throw new JsonSchemaException("Keyword enum needs to receive an array"); |
| 82 | + } |
| 83 | + ArrayList<String> result = new ArrayList<>(node.size()); |
| 84 | + for (JsonNode child : node) { |
| 85 | + result.add(child.asText()); |
| 86 | + } |
| 87 | + return result; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void customMetaSchemaWithIgnoredKeyword() throws JsonProcessingException, IOException { |
| 93 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 94 | + final JsonMetaSchema metaSchema = JsonMetaSchema |
| 95 | + .builder("https://github.com/networknt/json-schema-validator/tests/schemas/example01#", JsonMetaSchema.getDraftV4()) |
| 96 | + // Generated UI uses enumNames to render Labels for enum values |
| 97 | + .addKeyword(new EnumNamesKeyword()) |
| 98 | + .build(); |
| 99 | + |
| 100 | + 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" + |
| 106 | + "}"); |
| 107 | + |
| 108 | + Set<ValidationMessage> messages = schema.validate(objectMapper.readTree("\"foo\"")); |
| 109 | + assertEquals(1, messages.size()); |
| 110 | + |
| 111 | + ValidationMessage message = messages.iterator().next(); |
| 112 | + assertEquals("$: enumName is Foo !", message.getMessage()); |
| 113 | + } |
| 114 | +} |
0 commit comments