use kube_cel::Validator;
use serde_json::json;
fn main() {
let schema = json!({
"type": "object",
"properties": {
"spec": {
"type": "object",
"properties": {
"replicas": {"type": "integer"}
},
"x-kubernetes-validations": [{
"rule": "self.replicas <= 5",
"message": "too many replicas",
"messageExpression":
"'requested ' + string(self.replicas) + ' replicas, max allowed is 5'"
}]
}
}
});
let validator = Validator::new();
for replicas in [8, 20] {
let object = json!({"spec": {"replicas": replicas}});
if let Err(errors) = validator.validate(&schema, &object, None) {
for err in &errors {
println!("replicas={replicas}: {}", err.message);
}
}
}
let ok = json!({"spec": {"replicas": 3}});
match validator.validate(&schema, &ok, None) {
Ok(()) => println!("replicas=3: 0 error(s)"),
Err(errors) => println!("replicas=3: {} error(s)", errors.len()),
}
}