You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用tv4校验JSON schema时数组内部属性规则不生效问题求助

错误原因

  1. Schema 语法错误
    JSON Schema 中针对数组类型的子元素校验,必须通过items字段定义规则,你当前直接在data(数组类型)的配置下写properties,校验器不会识别为数组元素的校验规则,自然不会校验data数组内的字段类型。
  2. 测试代码逻辑缺陷
  • 变量初始化顺序错误:第一次调用tv4.validateMultiple时,jsonData还未完成赋值
  • 仅打印校验错误,没有主动触发测试失败逻辑,哪怕校验不通过用例也会显示为通过

修正后代码

修正后的Schema

const schema = {
    "type": "object",
    "properties": {
        "current_page": {
            "type": "integer"
        },
        "data": {
            "type": "array",
            // 数组元素的规则放在items下
            "items": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "integer"
                    },
                    "first_name": {
                        "type": "string"
                    },
                    "last_name": {
                        "type": "string"
                    },
                    "telephone": {
                        "type": "string"
                    },
                    "postcode": {
                        "type": "string"
                    },
                    "date_of_birth": {
                        "type": "string"
                    },
                    "profession": {
                        "type": "string"
                    },
                    "time_served": {
                        "type": "integer"
                    },
                    "national_insurance_number": {
                        "type": "string"
                    },
                    "employer": {
                        "type": "string"
                    },
                    "lco_id": {
                        "type": "integer"
                    },
                    "company_employed": {
                        "type": "null"
                    },
                    "company_employed_email": {
                        "type": "null"
                    },
                    "company_employed_telephone": {
                        "type": "string"
                    },
                    "company_employed_address": {
                        "type": "null"
                    },
                    "apprentice": {
                        "type": "boolean"
                    },
                    "apprentice_trade": {
                        "type": "string"
                    },
                    "apprentice_course": {
                        "type": "string"
                    },
                    "apprentice_started_at": {
                        "type": "string"
                    },
                    "apprentice_ended_at": {
                        "type": "string"
                    },
                    "work_experience": {
                        "type": "boolean"
                    },
                    "work_experience_trade": {
                        "type": "null"
                    },
                    "work_experience_education": {
                        "type": "null"
                    },
                    "work_experience_started_at": {
                        "type": "null"
                    },
                    "work_experience_ended_at": {
                        "type": "null"
                    },
                    "nvq": {
                        "type": "boolean"
                    },
                    "nvq_trade": {
                        "type": "string"
                    },
                    "nvq_education": {
                        "type": "string"
                    },
                    "nvq_started_at": {
                        "type": "string"
                    },
                    "nvq_ended_at": {
                        "type": "string"
                    },
                    "unemployed": {
                        "type": "boolean"
                    },
                    "unemployed_months": {
                        "type": "null"
                    },
                    "company_employed_postcode": {
                        "type": "string"
                    },
                    "partner_relationship": {
                        "type": "null"
                    },
                    "self_partner_relationship": {
                        "type": "null"
                    },
                    "emergency_contact_first_name": {
                        "type": "string"
                    },
                    "emergency_contact_last_name": {
                        "type": "string"
                    },
                    "emergency_contact_telephone": {
                        "type": "string"
                    },
                    "enrollment_id": {
                        "type": "integer"
                    },
                    "created_at": {
                        "type": "string"
                    },
                    "updated_at": {
                        "type": "string"
                    },
                    "mode_of_travel": {
                        "type": "string"
                    },
                    "driver_or_passenger": {
                        "type": "string"
                    },
                    "fuel_type": {
                        "type": "string"
                    },
                    "engine_capacity": {
                        "type": "string"
                    },
                    "rtw_declaration": {
                        "type": "boolean"
                    },
                    "rtw_proof1_upload_id": {
                        "type": "null"
                    },
                    "rtw_proof2_upload_id": {
                        "type": "null"
                    },
                    "card_type": {
                        "type": "string"
                    },
                    "gender": {
                        "type": "string"
                    },
                    "self_gender": {
                        "type": "null"
                    },
                    "marital_status": {
                        "type": "string"
                    },
                    "disability_act": {
                        "type": "string"
                    },
                    "disability_description": {
                        "type": "null"
                    },
                    "ethnic_origin": {
                        "type": "string"
                    },
                    "religion": {
                        "type": "string"
                    },
                    "nationality": {
                        "type": "string"
                    },
                    "sexual_orient": {
                        "type": "string"
                    },
                    "checked_membership": {
                        "type": "integer"
                    },
                    "training_checked": {
                        "type": "integer"
                    },
                    "enrollment": {
                        "type": "object",
                        "properties": {
                            "id": {
                                "type": "integer"
                            },
                            "inducted": {
                                "type": "boolean"
                            },
                            "user_id": {
                                "type": "integer"
                            },
                            "created_at": {
                                "type": "string"
                            },
                            "updated_at": {
                                "type": "string"
                            },
                            "expiry_date": {
                                "type": "string"
                            },
                            "user": {
                                "type": "object",
                                "properties": {
                                    "id": {
                                        "type": "integer"
                                    },
                                    "email": {
                                        "type": "string"
                                    },
                                    "role": {
                                        "type": "integer"
                                    },
                                    "created_at": {
                                        "type": "string"
                                    },
                                    "updated_at": {
                                        "type": "string"
                                    },
                                    "state": {
                                        "type": "integer"
                                    },
                                    "last_login_at": {
                                        "type": "null"
                                    }
                                },
                                "required": [
                                    "id",
                                    "email",
                                    "role",
                                    "created_at",
                                    "updated_at",
                                    "state",
                                    "last_login_at"
                                ]
                            }
                        },
                        "required": [
                            "id",
                            "inducted",
                            "user_id",
                            "created_at",
                            "updated_at",
                            "expiry_date",
                            "user"
                        ]
                    }
                },
                "required": [
                    "id",
                    "first_name",
                    "last_name",
                    "telephone",
                    "postcode",
                    "date_of_birth",
                    "profession",
                    "time_served",
                    "national_insurance_number",
                    "employer",
                    "lco_id",
                    "company_employed",
                    "company_employed_email",
                    "company_employed_telephone",
                    "company_employed_address",
                    "apprentice",
                    "apprentice_trade",
                    "apprentice_course",
                    "apprentice_started_at",
                    "apprentice_ended_at",
                    "work_experience",
                    "work_experience_trade",
                    "work_experience_education",
                    "work_experience_started_at",
                    "work_experience_ended_at",
                    "nvq",
                    "nvq_trade",
                    "nvq_education",
                    "nvq_started_at",
                    "nvq_ended_at",
                    "unemployed",
                    "unemployed_months",
                    "company_employed_postcode",
                    "partner_relationship",
                    "self_partner_relationship",
                    "emergency_contact_first_name",
                    "emergency_contact_last_name",
                    "emergency_contact_telephone",
                    "enrollment_id",
                    "created_at",
                    "updated_at",
                    "mode_of_travel",
                    "driver_or_passenger",
                    "fuel_type",
                    "engine_capacity",
                    "rtw_declaration",
                    "rtw_proof1_upload_id",
                    "rtw_proof2_upload_id",
                    "card_type",
                    "gender",
                    "self_gender",
                    "marital_status",
                    "disability_act",
                    "disability_description",
                    "ethnic_origin",
                    "religion",
                    "nationality",
                    "sexual_orient",
                    "checked_membership",
                    "training_checked",
                    "enrollment"
                ]
            }
        }
    }
}

修正后的测试代码

pm.test("Validate schema contains the relevant details", () => {
    // 先解析响应内容
    const jsonData = JSON.parse(responseBody);
    // 执行校验
    const validationResult = tv4.validateMultiple(jsonData, schema, true, true);
    // 打印错误日志
    validationResult.errors.forEach(err => {
        console.log(`path :${err.dataPath} message :${err.message}`);
    })
    // 校验不通过时主动触发测试失败
    pm.expect(validationResult.errors.length).to.equal(0, "Schema校验不通过,详情查看控制台日志");
});

修改完成后再调整数组内的字段类型,即可正常触发测试失败。


内容的提问来源于stack exchange,提问作者user16295980

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 20:06:03