GREsau / schemars

Generate JSON Schema documents from Rust code
https://graham.cool/schemars/
MIT License
797 stars 223 forks source link

Kubernetes extensions #258

Open clux opened 9 months ago

clux commented 9 months ago

Hey there,

(First off, thank you for your maintenance of schemars, it's really doing a lot of heavy lifting in the kube ecosystem where it's used everywhere - and it's more or less mandatory to derive JsonSchema on Kubernetes Custom Resources.)

Background

Kubernetes has a bunch of small extensions to openapi schemas that they allow to be injected for special behaviour on the server-side.

Most of them are straight key-value such as x-kubernetes-list-type: "set" which we have been telling people to use #[schemars(schema_with ...)] to override ala https://github.com/kube-rs/kube/blob/3a4f72414a4ad3733c2683514abff1924271759d/examples/crd_derive_schema.rs#L84-L102 to get the right behaviour in these special cases. This works, but it's a little awkward to deal with ergonomically (since you have to specify all the other properties manually - afaikt).

In newer Kubernetes versions, these extensions are becoming more common and the new validation extension is expected to be the main way to do complex validation on the server-side via declarative rules going forward.

Question

I was wondering if you would be open to having feature-flagged kubernetes openapi extensions in schemars to allow us to specify schemars attributes for the various x-kubernetes values?

E.g. so we could do stuff like;

#[derive(JsonSchema)]
struct SomeDataStruct {
    #[schemars(x-kubernetes-list-type = "set")
    some_set: Vec<u32>,

    #[schemars(x-kubernetes-validations(
        rule = "self.minReplicas <= self.replicas",
        message = "replicas should be greater than or equal to minReplicas."
    ))]
    replicas: usize,
}

without having to use schema_with in quite so many places.