Comments (4)
I would expect what you have to work just fine. Have you tried it?
The validating deserialization works (in this example) by creating a serializer when you deserialize ParentClass
. That serializer will decode the entire payload and validate it against ParentClass.Schema
which has the reference to PropertyClass.Schema
already.
So all validation really occurs at the parent level. You need the entire schema there.
If validation is successful, the validating serializer will be removed from the options (so that the serializing one doesn't get called again), and then it will attempt to deserialize with the updated options.
from json-everything.
I haven't tried it because it is still missing a piece of validation. I want the property of PropertyClass to also be required and I see no way to do that. I also have the string properties set as Required. Validation is missing in ParentClass.Schema.
from json-everything.
I think I figured it out. I am passing the Properties from the PropertyClass.Schema into the JsonSchemaBuilder of ParentClass. That seems to be doing the validation for me. I did realize that I was doing Required wrong. Since it was an optional parameter for the name, I thought if you just put Required() on the JsonSchemaBuilder that it would assign required to property name.
If there is a better way to do Required, much appreciated to know.
Here is my new code:
using Json.Schema;
using Json.Schema.Serialization;
using System.ComponentModel.DataAnnotations;
namespace JsonInvestigation;
[JsonSchema(typeof(ParentClass), nameof(Schema))]
public sealed class ParentClass
{
[Required]
[StringLength(5)]
public string Name { get; set; } = string.Empty;
[Required]
public PropertyClass Property { get; set; } = new PropertyClass();
public readonly static JsonSchema Schema = new JsonSchemaBuilder()
.Type(SchemaValueType.Object)
.Properties((nameof(Name), new JsonSchemaBuilder()
.Type(SchemaValueType.String)
.MaxLength(5)),
(nameof(Property), new JsonSchemaBuilder()
.Type(SchemaValueType.Object)
.Properties(PropertyClass.Schema.GetProperties() ?? new Dictionary<string, JsonSchema>())))
.Required(nameof(Name), nameof(Property));
}
[JsonSchema(typeof(PropertyClass), nameof(Schema))]
public sealed class PropertyClass
{
[Required]
[StringLength(5)]
public string Name { get; set; } = string.Empty;
public readonly static JsonSchema Schema = new JsonSchemaBuilder()
.Type(SchemaValueType.Object)
.Properties((nameof(Name), new JsonSchemaBuilder()
.Type(SchemaValueType.String)
.MaxLength(5)))
.Required(nameof(Name));
}
from json-everything.
The builder extensions mimic the schema itself. The required
keyword is placed at the object level to require the properties which are required in that object.
{
"type": "object",
"required": ["foo"]
}
The extension method acts the same.
JsonSchema schema = new JsonSchemaBuilder()
.Type(SchemaValueType.Object)
.Required("foo");
from json-everything.
Related Issues (20)
- Extract data annotation attributes from JsonSchema.Net.Generation into separate project HOT 2
- Is there a way to do JSON validation that does case insensitive matching on property names? HOT 8
- Support language packs in schema playground
- sileo-featured.json
- How to export or save JsonSchema to plaintext? HOT 4
- Schema generation doesn't work on site HOT 1
- maximum/minimum use decimal which do not parse certain many double "number" strings HOT 3
- `Json.Schema.JsonSchema.Evaluate` throws "`System.OverflowException`: Value was either too large or too small for a Decimal." HOT 4
- Get json data from only required properties based on the attributes set HOT 4
- Schema validation crashes with stack overflow HOT 12
- Patch Path Not Escaping Forward Slash HOT 6
- EvaluationResults default value shows up in Annotations when number out or range, but not on other situations HOT 3
- Performance vs Newtonsoft.Json.Schema HOT 6
- Pattern and PatternProperties keyword performance and security improvements HOT 3
- How to add a keyword before another one HOT 2
- Schema validation doesn't fill EvaluationResults with any Errors for anyOf HOT 3
- ValidateMetaSchema option not working
- Why is NullableAttribute needed for nullable properties HOT 3
- Peformance issue with certain type of schema HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-everything.