-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstraint_wbtest.mbt
More file actions
67 lines (65 loc) · 2.09 KB
/
Copy pathconstraint_wbtest.mbt
File metadata and controls
67 lines (65 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Pydantic-style field constraints: enforced at validation with the matching error types, and
// emitted into the field's OpenAPI schema (version-aware `exclusiveMinimum`).
///|
test "constraints: a conforming body passes, violations yield Pydantic error kinds" {
let schema = Schema::object("Thing", [
Field::new("n", SInt, constraints=[Minimum(1.0), Maximum(10.0)]),
Field::new("name", SStr, constraints=[
MinLength(3),
MaxLength(5),
Pattern("^[a-z]+$"),
]),
Field::new("tags", Schema::array(SStr), constraints=[MinItems(1)]),
])
let ok : Array[ValidationError] = []
validate_schema(
schema,
@json.parse(
(
#|{"n":5,"name":"abc","tags":["x"]}
),
),
[],
ok,
)
assert_eq(ok.length(), 0)
let errs : Array[ValidationError] = []
validate_schema(
schema,
@json.parse(
(
#|{"n":20,"name":"AB","tags":[]}
),
),
[],
errs,
)
let body = validation_error_body(errs).stringify()
// n = 20 exceeds the maximum.
assert_eq(body.contains("less_than_equal"), true)
// name = "AB" is too short and does not match the lowercase pattern.
assert_eq(body.contains("string_too_short"), true)
assert_eq(body.contains("string_pattern_mismatch"), true)
// tags = [] is below the minimum item count.
assert_eq(body.contains("too_short"), true)
}
///|
test "constraints: emission is version-aware for exclusiveMinimum" {
let base : Json = { "type": "integer" }
// OpenAPI 3.1 (JSON-Schema 2020-12): exclusiveMinimum is a number.
match with_constraints(base, [ExclusiveMinimum(5.0)], OpenApi31) {
Object(o) => {
assert_eq(o.get("exclusiveMinimum") is Some(Number(_)), true)
assert_eq(o.get("minimum"), None)
}
_ => fail("expected an object")
}
// OpenAPI 3.0 / Swagger 2.0: exclusiveMinimum is a boolean flag beside minimum.
match with_constraints(base, [ExclusiveMinimum(5.0)], OpenApi30) {
Object(o) => {
assert_eq(o.get("exclusiveMinimum") is Some(True), true)
assert_eq(o.get("minimum") is Some(Number(_)), true)
}
_ => fail("expected an object")
}
}