-
-
Notifications
You must be signed in to change notification settings - Fork 150
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
Labels
question
Further information is requested
Comments
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); |
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? |
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
Understood. Yes, that should work. Thanks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Documentation
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.
Code of Conduct
The text was updated successfully, but these errors were encountered: