我希望能够将答案字段验证为数字值。下面的代码段是一个答案,是更大的答案词典的一部分。每个答案都遵循通用格式,因此答案字段的类型必须为字符串。
"1": {
"answer": "80035",
"web_validated": true,
"web_error_string": "",
"server_error_string": ""
},
这在我们使用JSON模式验证答案字典时造成了一个问题。我们需要将答案字段验证为数字值,这由字典必须遵守的JSON模板确定。以下是字典中针对一个问题的上述答案的模板摘要。
{
"id": "1",
"text": "KFI Number (Null required check)",
"type": "text",
"source": "phoebus",
"kfid_mapping": "KFID000",
"kfid_mapping_value": "",
"valid_answers": null,
"display_online": "readonly",
"required": "1",
"display_internal": "yes",
"hints": null,
"logic": null,
"rules": null,
"reason": null,
"conditional_explanation": null,
"conditional_question_id": null,
"conditional_question_answered": null,
"enabled": "1",
"order": "2",
"fk_section_id": "1",
"validated": false
}
我们用于验证问题ID的当前JSON模式:1。
"definitions": {
"question1-1": {
"type": "object",
"properties": {
"answer": {
"type": "string",
"minLength": 1
}
}
//other definitions removed
}
}
上面是此问题中显示的答案的JSON模式定义。
可能的解决方案:
我希望看看JSON模式是否可行?
答案 0 :(得分:1)
正如其他人提到的,您可以使用模式。这是添加到示例中的语法:
"definitions": {
"question1-1": {
"type": "object",
"properties": {
"answer": {
"type": "string",
"pattern": "^\d+$",
"description": "Use regex to validate this string as a series of one or more digits"
}
}
//other definitions removed
}
}