【问题标题】:How can I compile a subset of a JSON Schema with Ajv?如何使用 Ajv 编译 JSON Schema 的子集?
【发布时间】:2020-01-28 15:22:52
【问题描述】:

我必须遵循 JSON 架构:

{
  "type": "object",
  "required": [
    "type",
    "title"
  ],
  "properties": {
    "type": {
      "enum": [
        "journal",
        "book",
        "generic"
      ]
    },
    "title": {
      "type": "string",
      "minLength": 1,
      "maxLength": 500
    }
  }
}

根据该架构,以下对象是有效的:

{type: 'journal', title: 'foo'}

而这个不是:

{type: 'qux', title: ''}

有没有一种方法可以重用该架构的子集,以便我可以单独验证类型和标题?

例如(伪代码)

const Ajv = require('ajv');
const ajv = new Ajv;

ajv.addSchema(/* see schema above */);

const validate_document = ajv.compile(/* see schema above */);
const validate_type = ajv.compile(/* reuse corresponding subset */);
const validate_title = ajv.compile(/* reuse corresponding subset */);

validate_document({type: 'qux', title: ''}); // false
validate_type('qux'); // false
validate_title(''); // false

【问题讨论】:

    标签: javascript jsonschema ajv


    【解决方案1】:

    我选择了这个解决方案,它允许我保持架构“完整性”(我不需要将其拆分为更小的部分,以便它易于阅读和理解)。 我需要做的就是在相关架构上设置 $id 键:

    {
      "$id": "document",          <-- id for the whole schema
      "type": "object",
      "required": [
        "type",
        "title"
      ],
      "properties": {
        "type": {
          "$id": "document-type", <-- id for the type schema
          "enum": [
            "journal",
            "book",
            "generic"
          ]
        },
        "title": {
          "$id": "document-title", <-- id for the title schema
          "type": "string",
          "minLength": 1,
          "maxLength": 500
        }
      }
    }
    

    Ajv 允许您通过 Ajv#getSchema 中的 id 引用架构,它将架构编译为验证函数:

    const validate_document = ajv.getSchema('document');
    const validate_type = ajv.getSchema('document-type');
    const validate_title = ajv.getSchema('document-title');
    
    console.log(
        'Is {type: "journal", title: "foo"} valid? '
      , validate_document({type: "journal", title: "foo"})
    ); // true
    
    console.log(
        'Is "qux" a valid document type? '
      , validate_type("qux")
    ); // false
    
    console.log(
        'Is "journal" a valid document type? '
      , validate_type("journal")
    ); // true
    
    console.log(
        'Is "" a valid document title? '
      , validate_title("")
    ); // false
    
    console.log(
        'Is "foo" a valid document title? '
      , validate_title("foo")
    ); // true
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.11.0/ajv.min.js"></script>
    <script>
    const ajv = new Ajv;
    
    ajv.addSchema({
      "$id": "document",
      "type": "object",
      "required": [
        "type",
        "title"
      ],
      "properties": {
        "type": {
          "$id": "document-type",
          "enum": [
            "journal",
            "book",
            "generic"
          ]
        },
        "title": {
          "$id": "document-title",
          "type": "string",
          "minLength": 1,
          "maxLength": 500
        }
      }
    });
    
    </script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-19
      • 2018-11-25
      • 2015-06-01
      • 2019-04-26
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 2019-04-08
      相关资源
      最近更新 更多