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

Does JsonSchema.NET support validating while deserializing to external types? #603

Closed
2 tasks done
guythetechie opened this issue Dec 23, 2023 · 5 comments · Fixed by #606
Closed
2 tasks done

Does JsonSchema.NET support validating while deserializing to external types? #603

guythetechie opened this issue Dec 23, 2023 · 5 comments · Fixed by #606
Labels
question Further information is requested

Comments

@guythetechie
Copy link

Documentation

  • I have consulted the documentation, and my question isn't answered there.

Nuget Package

JsonSchema.Net

Package Version

5.4.3

How can I help? Please provide as much context as possible.

Thanks for the great library.

If I understand the documentation correctly, deserialization with schema validation requires a [JsonSchema()] annotation on the destination type. What if we don't own the destination type though (e.g from an external class library)? Is there a way to validate and deserialize without the annotation?

My use case might actually be simpler than this. I'm trying to take a JsonObject, validate it with a predefined schema, then return a validated JsonObject. If the source JSON has extraneous properties but still passes validation, the destination JSON should only have properties from the schema. I was hoping for something like the code below, but I may be using the wrong tool for the job.

public static class ValidationHelpers
{
    private static readonly JsonSchema schema = JsonSchema.FromText("my schema text");

    public static void Validate(JsonNode? source)
    {
        var result = schema.Evaluate(source, new EvaluationOptions { OutputFormat = OutputFormat.Hierarchical});
        if (result.IsValid)
        {
            var validatedNode = result.xxx;
            Console.Write("""
                          Validation successful. Source might have extra properties, but no big deal.
                          validatedNode only has properties defined in the schema.
                          """);
        }
        else
        {
            var errorMessage = GetEvaluationErrorMessage(result);
            throw new JsonException(errorMessage);
        }
    }
}

Code of Conduct

  • I agree to follow this project's Code of Conduct
@guythetechie guythetechie added the question Further information is requested label Dec 23, 2023
@gregsdennis
Copy link
Collaborator

I don't have a way to do that, currently, no. However, off the top of my head, maybe a static type/schema registry would do. Something like:

ValidatingJsonConverter.Register<Point>(pointSchema);

@gregsdennis
Copy link
Collaborator

Here's what I've come up with:

JsonSchema PointSchema =
    new JsonSchemaBuilder()
        .Type(SchemaValueType.Object)
        .Properties(
            ("X", new JsonSchemaBuilder().Type(SchemaValueType.Integer)),
            ("Y", new JsonSchemaBuilder().Type(SchemaValueType.Integer))
        )
        .AdditionalProperties(false);

ValidatingJsonConverter.MapType<Point>(PointSchema);

Would this work for you?

@guythetechie
Copy link
Author

I'm sorry, but I don't see MapType or Register as static methods on ValidatingJsonConverter.

image

If I create an instance of ValidatingJsonConverter, those aren't available either.
image

Am I missing a package? Here's what I have installed.
<PackageReference Include="JsonSchema.Net" Version="5.4.3" />

@gregsdennis
Copy link
Collaborator

Yeah, I haven't published anything. I'm getting your thoughts on whether a particular UX would work for you.

This was referenced Dec 27, 2023
@guythetechie
Copy link
Author

Understood. Yes, that should work. Thanks!

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

Successfully merging a pull request may close this issue.

2 participants