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
i was blind and now i see.
https://json-schema./draft-04/schema
this version is supported in this nuget. so no condidional .