schemars icon indicating copy to clipboard operation
schemars copied to clipboard

Schema customization

Open MikailBag opened this issue 3 years ago • 0 comments

Sometimes user wants not to override schema generation, but to post-process it. One possible case is to add non-standard extensions, e.g. x-kubernetes-validations extension can be used to CustomResourceDefinition-s to add programmatic validation rules.

Proposed solution: add a new customize_with attribute which can be applied to the whole derive-ing container, e.g.

#[derive(JsonSchema)]
#[schemars(customize_with = "customize")]
struct Foo {
   // ...
}

// this function will be called after schema for Foo is generated, and it can modify returned schema
fn customize(schema: &mut schemars::schema::Schema) {
    // something like schema.extensions.insert("x-my-custom-extension", ...) here.
}

MikailBag avatar Sep 04 '22 10:09 MikailBag