[英]How to verify if a value is of format and/or type double using ajv schema validation?
我使用 Ajv 版本 07。
我正在尝试使用 ajv 验证来验证由 JSON 响应正文返回的属性值在邮递员中的类型和格式是否为 double,但是,我无法做到。 我试过在网上搜索它,但仍然没有找到任何关于它的信息。
我试过输入以下内容:
“类型”:“双”,
“格式”:“双”
"type": "双",
“格式”:“双”
“类型”:“数字”
“格式”:“双”
上述所有尝试都没有成功,因为它们都带有一条错误消息:
或者
有人能帮我解决这个问题吗?
模式
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"
}
}
}
}
}
}
邮递员测试
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)});
}
身体反应
[
{
"payments": [
{
"amount": 2.200000045367898,
}
]
}
]
我不确定您从何处获取类型和格式,但根据AJV 文档(可能已过时),这不是有效type
。
编辑:
根据您的更新,我建议将测试脚本更改为这样的内容,以便您检查正确的部分架构:
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)
})
如果您也需要,可以在此周围添加try/catch
块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.