[go: up one dir, main page]

Function validate

Source
pub fn validate(s: &mut Schema, rules: &[Rule]) -> Result<(), Error>
Expand description

Validate takes schema and applies a set of validation rules to it. The rules are stored on the top level under the “x-kubernetes-validations”.

use schemars::schema::Schema;
use kube::core::{Rule, Reason, Message, validate};

let mut schema = Schema::Object(Default::default());
let rules = &[Rule{
    rule: "self.spec.host == self.url.host".into(),
    message: Some("must be a URL with the host matching spec.host".into()),
    field_path: Some("spec.host".into()),
    ..Default::default()
}];
validate(&mut schema, rules)?;
assert_eq!(
    serde_json::to_string(&schema).unwrap(),
    r#"{"x-kubernetes-validations":[{"fieldPath":"spec.host","message":"must be a URL with the host matching spec.host","rule":"self.spec.host == self.url.host"}]}"#,
);