|
| 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.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 20 | + |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import com.networknt.schema.SpecVersion.VersionFlag; |
| 26 | + |
| 27 | +/** |
| 28 | + * Test ExclusiveMinimumValidator validator. |
| 29 | + */ |
| 30 | +public class ExclusiveMinimumValidatorTest { |
| 31 | + @Test |
| 32 | + void draftV4ShouldHaveExclusiveMinimum() { |
| 33 | + String schemaData = "{" + |
| 34 | + " \"type\": \"object\"," + |
| 35 | + " \"properties\": {" + |
| 36 | + " \"value1\": {" + |
| 37 | + " \"type\": \"number\"," + |
| 38 | + " \"minimum\": 0," + |
| 39 | + " \"exclusiveMinimum\": true" + |
| 40 | + " }" + |
| 41 | + " }" + |
| 42 | + "}"; |
| 43 | + JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV4()) |
| 44 | + .unknownKeywordFactory(DisallowUnknownKeywordFactory.getInstance()).build(); |
| 45 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V4, |
| 46 | + builder -> builder.metaSchema(metaSchema)); |
| 47 | + JsonSchema schema = factory.getSchema(schemaData); |
| 48 | + String inputData = "{\"value1\":0}"; |
| 49 | + String validData = "{\"value1\":0.1}"; |
| 50 | + |
| 51 | + Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON); |
| 52 | + assertEquals(1, messages.size()); |
| 53 | + assertEquals(1, messages.stream().filter(m -> "minimum".equals(m.getType())).count()); |
| 54 | + |
| 55 | + messages = schema.validate(validData, InputFormat.JSON); |
| 56 | + assertEquals(0, messages.size()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void draftV6ShouldNotAllowExclusiveMinimumBoolean() { |
| 61 | + String schemaData = "{" + |
| 62 | + " \"type\": \"object\"," + |
| 63 | + " \"properties\": {" + |
| 64 | + " \"value1\": {" + |
| 65 | + " \"type\": \"number\"," + |
| 66 | + " \"minimum\": 0," + |
| 67 | + " \"exclusiveMinimum\": true" + |
| 68 | + " }" + |
| 69 | + " }" + |
| 70 | + "}"; |
| 71 | + JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV6()) |
| 72 | + .unknownKeywordFactory(DisallowUnknownKeywordFactory.getInstance()).build(); |
| 73 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V6, |
| 74 | + builder -> builder.metaSchema(metaSchema)); |
| 75 | + assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void draftV7ShouldNotAllowExclusiveMinimumBoolean() { |
| 80 | + String schemaData = "{" + |
| 81 | + " \"type\": \"object\"," + |
| 82 | + " \"properties\": {" + |
| 83 | + " \"value1\": {" + |
| 84 | + " \"type\": \"number\"," + |
| 85 | + " \"minimum\": 0," + |
| 86 | + " \"exclusiveMinimum\": true" + |
| 87 | + " }" + |
| 88 | + " }" + |
| 89 | + "}"; |
| 90 | + JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV7()) |
| 91 | + .unknownKeywordFactory(DisallowUnknownKeywordFactory.getInstance()).build(); |
| 92 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V7, |
| 93 | + builder -> builder.metaSchema(metaSchema)); |
| 94 | + assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData)); |
| 95 | + } |
| 96 | +} |
0 commit comments