JSON模式验证-比较单独模式中的日期

时间:2019-07-19 11:58:57

标签: json jsonschema json-schema-validator ajv

我有一个正在使用JSON模式建模的调查表。每个架构都使用AJV进行了验证。我的“问卷”对象如下所示。 “部分”包含用于单独呈现的问题的模式,“路线”用于确定下一个要路由到的页面,“答案”是当用户发布问题的答案时填充的对象。

"type": "apply-for-stuff",
"version": "0.0.1",
"sections": {
    "when-was-it-ordered": {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "object",
        "required": [
            "q-when-was-it-ordered"
        ],
        "additionalProperties": false,
        "properties": {
            "q-when-was-it-ordered": {
                "type": "string",
                "format": "date-time",
                "title": "When was it ordered?",
                "description": "For example, 31 3 2018. You can enter an approximate date.",
                "errorMessage": {
                    "format": "The date it was ordered must be in the past"
                }
            }
        },
        "errorMessage": {
            "required": {
                "q-when-was-it-ordered": "Enter the date it was ordered"
            }
        }
    },
    "when-did-it-arrive": {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "object",
        "required": [
            "q-applicant-when-did-it-arrive"
        ],
        "additionalProperties": false,
        "properties": {
            "q-when-did-it-arrive": {
                "type": "string",
                "format": "date-time",
                "title": "When did it arrive?",
                "description": "For example, 31 3 2018. You can enter an approximate date.",
                "errorMessage": {
                    "format": "The date it arrived must be in the past"
                }
            }
        },
        "errorMessage": {
            "required": {
                "q-when-did-it-arrive": "Enter the date the it arrived"
            }
        }
    },
    "date-of-birth": {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "object",
        "required": [
            "q-when-did-it-start"
        ],
        "additionalProperties": false,
        "properties": {
            "q-when-did-it-start": {
                "type": "string",
                "format": "date-time",
                "title": "When did it start?",
                "description": "For example, 03 2018. You can enter an approximate date.",
                "errorMessage": {
                    "format": "Enter the date it started"
                }
            }
        },
        "errorMessage": {
            "required": {
                "q-when-did-it-start": "Enter the date it started"
            }
        }
    }
},
"routes": {
    "states": {
        "when-was-it-ordered": {
            "on": {
                "ANSWER": [
                    {
                        "target": "another-question"
                    }
                ]
            }
        },
        "applicant-when-did-it-arrive": {
            "on": {
                "ANSWER": [
                    {
                        "target": "another-question"
                    }
                ]
            }
        },
        "date-of-birth": {
            "on": {
                "ANSWER": [
                    {
                        "target": "another-question"
                    }
                ]
            }
        }
    }
},
"answers": {
    "when-was-it-ordered": {
        "q-when-was-it-ordered": "2019-01-01T14:48:00.000Z"
    },
    "when-did-it-arrive": {
        "q-when-did-it-arrive": "2019-01-03T14:48:00.000Z"
    },
    "date-of-birth": {
        "q-date-of-birth": "2000-01-01T14:48:00.000Z"
    }
}

此信息包含在服务中,该服务将每个问题分别发送到另一个服务,另一个服务将呈现输出并将其显示给用户。

在上面的示例中,我包含了3个“日期”类型的问题。我正在寻找一种比较这些日期并强制执行验证规则的优雅方法。例如,我希望为“当到达时”输入的值在“当它被排序时”之后,否则AJV将无法通过验证和错误。

我找到了先前回答的问题,几乎可以解决我的问题here。 但是,此解决方案似乎并不立即适合我的问题,因为每个“部分”都是单独进行验证的-如果用户尝试导航到“到达时”,则验证者不知道问卷中的其他任何内容(“到达时”的模式除外:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": [
        "q-applicant-when-did-it-arrive"
    ],
    "additionalProperties": false,
    "properties": {
        "q-when-did-it-arrive": {
            "type": "string",
            "format": "date-time",
            "title": "When did it arrive?",
            "description": "For example, 31 3 2018. You can enter an approximate date.",
            "errorMessage": {
                "format": "The date it arrived must be in the past"
            }
        }
    },
    "errorMessage": {
        "required": {
            "q-when-did-it-arrive": "Enter the date the it arrived"
        }
    }
}

以及用户发布的答案:

"when-did-it-arrive": {
    "q-when-did-it-arrive": "2019-01-03T14:48:00.000Z"
}

任何有关如何进行这种类型验证的建议都将受到赞赏。

0 个答案:

没有答案