Skip to content

Commit da4e08a

Browse files
author
Pascal Post
committed
add contains
1 parent f1c7458 commit da4e08a

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The validator currently uses C++ regex implementation for pattern matching, requ
2626
- [x] anyOf.json
2727
- [x] boolean_schema.json
2828
- [x] const.json
29-
- [ ] contains.json
29+
- [x] contains.json
3030
- [ ] default.json
3131
- [ ] definitions.json
3232
- [ ] dependencies.json

src/array.zig

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,26 @@ pub fn checks(node: std.json.ObjectMap, data: std.json.Value, stack: *Stack, col
111111
for (head + 1..items.len) |index| {
112112
if (eql(item, items[index])) {
113113
if (collect_errors) |errors| {
114-
const msg = try std.fmt.allocPrint(errors.arena.allocator(), "Array contains non-unique items ({} and {}) ", .{ head, index });
114+
const msg = try std.fmt.allocPrint(errors.arena.allocator(), "Array contains non-unique items ({} and {})", .{ head, index });
115115
try errors.append(.{ .path = try stack.constructPath(errors.arena.allocator()), .msg = msg });
116116
} else return false;
117117
}
118118
}
119119
}
120120
}
121121

122+
if (node.get("contains")) |contains| blk: {
123+
try stack.pushPath("contains");
124+
defer stack.pop();
125+
126+
for (data.array.items) |item| {
127+
if (try schema.checks(contains, item, stack, null)) break :blk;
128+
}
129+
130+
if (collect_errors) |errors| {
131+
try errors.append(.{ .path = try stack.constructPath(errors.arena.allocator()), .msg = "Array items are all invalid against contains schema." });
132+
} else return false;
133+
}
134+
122135
return if (collect_errors) |errors| errors.empty() else true;
123136
}

src/test_suite.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ test "run test suite" {
6363
"allOf.json",
6464
"not.json",
6565
"if-then-else.json",
66+
"contains.json",
6667
// "ref.json",
6768
// "WIP.json",
6869
};

0 commit comments

Comments
 (0)