schemars icon indicating copy to clipboard operation
schemars copied to clipboard

Provide example/functionality for validating incoming json against schema

Open TheButlah opened this issue 1 year ago • 3 comments

There are many examples of extracting a schema from a rust type. But what I would really like is for me to deserialize a RootSchema from my backend, and use it to validate incoming data entirely dynamically. In other words, my code will only have a RootSchema but no concrete types that have #[derive(JsonSchema)].

As far as I can tell, there is no way to do this with schemars.

TheButlah avatar Jan 09 '25 04:01 TheButlah

Hello.

Do you want something like axum_jsonschema?

ya7010 avatar Jan 12 '25 01:01 ya7010

I'm not using axum, so probably not. I mean something more like this, where the schema and value are dynamic and serializeable.

TheButlah avatar Jan 13 '25 21:01 TheButlah

My understanding is that this crate is intended to provide the ability to generate JSON Schema.

What axum_jsonschema does is to create a JSON Schema with schemars and then validate it using jsonschema and serde_json. (#[derive(Deserialize, JsonSchema)] is required for struct)

If you want to validate a structure after deserializing it with serde_json, you can use a combination of schemars and validator. (#[derive(Deserialize, JsonSchema, Validate)] is required for struct)

In any case, a devive like #[derive(JsonSchema)] is necessary if you want to use schemars.

ya7010 avatar Jan 14 '25 01:01 ya7010