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

Evaluate JsonRequired Attribute #682

Closed
2 tasks done
kostyrin opened this issue Mar 6, 2024 · 5 comments
Closed
2 tasks done

Evaluate JsonRequired Attribute #682

kostyrin opened this issue Mar 6, 2024 · 5 comments
Labels
pkg:schema-generation question Further information is requested

Comments

@kostyrin
Copy link

kostyrin commented Mar 6, 2024

Documentation

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

Nuget Package

JsonSchema.Net.Generation

Package Version

5.4.3

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

How to evaluate json schema with object which has properties with JsonRequiredAttribute?
I have written an example, but there are no errors in the result.

class MyClass
{
    [JsonRequired]
    public string RequiredString { get; set; }
}

[Test]
public void JsonSchema_Test()
{

    var schema = new JsonSchemaBuilder().FromType<MyClass>().Build();
    var jsonString = "{\"NotRequiredString\":\"not_required\"}";

    using var jDoc = JsonDocument.Parse(jsonString);
    var result = schema.Evaluate(jDoc);
    result.HasErrors.ShouldBeTrue();
}

Thanks for any advice

Code of Conduct

  • I agree to follow this project's Code of Conduct
@kostyrin kostyrin added the question Further information is requested label Mar 6, 2024
@kostyrin kostyrin changed the title JsonRequired Evaluate JsonRequired Attribute Mar 6, 2024
@gregsdennis
Copy link
Owner

Have you read the documentation?

https://docs.json-everything.net/schema/schemagen/schema-generation/#schema-schemagen-nullability

The attribute you're looking for is [Required]. I can support more with this attribute than what [JsonRequired] offers.

@kostyrin
Copy link
Author

kostyrin commented Mar 11, 2024

Due to Required Attribure is more specific for Json.Schema.Generation. I would prefer to use mostly using System.Text.Json.Serialization.JsonRequiredAttribute like System.Text.Json.Serialization.JsonPropertyNameAttribute. It's more make sense.

@gregsdennis
Copy link
Owner

gregsdennis commented Mar 11, 2024

I don't understand your question then. How do you want to use the [JsonRequired] attribute?

System.Text.Json.Serialization.JsonRequiredAttribute doesn't support the functionality I need for schema generation, so I had to make a new attribute.

@kostyrin
Copy link
Author

kostyrin commented Mar 12, 2024

just after evaluation should be contained errors due to RequiredString is marked as required with JsonRequired attribute. is it possible?

@gregsdennis
Copy link
Owner

Your issue is in the schema generation, which doesn't recognize [JsonRequired] (and I'm not inclined to add support for it). You need to use the [Required] that's supplied with the generation library.

class MyClass
{
    [Required]
    [JsonRequired]
    public string RequiredString { get; set; }
}

This will add the required keyword to the schema, which will create an error.

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

No branches or pull requests

2 participants