【问题标题】:How to validate the items of an array with ajv?如何使用 ajv 验证数组的项目?
【发布时间】:2019-08-01 02:00:20
【问题描述】:

我的验证器定义如下

var abcSchemaValidator = {
"type": "object",
"required": [],
"properties": {
    "remarks": {
        "type": "string",
        "maxLength": 2000
    },
    "comment": {
        "type": "string",
        "maxLength": 2000
    }
}
};

在我应用这些验证的代码中,我正在做类似的事情

modelObject.remarks = sometext;

modelObject.parent[0].comment

所以当我使用以下代码运行我的 ajv 验证时

let validate = ajv.compile(schema);
let validResult = validate(data);

评论正确验证,而评论没有。我明白为什么这些评论很简单,但我不知道如何使评论起作用。我应该在 schemaValidator 中将评论更改为 parent.comment 吗?我尝试更改为 parent[0].comment 但这没有用。

【问题讨论】:

    标签: validation jsonschema ajv


    【解决方案1】:

    您的架构没有为parent 定义任何规则,也没有禁止其他属性。您的架构按预期工作。

    据我所知,parent 是:

    1. 对象的非必需但预期的属性
    2. 一个对象数组,每个对象都可以有一个comment 属性,其架构与remarks 相同

    首先,让我们定义一些我们可以重复使用的东西:

    ajv.addSchema({
      $id: 'defs.json',
      definitions: {
        userInput: {
          type: 'string',
          maxLength: 10
        }
      }
    });
    

    那么,让我们用这个通用定义重新定义remarks,定义parent

    const validate = ajv.compile({
      $id: 'main.json',
      type: 'object',
      properties: {
        remarks: {$ref: 'defs.json#/definitions/userInput'},
        parent: {
          type: 'array',
          items: {
            type: 'object',
            properties: {
              comment: {$ref: 'defs.json#/definitions/userInput'}
            }
          }
        }
      }
    });
    

    现在让我们验证一些数据:

    // OK
    console.assert(validate({remarks: 'foo'}),
      JSON.stringify(validate.errors, null, 2));
    
    // ERR: `remarks` is too long
    console.assert(validate({remarks: 'foobarbazbat'}),
      JSON.stringify(validate.errors, null, 2));
    
    // OK: schema doesn't say `parent` can't be empty
    console.assert(validate({remarks: 'foo', parent: []}),
      JSON.stringify(validate.errors, null, 2));
    
    // OK: schema doesn't say `parent` elements MUST have a `comment` property
    console.assert(validate({remarks: 'foo', parent: [{}]}),
      JSON.stringify(validate.errors, null, 2));
    
    // OK
    console.assert(validate({remarks: 'foo', parent: [{comment: 'foo'}]}),
      JSON.stringify(validate.errors, null, 2));
    
    // ERR: `comment` is too long
    console.assert(validate({remarks: 'foo', parent: [{comment: 'foobarbazbat'}]}),
      JSON.stringify(validate.errors, null, 2));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.10.2/ajv.min.js"></script>
    <script>
    const ajv = new Ajv();
    
    ajv.addSchema({
      $id: 'defs.json',
      definitions: {
        userInput: {
          type: 'string',
          maxLength: 10
        }
      }
    });
    
    const validate = ajv.compile({
      $id: 'main.json',
      type: 'object',
      properties: {
        remarks: {$ref: 'defs.json#/definitions/userInput'},
        parent: {
          type: 'array',
          items: {
            type: 'object',
            properties: {
              comment: {$ref: 'defs.json#/definitions/userInput'}
            }
          }
        }
      }
    });
    </script>

    【讨论】:

      猜你喜欢
      • 2016-10-04
      • 1970-01-01
      • 2016-08-23
      • 2017-10-23
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      相关资源
      最近更新 更多