|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 the original author or authors. |
| 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 static org.junit.jupiter.api.Assertions.*; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import com.networknt.schema.SpecVersion.VersionFlag; |
| 26 | + |
| 27 | +/** |
| 28 | + * EnumValidator test. |
| 29 | + */ |
| 30 | +class EnumValidatorTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void enumWithObjectNodes() { |
| 34 | + String schemaData = "{\r\n" |
| 35 | + + " \"title\": \"Severity\",\r\n" |
| 36 | + + " \"type\": \"object\",\r\n" |
| 37 | + + " \"properties\": {\r\n" |
| 38 | + + " \"name\": {\r\n" |
| 39 | + + " \"title\": \"Name\",\r\n" |
| 40 | + + " \"description\": \"The human readable name of the severity\",\r\n" |
| 41 | + + " \"type\": \"string\"\r\n" |
| 42 | + + " },\r\n" |
| 43 | + + " \"cardinality\": {\r\n" |
| 44 | + + " \"title\": \"Cardinality\",\r\n" |
| 45 | + + " \"description\": \"The severities cardinality, the higher the worse it gets\",\r\n" |
| 46 | + + " \"type\": \"integer\",\r\n" |
| 47 | + + " \"minimum\": 0,\r\n" |
| 48 | + + " \"maximum\": 50,\r\n" |
| 49 | + + " \"multipleOf\": 10\r\n" |
| 50 | + + " }\r\n" |
| 51 | + + " },\r\n" |
| 52 | + + " \"additionalProperties\": false,\r\n" |
| 53 | + + " \"required\": [\r\n" |
| 54 | + + " \"name\",\r\n" |
| 55 | + + " \"cardinality\"\r\n" |
| 56 | + + " ],\r\n" |
| 57 | + + " \"enum\": [\r\n" |
| 58 | + + " {\r\n" |
| 59 | + + " \"name\": \"EMPTY\",\r\n" |
| 60 | + + " \"cardinality\": 0\r\n" |
| 61 | + + " },\r\n" |
| 62 | + + " {\r\n" |
| 63 | + + " \"name\": \"OK\",\r\n" |
| 64 | + + " \"cardinality\": 20\r\n" |
| 65 | + + " },\r\n" |
| 66 | + + " {\r\n" |
| 67 | + + " \"name\": \"UNKNOWN\",\r\n" |
| 68 | + + " \"cardinality\": 30\r\n" |
| 69 | + + " },\r\n" |
| 70 | + + " {\r\n" |
| 71 | + + " \"name\": \"WARNING\",\r\n" |
| 72 | + + " \"cardinality\": 40\r\n" |
| 73 | + + " },\r\n" |
| 74 | + + " {\r\n" |
| 75 | + + " \"name\": \"CRITICAL\",\r\n" |
| 76 | + + " \"cardinality\": 50\r\n" |
| 77 | + + " }\r\n" |
| 78 | + + " ],\r\n" |
| 79 | + + " \"default\": {\r\n" |
| 80 | + + " \"name\": \"UNKNOWN\",\r\n" |
| 81 | + + " \"cardinality\": 30\r\n" |
| 82 | + + " }\r\n" |
| 83 | + + "}"; |
| 84 | + String inputData = "{\r\n" |
| 85 | + + " \"name\": \"FOO\",\r\n" |
| 86 | + + " \"cardinality\": 50\r\n" |
| 87 | + + "}"; |
| 88 | + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, |
| 89 | + SchemaValidatorsConfig.builder().build()); |
| 90 | + List<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON).stream().collect(Collectors.toList()); |
| 91 | + assertEquals(1, messages.size()); |
| 92 | + ValidationMessage message = messages.get(0); |
| 93 | + assertEquals( |
| 94 | + ": does not have a value in the enumeration [{\"name\":\"EMPTY\",\"cardinality\":0}, {\"name\":\"OK\",\"cardinality\":20}, {\"name\":\"UNKNOWN\",\"cardinality\":30}, {\"name\":\"WARNING\",\"cardinality\":40}, {\"name\":\"CRITICAL\",\"cardinality\":50}]", |
| 95 | + message.toString()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void enumWithHeterogenousNodes() { |
| 100 | + String schemaData = "{\r\n" |
| 101 | + + " \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\r\n" |
| 102 | + + " \"enum\": [6, \"foo\", [], true, {\"foo\": 12}]\r\n" |
| 103 | + + " }"; |
| 104 | + String inputData = "{\r\n" |
| 105 | + + " \"name\": \"FOO\",\r\n" |
| 106 | + + " \"cardinality\": 50\r\n" |
| 107 | + + "}"; |
| 108 | + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, |
| 109 | + SchemaValidatorsConfig.builder().build()); |
| 110 | + List<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON).stream().collect(Collectors.toList()); |
| 111 | + assertEquals(1, messages.size()); |
| 112 | + ValidationMessage message = messages.get(0); |
| 113 | + assertEquals(": does not have a value in the enumeration [6, \"foo\", [], true, {\"foo\":12}]", message.toString()); |
| 114 | + } |
| 115 | +} |
0 commit comments