Njsonschema -зависимость не работает/if/then/else также не работает почему?C#

Место общения программистов C#
Anonymous
Njsonschema -зависимость не работает/if/then/else также не работает почему?

Сообщение Anonymous »

Мне нужно выполнить проверку схемы json
я обнаружил, что njsonschema-хорошо выглядит
valysail works, как и ожидалось, когда валидации типов перечислены и т. Д. />

Код: Выделить всё

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}");
}
}
}

После запуска это я получаю 2 раза действительный json
, но не следует быть действительным
Почему/что мне не хватает, чтобы работать?>

Подробнее здесь: https://stackoverflow.com/questions/794 ... t-work-why

Вернуться в «C#»