JsonSchema.Net 在遇到第一个具有不适当错误的属性时停止验证。

huangapple go评论74阅读模式
英文:

JsonSchema.Net stops validation at first property with inappropriate error

问题

我使用 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));

而且错误总是相同的:

Value is "string" but should be "object" at #/type

真的不明白为什么。我想也许在 JSON 模式的顶部和作为必需参数的 type 之间存在冲突。但即使我从参数中移除了 type,仍然出现相同的错误。

有什么问题吗?

英文:

I have JSON schema generated using JsonSchema.Net.Generation. My classes for schema are as below:

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

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

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

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

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

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

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

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

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

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

and it's the generated schema:

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

I created sample function to validate given 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 + &quot; at &quot; + result.SchemaLocation.Source);

    return result.IsValid;
}

And it's always false for this call:

var schemaBuilder = new JsonSchemaBuilder();
var schema = schemaBuilder.FromType&lt;PaymentInitiationSchema&gt;().Build();

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

And the error is also always the same:
> Value is "string" but should be "object" at #/type

Really don't understand why. I thought that maybe there is conflict between type at top of the JSON scheme and the type as required parameter. But even if I removed type from parameters the same error stills occurs.

What's wrong with it?

答案1

得分: 1

请注意,以下是您提供的文本的翻译:

"I'm not sure what version of the libs you're using, but have you checked over at https://json-everything.net?

Inputting your generated schema and the instance you wrote, it says that two properties are the problems (I've edited the output down to just the errors for ease of viewing):

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

It looks like you have two typos. Changing remittanceInformationUnstructured to reemitanceInformationUnstructured and ban to iban allows the data to validate."

英文:

I'm not sure what version of the libs you're using, but have you checked over at https://json-everything.net?

Inputting your generated schema and the instance you wrote, it says that two properties are the problems (I've edited the output down to just the errors for ease of viewing):

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

It looks like you have two typos. Changing remittanceInformationUnstructured to reemitanceInformationUnstructured and ban to iban allows the data to validate.

huangapple
  • 本文由 发表于 2023年2月23日 19:38:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定