|
| 1 | +package io.github.simbo1905.json.schema; |
| 2 | + |
| 3 | +import jdk.sandbox.java.util.json.JsonArray; |
| 4 | +import jdk.sandbox.java.util.json.JsonObject; |
| 5 | +import jdk.sandbox.java.util.json.JsonString; |
| 6 | +import jdk.sandbox.java.util.json.JsonValue; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | + |
| 10 | +/// Array schema with item validation and constraints |
| 11 | +public record ArraySchema( |
| 12 | + JsonSchema items, |
| 13 | + Integer minItems, |
| 14 | + Integer maxItems, |
| 15 | + Boolean uniqueItems, |
| 16 | + // NEW: Pack 2 array features |
| 17 | + List<JsonSchema> prefixItems, |
| 18 | + JsonSchema contains, |
| 19 | + Integer minContains, |
| 20 | + Integer maxContains |
| 21 | +) implements JsonSchema { |
| 22 | + |
| 23 | + @Override |
| 24 | + public ValidationResult validateAt(String path, JsonValue json, Deque<ValidationFrame> stack) { |
| 25 | + if (!(json instanceof JsonArray arr)) { |
| 26 | + return ValidationResult.failure(List.of( |
| 27 | + new ValidationError(path, "Expected array") |
| 28 | + )); |
| 29 | + } |
| 30 | + |
| 31 | + List<ValidationError> errors = new ArrayList<>(); |
| 32 | + int itemCount = arr.values().size(); |
| 33 | + |
| 34 | + // Check item count constraints |
| 35 | + if (minItems != null && itemCount < minItems) { |
| 36 | + errors.add(new ValidationError(path, "Too few items: expected at least " + minItems)); |
| 37 | + } |
| 38 | + if (maxItems != null && itemCount > maxItems) { |
| 39 | + errors.add(new ValidationError(path, "Too many items: expected at most " + maxItems)); |
| 40 | + } |
| 41 | + |
| 42 | + // Check uniqueness if required (structural equality) |
| 43 | + if (uniqueItems != null && uniqueItems) { |
| 44 | + Set<String> seen = new HashSet<>(); |
| 45 | + for (JsonValue item : arr.values()) { |
| 46 | + String canonicalKey = canonicalize(item); |
| 47 | + if (!seen.add(canonicalKey)) { |
| 48 | + errors.add(new ValidationError(path, "Array items must be unique")); |
| 49 | + break; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Validate prefixItems + items (tuple validation) |
| 55 | + if (prefixItems != null && !prefixItems.isEmpty()) { |
| 56 | + // Validate prefix items - fail if not enough items for all prefix positions |
| 57 | + for (int i = 0; i < prefixItems.size(); i++) { |
| 58 | + if (i >= itemCount) { |
| 59 | + errors.add(new ValidationError(path, "Array has too few items for prefixItems validation")); |
| 60 | + break; |
| 61 | + } |
| 62 | + String itemPath = path + "[" + i + "]"; |
| 63 | + // Validate prefix items immediately to capture errors |
| 64 | + ValidationResult prefixResult = prefixItems.get(i).validateAt(itemPath, arr.values().get(i), stack); |
| 65 | + if (!prefixResult.valid()) { |
| 66 | + errors.addAll(prefixResult.errors()); |
| 67 | + } |
| 68 | + } |
| 69 | + // Validate remaining items with items schema if present |
| 70 | + if (items != null && items != AnySchema.INSTANCE) { |
| 71 | + for (int i = prefixItems.size(); i < itemCount; i++) { |
| 72 | + String itemPath = path + "[" + i + "]"; |
| 73 | + stack.push(new ValidationFrame(itemPath, items, arr.values().get(i))); |
| 74 | + } |
| 75 | + } |
| 76 | + } else if (items != null && items != AnySchema.INSTANCE) { |
| 77 | + // Original items validation (no prefixItems) |
| 78 | + int index = 0; |
| 79 | + for (JsonValue item : arr.values()) { |
| 80 | + String itemPath = path + "[" + index + "]"; |
| 81 | + stack.push(new ValidationFrame(itemPath, items, item)); |
| 82 | + index++; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Validate contains / minContains / maxContains |
| 87 | + if (contains != null) { |
| 88 | + int matchCount = 0; |
| 89 | + for (JsonValue item : arr.values()) { |
| 90 | + // Create isolated validation to check if item matches contains schema |
| 91 | + Deque<ValidationFrame> tempStack = new ArrayDeque<>(); |
| 92 | + List<ValidationError> tempErrors = new ArrayList<>(); |
| 93 | + tempStack.push(new ValidationFrame("", contains, item)); |
| 94 | + |
| 95 | + while (!tempStack.isEmpty()) { |
| 96 | + ValidationFrame frame = tempStack.pop(); |
| 97 | + ValidationResult result = frame.schema().validateAt(frame.path(), frame.json(), tempStack); |
| 98 | + if (!result.valid()) { |
| 99 | + tempErrors.addAll(result.errors()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (tempErrors.isEmpty()) { |
| 104 | + matchCount++; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + int min = (minContains != null ? minContains : 1); // default min=1 |
| 109 | + int max = (maxContains != null ? maxContains : Integer.MAX_VALUE); // default max=∞ |
| 110 | + |
| 111 | + if (matchCount < min) { |
| 112 | + errors.add(new ValidationError(path, "Array must contain at least " + min + " matching element(s)")); |
| 113 | + } else if (matchCount > max) { |
| 114 | + errors.add(new ValidationError(path, "Array must contain at most " + max + " matching element(s)")); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return errors.isEmpty() ? ValidationResult.success() : ValidationResult.failure(errors); |
| 119 | + } |
| 120 | + |
| 121 | + /// Canonicalization helper for structural equality in uniqueItems |
| 122 | + static String canonicalize(JsonValue v) { |
| 123 | + switch (v) { |
| 124 | + case JsonObject o -> { |
| 125 | + var keys = new ArrayList<>(o.members().keySet()); |
| 126 | + Collections.sort(keys); |
| 127 | + var sb = new StringBuilder("{"); |
| 128 | + for (int i = 0; i < keys.size(); i++) { |
| 129 | + String k = keys.get(i); |
| 130 | + if (i > 0) sb.append(','); |
| 131 | + sb.append('"').append(escapeJsonString(k)).append("\":").append(canonicalize(o.members().get(k))); |
| 132 | + } |
| 133 | + return sb.append('}').toString(); |
| 134 | + } |
| 135 | + case JsonArray a -> { |
| 136 | + var sb = new StringBuilder("["); |
| 137 | + for (int i = 0; i < a.values().size(); i++) { |
| 138 | + if (i > 0) sb.append(','); |
| 139 | + sb.append(canonicalize(a.values().get(i))); |
| 140 | + } |
| 141 | + return sb.append(']').toString(); |
| 142 | + } |
| 143 | + case JsonString s -> { |
| 144 | + return "\"" + escapeJsonString(s.value()) + "\""; |
| 145 | + } |
| 146 | + case null, default -> { |
| 147 | + // numbers/booleans/null: rely on stable toString from the Json* impls |
| 148 | + assert v != null; |
| 149 | + return v.toString(); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + static String escapeJsonString(String s) { |
| 154 | + if (s == null) return "null"; |
| 155 | + StringBuilder result = new StringBuilder(); |
| 156 | + for (int i = 0; i < s.length(); i++) { |
| 157 | + char ch = s.charAt(i); |
| 158 | + switch (ch) { |
| 159 | + case '"': |
| 160 | + result.append("\\\""); |
| 161 | + break; |
| 162 | + case '\\': |
| 163 | + result.append("\\\\"); |
| 164 | + break; |
| 165 | + case '\b': |
| 166 | + result.append("\\b"); |
| 167 | + break; |
| 168 | + case '\f': |
| 169 | + result.append("\\f"); |
| 170 | + break; |
| 171 | + case '\n': |
| 172 | + result.append("\\n"); |
| 173 | + break; |
| 174 | + case '\r': |
| 175 | + result.append("\\r"); |
| 176 | + break; |
| 177 | + case '\t': |
| 178 | + result.append("\\t"); |
| 179 | + break; |
| 180 | + default: |
| 181 | + if (ch < 0x20 || ch > 0x7e) { |
| 182 | + result.append("\\u").append(String.format("%04x", (int) ch)); |
| 183 | + } else { |
| 184 | + result.append(ch); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + return result.toString(); |
| 189 | + } |
| 190 | + |
| 191 | +} |
0 commit comments