c# - NJsonSchema dependentRequired not workifthenelse also not work why? - Stack Overflow

admin2025-04-16  0

I need to do json schema validation I Found NJsonSchema - looks good Validation works as expected when validationg types enums etc but do not work for condidional as here

so as in example

static async Task Main()
{
    // Define JSON Schema with dependentRequired validation
    string schemaJson = @"
    {
        ""type"": ""object"",
        ""properties"": {
            ""name"": { ""type"": ""string"" },
            ""credit_card"": { ""type"": ""string"" },
            ""billing_address"": { ""type"": ""string"" }
        },
        ""dependentRequired"": {
            ""credit_card"": [""billing_address""]
        }
    }";

    string validJson1 = @"{ ""name"": ""John Doe"" }";  // Valid JSON: No credit_card, so no billing_address required
    string invalidJson = @"{ ""name"": ""John Doe"", ""credit_card"": ""1234-5678-9876"" }";  // Invalid JSON: credit_card is present but billing_address is missing

    var schema = await NJsonSchema.JsonSchema.FromJsonAsync(schemaJson);     // Load schema

    // Validate JSON cases
    Console.WriteLine("Valid JSON 1 (No credit card):");
    ValidateJson(validJson1, schema);

    Console.WriteLine("\nInvalid JSON (Missing billing address but has credit card):");
    ValidateJson(invalidJson, schema);
}

static void ValidateJson(string json, NJsonSchema.JsonSchema schema)
{
    var validator = new JsonSchemaValidator();
    
    var errors = validator.Validate(json, schema);// schema.Validate(json); - the smae..
    if (errors.Count == 0)
    {
        Console.WriteLine("✅ Valid JSON");
    }
    else
    {
        Console.WriteLine("❌ Invalid JSON");
        foreach (var error in errors)
        {
            Console.WriteLine($"Error: {error.Path} - {error.Kind}");
        }
    }
}

after run this i get 2 times Valid JSON but one should NOT be valid why / what am i missing for this to work?

when i test this in / then validaiton error is visible.

regards

I need to do json schema validation I Found NJsonSchema - looks good Validation works as expected when validationg types enums etc but do not work for condidional as here https://json-schema./understanding-json-schema/reference/conditionals

so as in example

static async Task Main()
{
    // Define JSON Schema with dependentRequired validation
    string schemaJson = @"
    {
        ""type"": ""object"",
        ""properties"": {
            ""name"": { ""type"": ""string"" },
            ""credit_card"": { ""type"": ""string"" },
            ""billing_address"": { ""type"": ""string"" }
        },
        ""dependentRequired"": {
            ""credit_card"": [""billing_address""]
        }
    }";

    string validJson1 = @"{ ""name"": ""John Doe"" }";  // Valid JSON: No credit_card, so no billing_address required
    string invalidJson = @"{ ""name"": ""John Doe"", ""credit_card"": ""1234-5678-9876"" }";  // Invalid JSON: credit_card is present but billing_address is missing

    var schema = await NJsonSchema.JsonSchema.FromJsonAsync(schemaJson);     // Load schema

    // Validate JSON cases
    Console.WriteLine("Valid JSON 1 (No credit card):");
    ValidateJson(validJson1, schema);

    Console.WriteLine("\nInvalid JSON (Missing billing address but has credit card):");
    ValidateJson(invalidJson, schema);
}

static void ValidateJson(string json, NJsonSchema.JsonSchema schema)
{
    var validator = new JsonSchemaValidator();
    
    var errors = validator.Validate(json, schema);// schema.Validate(json); - the smae..
    if (errors.Count == 0)
    {
        Console.WriteLine("✅ Valid JSON");
    }
    else
    {
        Console.WriteLine("❌ Invalid JSON");
        foreach (var error in errors)
        {
            Console.WriteLine($"Error: {error.Path} - {error.Kind}");
        }
    }
}

after run this i get 2 times Valid JSON but one should NOT be valid why / what am i missing for this to work?

when i test this in https://www.jsonschemavalidator/ then validaiton error is visible.

regards

Share edited Mar 11 at 7:39 Dorian asked Mar 11 at 7:24 DorianDorian 1,0239 silver badges32 bronze badges 5
  • Quite unrelated / one step back: Wouldn't it be easier to just deserialize and validate the resulting model object's properties? – Fildor Commented Mar 11 at 7:28
  • mayby yes but this project is generic so no classes inside / just json in / out as text and validation this way is needed – – Dorian Commented Mar 11 at 7:39
  • That's weird. Code looks fine to me at first glance. If you can produce a minimal reproducible example, and it is in fact some bug, you should open an issue on their github. This is basically the exact example from their docs, right? – Fildor Commented Mar 11 at 7:40
  • Ah, wait... that's the specs of json schema ... does NJsonSchema actually support the full spec and that version? – Fildor Commented Mar 11 at 7:42
  • this is minimal reproducible example - just copy this to new c# console app and ready to go ... about second - hmm i have last 11.1.0 version, it is writen NJsonSchema is a .NET library to read, generate and validate JSON Schema draft v4+ so i guess 4 up to last? – Dorian Commented Mar 11 at 8:02
Add a comment  | 

1 Answer 1

Reset to default 1

i was blind and now i see.

https://json-schema./draft-04/schema

this version is supported in this nuget. so no condidional .

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744809547a268200.html

最新回复(0)