|
| 1 | +// Copyright 2025 Croupier Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 |
| 3 | + |
| 4 | +using System.Text.Json; |
| 5 | +using Croupier.Sdk.Validation; |
| 6 | +using FluentAssertions; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Croupier.Sdk.Tests; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// JsonSchemaValidator 字符串入口与各约束分支的缺口补测 |
| 13 | +/// (numeric/string/array/object 约束、$ref 解析失败、错误消息文案)。 |
| 14 | +/// </summary> |
| 15 | +public class JsonSchemaValidatorBranchTests |
| 16 | +{ |
| 17 | + private static JsonElement Parse(string json) |
| 18 | + { |
| 19 | + using var doc = JsonDocument.Parse(json); |
| 20 | + return doc.RootElement.Clone(); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void Validate_StringEntry_MissingRequired_ReportsPath() |
| 25 | + { |
| 26 | + var schema = """{"type":"object","required":["name"],"properties":{"name":{"type":"string"}}}"""; |
| 27 | + var errors = JsonSchemaValidator.Validate("{}", schema); |
| 28 | + errors.Should().NotBeEmpty(); |
| 29 | + errors[0].Should().Contain("name"); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void Numeric_Bounds_AllBranches() |
| 34 | + { |
| 35 | + var schema = """ |
| 36 | + { |
| 37 | + "type": "number", |
| 38 | + "minimum": 1, |
| 39 | + "maximum": 10, |
| 40 | + "exclusiveMinimum": 0.5, |
| 41 | + "exclusiveMaximum": 9.5, |
| 42 | + "multipleOf": 3 |
| 43 | + } |
| 44 | + """; |
| 45 | + JsonSchemaValidator.Validate("0", schema)[0].Should().Contain("less than minimum"); |
| 46 | + JsonSchemaValidator.Validate("11", schema)[0].Should().Contain("greater than maximum"); |
| 47 | + JsonSchemaValidator.Validate("0.4", schema).Should().Contain(e => e.Contains("greater than 0.5") || e.Contains("exclusiveMinimum")); |
| 48 | + JsonSchemaValidator.Validate("9.5", schema).Should().Contain(e => e.Contains("less than 9.5")); |
| 49 | + JsonSchemaValidator.Validate("4", schema).Should().Contain(e => e.Contains("multiple of")); |
| 50 | + JsonSchemaValidator.Validate("6", schema).Should().BeEmpty(); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void String_Constraints_AllBranches() |
| 55 | + { |
| 56 | + var schema = """ |
| 57 | + { "type": "string", "minLength": 2, "maxLength": 5, "pattern": "^[a-z]+$" } |
| 58 | + """; |
| 59 | + JsonSchemaValidator.Validate("\"a\"", schema)[0].Should().Contain("minLength"); |
| 60 | + JsonSchemaValidator.Validate("\"abcdef\"", schema)[0].Should().Contain("maxLength"); |
| 61 | + JsonSchemaValidator.Validate("\"AB\"", schema).Should().Contain(e => e.Contains("pattern")); |
| 62 | + JsonSchemaValidator.Validate("\"abc\"", schema).Should().BeEmpty(); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void String_InvalidPattern_IsIgnored() |
| 67 | + { |
| 68 | + // 非法正则(未闭合括号)——校验器必须忽略而不是抛出。 |
| 69 | + var schema = """{"type":"string","pattern":"[unclosed"}"""; |
| 70 | + var act = () => JsonSchemaValidator.Validate("\"anything\"", schema); |
| 71 | + act.Should().NotThrow(); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void Array_Constraints_AllBranches() |
| 76 | + { |
| 77 | + var schema = """ |
| 78 | + { "type": "array", "minItems": 2, "maxItems": 3, "uniqueItems": true } |
| 79 | + """; |
| 80 | + JsonSchemaValidator.Validate("[1]", schema)[0].Should().Contain("minItems"); |
| 81 | + JsonSchemaValidator.Validate("[1,2,3,4]", schema)[0].Should().Contain("maxItems"); |
| 82 | + JsonSchemaValidator.Validate("[1,1]", schema).Should().Contain(e => e.Contains("not unique")); |
| 83 | + JsonSchemaValidator.Validate("[1,2]", schema).Should().BeEmpty(); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void Array_ItemsSchema_AppliedToElements() |
| 88 | + { |
| 89 | + var schema = """{"type":"array","items":{"type":"integer"}}"""; |
| 90 | + JsonSchemaValidator.Validate("[1, \"x\"]", schema).Should().Contain(e => e.Contains("integer")); |
| 91 | + JsonSchemaValidator.Validate("[1, 2]", schema).Should().BeEmpty(); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void Object_AdditionalProperty_Branches() |
| 96 | + { |
| 97 | + var noAdditional = """{"type":"object","properties":{"a":{"type":"string"}},"additionalProperties":false}"""; |
| 98 | + JsonSchemaValidator.Validate("""{"a":"x","b":1}""", noAdditional) |
| 99 | + .Should().Contain(e => e.Contains("not allowed")); |
| 100 | + |
| 101 | + var typedAdditional = """{"type":"object","properties":{"a":{"type":"string"}},"additionalProperties":{"type":"number"}}"""; |
| 102 | + JsonSchemaValidator.Validate("""{"a":"x","b":"not-a-number"}""", typedAdditional) |
| 103 | + .Should().Contain(e => e.Contains("number")); |
| 104 | + |
| 105 | + JsonSchemaValidator.Validate("""{"a":"x","b":7}""", typedAdditional).Should().BeEmpty(); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void Ref_UnknownPointer_ReportsError() |
| 110 | + { |
| 111 | + var schema = """{"$ref":"#/definitions/missing","definitions":{}}"""; |
| 112 | + var errors = JsonSchemaValidator.Validate("42", schema); |
| 113 | + errors.Should().Contain(e => e.Contains("$ref") || e.Contains("missing")); |
| 114 | + } |
| 115 | + |
| 116 | + [Fact] |
| 117 | + public void Ref_EscapedPointerSegments_Resolve() |
| 118 | + { |
| 119 | + // ~1 = /,~0 = ~ 的 JSON Pointer 转义。 |
| 120 | + var schema = """ |
| 121 | + { |
| 122 | + "$ref": "#/definitions/a~1b", |
| 123 | + "definitions": { "a/b": { "type": "string" } } |
| 124 | + } |
| 125 | + """; |
| 126 | + JsonSchemaValidator.Validate("\"ok\"", schema).Should().BeEmpty(); |
| 127 | + JsonSchemaValidator.Validate("42", schema).Should().NotBeEmpty(); |
| 128 | + } |
| 129 | + |
| 130 | + [Fact] |
| 131 | + public void Enum_MismatchReports_NonStringValues() |
| 132 | + { |
| 133 | + var schema = """{"enum":[1,2,3]}"""; |
| 134 | + JsonSchemaValidator.Validate("4", schema).Should().Contain(e => e.Contains("enum")); |
| 135 | + JsonSchemaValidator.Validate("2", schema).Should().BeEmpty(); |
| 136 | + } |
| 137 | + |
| 138 | + [Fact] |
| 139 | + public void Const_MismatchReports() |
| 140 | + { |
| 141 | + var schema = """{"const":"v1"}"""; |
| 142 | + JsonSchemaValidator.Validate("\"v2\"", schema).Should().NotBeEmpty(); |
| 143 | + JsonSchemaValidator.Validate("\"v1\"", schema).Should().BeEmpty(); |
| 144 | + } |
| 145 | + |
| 146 | + [Fact] |
| 147 | + public void Integer_Type_ChecksIntegralForm() |
| 148 | + { |
| 149 | + var schema = """{"type":"integer"}"""; |
| 150 | + JsonSchemaValidator.Validate("1.5", schema).Should().NotBeEmpty(); |
| 151 | + JsonSchemaValidator.Validate("1e3", schema).Should().NotBeEmpty(); |
| 152 | + JsonSchemaValidator.Validate("7", schema).Should().BeEmpty(); |
| 153 | + } |
| 154 | + |
| 155 | + [Fact] |
| 156 | + public void TypeNames_InErrorMessages() |
| 157 | + { |
| 158 | + var schema = """{"type":"string"}"""; |
| 159 | + foreach (var payload in new[] { "42", "true", "null", "[1]", """{"k":1}""" }) |
| 160 | + { |
| 161 | + var errors = JsonSchemaValidator.Validate(payload, schema); |
| 162 | + errors.Should().NotBeEmpty($"payload {payload} is not a string"); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments