Git Product home page Git Product logo

Comments (4)

gregsdennis avatar gregsdennis commented on July 18, 2024

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.

Welchen avatar Welchen commented on July 18, 2024

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.

Welchen avatar Welchen commented on July 18, 2024

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.

gregsdennis avatar gregsdennis commented on July 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.