Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if Value satisfies RootSchema #309

Open
schneiderfelipe opened this issue Jul 26, 2024 · 1 comment
Open

Check if Value satisfies RootSchema #309

schneiderfelipe opened this issue Jul 26, 2024 · 1 comment

Comments

@schneiderfelipe
Copy link

I would like to check whether a serde_json::Value satisfies a given RootSchema. Is this the right library or should I use e.g. jsonschema?

@GREsau
Copy link
Owner

GREsau commented Aug 12, 2024

It's possible schemars will get this feature in the future, but there's no immediate plans to implement such a feature yet - for now, the jsonschema crate is probably what you want.

You can, however, generate a JSON schema using schemars and then use jsonschema to validate a value against the schema. e.g. using schemars 1.0.0-alpha.3:

let schema = SchemaSettings::draft07()
    .into_generator()
    .into_root_schema_for::<MyStruct>();
let compiled = JSONSchema::compile(schema.as_value()).expect("A valid schema");

let instance = json!({ "foo": "bar" });

let result = compiled.validate(&instance);
if let Err(errors) = result {
    for error in errors {
        println!("Validation error: {}", error);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants