【问题标题】:How to set multiple types for a JsonSchema field in Json.Net?如何为 Json.Net 中的 JsonSchema 字段设置多种类型?
【发布时间】:2014-09-04 23:27:36
【问题描述】:

如何在 JsonSchema 中设置多种类型。 下面示例中的描述字段我希望它是 JsonSchemaType.String 或 JsonSchemaType.null。

 {PropertyNames.Id, new JsonSchema { Type = JsonSchemaType.Integer, Required = true }},
      {PropertyNames.Description, new JsonSchema { Type = JsonSchemaType.String, Required = true }}

另外,我有一个由整数和浮点数组成的数组。

 result[PropertyNames.Metrics] = new JsonSchema { Type = JsonSchemaType.Array, Required = true, Items = new List<JsonSchema> { new JsonSchema() { Type = JsonSchemaType.Integer } } };

验证失败,因为它需要整数但接收浮点数。我可以做一些像 Type = JsonSchemaType.Integer "or" JsonSchemaType.Float

【问题讨论】:

  • 不要将您的 Google 搜索放在问题的标题中。 把你要问的问题放在标题中。

标签: json schema json.net jsonschema json-schema-validator


【解决方案1】:

可以使用简单的| 运算符来完成

result[PropertyNames.Metrics] = new JsonSchema 
{ 
    Type = JsonSchemaType.Array, 
    Required = true, 
    Items = new List<JsonSchema> 
    { 
        new JsonSchema() 
        { 
            Type = JsonSchemaType.Integer | JsonSchemaType.Null
        } 
    }
};

【讨论】:

    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 2016-11-26
    • 2023-03-17
    • 2014-04-21
    • 2019-03-25
    • 2022-11-21
    • 2012-10-10
    • 1970-01-01
    相关资源
    最近更新 更多