JsonSchema.Net在出现不适当错误的第一个属性处停止验证

46qrfjad  于 2023-02-26  发布在  .NET
关注(0)|答案(2)|浏览(140)

我使用JsonSchema.Net.Generation生成了JSON模式,我的模式类如下:

class PaymentInitiationSchema
{
    [Required]
    [JsonPropertyName("type")]
    public string Type { get; set; }

    [Required]
    [JsonPropertyName("actions")]
    public string[] Actions { get; set; }

    [Required]
    [JsonPropertyName("locations")]
    public string[] Locations { get; set; }

    [Required]
    [JsonPropertyName("instructedAmount")]
    public InstructedAmount InstructedAmount { get; set; }

    [Required]
    [JsonPropertyName("creditorName")]
    public string CreditorName { get; set; }

    [Required]
    [JsonPropertyName("creditorAccount")]
    public CreditorAccount CreditorAccount { get; set; }

    [Required]
    [JsonPropertyName("reemitanceInformationUnstructured")]
    public string ReemitanceInformationUnstructured { get; set; }
}

class InstructedAmount
{
    [Required]
    [JsonPropertyName("currency")]
    public string Currency { get; set; }

    [Required]
    [JsonPropertyName("amount")]
    public decimal Amount { get; set; }
}

class CreditorAccount
{
    [Required]
    [JsonPropertyName("iban")]
    public string Iban { get; set; }
}

它是生成的模式:

{
   "type":"object",
   "properties":{
      "type":{
         "type":"string"
      },
      "actions":{
         "$ref":"#/$defs/array"
      },
      "locations":{
         "$ref":"#/$defs/array"
      },
      "instructedAmount":{
         "type":"object",
         "properties":{
            "currency":{
               "type":"string"
            },
            "amount":{
               "type":"number"
            }
         },
         "required":[
            "currency",
            "amount"
         ]
      },
      "creditorName":{
         "type":"string"
      },
      "creditorAccount":{
         "type":"object",
         "properties":{
            "iban":{
               "type":"string"
            }
         },
         "required":[
            "iban"
         ]
      },
      "reemitanceInformationUnstructured":{
         "type":"string"
      }
   },
   "required":[
      "type",
      "actions",
      "locations",
      "instructedAmount",
      "creditorName",
      "creditorAccount",
      "reemitanceInformationUnstructured"
   ],
   "$defs":{
      "array":{
         "type":"array",
         "items":{
            "type":"string"
         }
      }
   }
}

我创建了示例函数来验证给定的JSON:

static bool IsValid(string requestedAuthorizationDetails, JsonSchema authorizationDetailsSchema)
{
    try
    {
        JsonDocument.Parse(requestedAuthorizationDetails);
    }
    catch
    {
        return false;
    }

    var result = authorizationDetailsSchema.Validate(requestedAuthorizationDetails, new ValidationOptions
    {
        OutputFormat = OutputFormat.Detailed
    });

    Console.WriteLine(result.Message + " at " + result.SchemaLocation.Source);

    return result.IsValid;
}

这个调用总是false:

var schemaBuilder = new JsonSchemaBuilder();
var schema = schemaBuilder.FromType<PaymentInitiationSchema>().Build();

Console.WriteLine(IsValid(@"{
      ""type"": ""payment_initiation"",
      ""actions"": [
         ""initiate"",
         ""status"",
         ""cancel""
      ],
      ""locations"": [
         ""https://example.com/payments""
      ],
      ""instructedAmount"": {
         ""currency"": ""EUR"",
         ""amount"": 123.50
      },
      ""creditorName"": ""Merchant A"",
      ""creditorAccount"": {
         ""iban"": ""DE02100100109307118603""
      },
      ""remittanceInformationUnstructured"": ""Ref Number Merchant""
   }", schema));

而且误差也总是一样的:
值为"string",但在#/type处应为"object"
真的不明白为什么。我想可能是JSON方案顶部的type和作为必需参数的type之间有冲突。但是即使我从参数中删除了type,仍然会出现同样的错误。
有什么不对吗?

0mkxixxg

0mkxixxg1#

我不确定您使用的是什么版本的库,但是您是否检查过https://json-everything.net
输入您生成的模式和您编写的示例,它指出有两个属性存在问题(为了便于查看,我将输出编辑为只有错误):

{
  "valid": false,
  "evaluationPath": "",
  "schemaLocation": "https://json-everything.net/43e8b7754c",
  "instanceLocation": "",
  "errors": {
    "required": "Required properties [\"reemitanceInformationUnstructured\"] were not present"
  },
  "details": [
    {
      "valid": false,
      "evaluationPath": "/properties/creditorAccount",
      "schemaLocation": "https://json-everything.net/43e8b7754c#/properties/creditorAccount",
      "instanceLocation": "/creditorAccount",
      "errors": {
        "required": "Required properties [\"iban\"] were not present"
      }
    }
  ]
}

看起来您有两处打字错误。将remittanceInformationUnstructured更改为reemitanceInformationUnstructured,将ban更改为iban可以验证数据。

ne5o7dgx

ne5o7dgx2#

首先是现场
“汇款信息非结构化”
应该在您创建的json中重命名为:

偿付信息非结构化

第二,JsonSchema已经过时了,您应该使用最新版本的验证。并且请在json中的每个元素使用大写字母。(这是因为您使用了不同的库作为属性名称和验证)

JSchemaGenerator generator = new JSchemaGenerator();
        JSchema schema = generator.Generate(typeof(PaymentInitiationSchema));

        JObject paymentInitation = JObject.Parse(requestedAuthorizationDetails);

        bool valid = paymentInitation.IsValid(schema);

如果您仍然希望它能与您的小写版本一起工作,那么您应该使用相同的库来实现命名约定。
不使用:

[JsonPropertyName("type")]

用途(对于每个属性)

[JsonProperty("type")]

并且它也可以使用小写。

相关问题