简体   繁体   English

如何使用 ajv 模式验证来验证值是否为 double 格式和/或类型?

[英]How to verify if a value is of format and/or type double using ajv schema validation?

Im using Ajv version 07.我使用 Ajv 版本 07。

I am trying to validate that the value of a property, returned by the JSON response body is of type and format double in postman, using ajv validation, however, I'm not being able to do it.我正在尝试使用 ajv 验证来验证由 JSON 响应正文返回的属性值在邮递员中的类型和格式是否为 double,但是,我无法做到。 I've tried searching it online, but still did not find anything about it.我试过在网上搜索它,但仍然没有找到任何关于它的信息。

I've tried typing the following:我试过输入以下内容:

  • "type" : "double", “类型”:“双”,

    "format": "double" “格式”:“双”

  • "type": "Double", "type": "双",

    "format": "Double" “格式”:“双”

  • "type":"number" “类型”:“数字”

    "format":"double" “格式”:“双”

All the above attempts were unsuccessful as they all come with an error message saying either:上述所有尝试都没有成功,因为它们都带有一条错误消息:

  • Error: unknown type "double" is used in schema错误:架构中使用了未知类型“double”

or或者

  • Error: unknown format "double" is used in schema错误:架构中使用了未知格式“double”

Would anyone be able to help me with this please?有人能帮我解决这个问题吗?

schema模式

var Ajv = require ('ajv'),
ajv = new Ajv ({logger:console}),
expectedResponseSchema = 
{
    "items": {
        "required": [
            "payments"
     ],
        "properties": {
    "payments": {
                "items": {
                    "required": [
                        "amount"
    ]
        "properties": {
                        "amount": {
                            "$id": "#/items/properties/payments/items/properties/amount",
                            "type": "number",
                            "format": "double"
                        }
   }
  }
 }
}
}

Postman test邮递员测试

var currentSchPmExpTest;

try{     
currentSchPmExpTest = ' expectedResponseSchema variable';
    pm.expect(ajv.validate(expectedResponseSchema, jsonData)).to.be.true;
pm.test('Test 1 - PASSED - expectedResponseSchema variable data matches schema returned by body response!', () => true);
} catch(e){
    pm.test('Test 1 - FAILED - Expected data does not match response body data!', () => {throw new Error(e.message + " in " + currentSchPmExpTest)});
}

body response身体反应


[
  {
    "payments": [
      {
        "amount": 2.200000045367898,

      }
    ]
  }
]

I'm not sure where you're getting the type and format from but as per the AJV docs (could be out of date) that isn't a valid type .我不确定您从何处获取类型和格式,但根据AJV 文档(可能已过时),这不是有效type

在此处输入图片说明

EDIT:编辑:

From your update, I would recommend changing the test script to something like this so that you correct part of the schema is getting checked:根据您的更新,我建议将测试脚本更改为这样的内容,以便您检查正确的部分架构:

let schema = {
    "type": "array",
    "items": {
        "type": "object",
        "required": [
            "payments"
        ],
        "properties": {
            "payments": {
                "type": "array",
                "items": {
                    "type": "object",
                    "required": [
                        "amount"
                    ],
                    "properties": {
                        "amount": {
                            "type": "number",
                        }
                    }
                }
            }
        }
    }
}

pm.test("Check Schema", () => {
    pm.response.to.have.jsonSchema(schema)
}) 

The try/catch block can be added around this if you need too.如果您也需要,可以在此周围添加try/catch块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM