pub use multicast_codemode::workflow_schema::{check_supported, validate};
#[cfg(test)]
mod tests {
use super::*;
use serde_json::{Value, json};
fn schema() -> Value {
json!({
"type": "object",
"properties": {
"count": {"type": "integer", "minimum": 1, "maximum": 10},
"label": {"type": "string", "minLength": 2, "maxLength": 3},
"mode": {"enum": ["fast", "safe"]},
"tags": {"type": "array", "items": {"type": "string"}, "maxItems": 2},
"note": {"type": ["string", "null"]}
},
"required": ["count"],
"additionalProperties": false
})
}
#[test]
fn accepts_conforming_input() {
let value = json!({"count": 3, "label": "ab", "mode": "safe", "tags": ["x"], "note": null});
assert_eq!(validate(&schema(), &value), Ok(()));
}
#[test]
fn rejects_each_violation_independently() {
let cases = [
json!({}),
json!({"count": 0}),
json!({"count": 11}),
json!({"count": 1.5}),
json!({"count": "1"}),
json!({"count": 1, "label": "a"}),
json!({"count": 1, "label": "abcd"}),
json!({"count": 1, "mode": "slow"}),
json!({"count": 1, "tags": [1]}),
json!({"count": 1, "tags": ["a", "b", "c"]}),
json!({"count": 1, "note": 4}),
json!({"count": 1, "extra": true}),
json!([]),
];
for case in cases {
assert!(validate(&schema(), &case).is_err(), "accepted {case}");
}
}
#[test]
fn counts_unicode_scalars_for_length() {
let schema = json!({"type": "string", "maxLength": 2});
assert_eq!(validate(&schema, &json!("😀😀")), Ok(()));
assert!(validate(&schema, &json!("😀😀😀")).is_err());
}
#[test]
fn compares_large_integers_exactly() {
let schema = json!({"type": "integer", "maximum": 9007199254740993_u64});
assert_eq!(validate(&schema, &json!(9007199254740993_u64)), Ok(()));
assert!(validate(&schema, &json!(9007199254740994_u64)).is_err());
}
#[test]
fn rejects_unsupported_keywords() {
let schema =
json!({"type": "object", "properties": {"a": {"type": "string", "pattern": "x"}}});
assert!(check_supported(&schema).is_err());
assert!(validate(&schema, &json!({"a": "y"})).is_err());
assert_eq!(
check_supported(&json!({"type": "string", "description": "d"})),
Ok(())
);
}
}