是否可以将excludes属性与cerberus的默认属性组合在一起?

时间:2019-04-29 16:11:30

标签: python cerberus

当我使用cerberus验证数据输入时,我遇到了一个问题,因为我使用默认键来配置值,但是在同一模式下,我使用具有该属性的另一个字段排除了指向具有默认值的字段。

这是我正在使用的架构的配置:

# -*- coding: utf-8 -*-
# !/usr/bin/env python3

from src.helpers.date_helper import DateHelper


initial_date = DateHelper().get_first_day_of_working_week()

schema = {
    "initial_date": {
        "type": "string",
        "regex": r"\d{4}[-/]\d{2}[-/]\d{2}",
        "default": initial_date,
        "dependencies": "final_date",
    },
    "final_date": {
        "type": "string",
        "regex": r"\d{4}[-/]\d{2}[-/]\d{2}",
        "default": DateHelper().get_final_day_of_working_week(initial_date),
        "dependencies": "initial_date"
    },
    "days": {
        "type": "integer",
        "default": "7",
        "coerce": lambda x: int(x),
        "excludes": ["final_date", "initial_date"],
    },
    "latitude": {
        "type": "number",
        "required": True,
        "coerce": lambda x: float(x),
        "dependencies": "longitude",
    },
    "longitude": {
        "type": "number",
        "required": True,
        "coerce": lambda x: float(x),
        "dependencies": "latitude",
    },
}

当我针对此配置执行验证时: {"days": "15", "latitude": "-9.97","longitude": "-67.81"}

我收到的回复是:{'days': ["'final_date', 'initial_date' must not be present with 'days'"]}

我想知道如何在不更改验证引擎的情况下使用默认值和排除属性。

0 个答案:

没有答案