json 模式不适用于类型 = 数字和模式 = 10 位数字常规模式
json schema not working for type = number and pattern = 10 digit regular pattern
我正在尝试编写一个适用于 phone 数字的 json 模式,它是一个数字,它应该等于 10 位数字。为此,我编写了一个 json 架构,如下所示
{"title":"Schema","description":"Schema","type":"object","properties":{"phone_number":{"type":"number","pattern":"^[0-9]{10}$"}}}.
我的请求json是{"phone_number":123481}
理想情况下,我的 json 模式应该抛出一个异常,告诉我 phone_number 不是 10 位数字,但没有抛出任何错误。有人能告诉我这段代码有什么问题吗?
JSON Schema validation draft spec 在“字符串的验证关键字”下列出了 pattern
。因此,如果您想验证 phone 个数字,则必须将它们记录为 string
,而不是 number
。
换句话说,您的架构应该是
{"title":"Schema","description":"Schema","type":"object","properties":{"phone_number":{"type":"string","pattern":"^[0-9]{10}$"}}}.
并且您的请求 JSON 必须是 {"phone_number":"123481"}
。
在某些地方(尤其是在英国),phone 数字可以以零开头,但 number
类型不能存储前导零。此外,例如 123
和 0123
作为 phone 数字之间可能存在差异,如果您使用 number
类型。
我正在尝试编写一个适用于 phone 数字的 json 模式,它是一个数字,它应该等于 10 位数字。为此,我编写了一个 json 架构,如下所示
{"title":"Schema","description":"Schema","type":"object","properties":{"phone_number":{"type":"number","pattern":"^[0-9]{10}$"}}}.
我的请求json是{"phone_number":123481}
理想情况下,我的 json 模式应该抛出一个异常,告诉我 phone_number 不是 10 位数字,但没有抛出任何错误。有人能告诉我这段代码有什么问题吗?
JSON Schema validation draft spec 在“字符串的验证关键字”下列出了 pattern
。因此,如果您想验证 phone 个数字,则必须将它们记录为 string
,而不是 number
。
换句话说,您的架构应该是
{"title":"Schema","description":"Schema","type":"object","properties":{"phone_number":{"type":"string","pattern":"^[0-9]{10}$"}}}.
并且您的请求 JSON 必须是 {"phone_number":"123481"}
。
在某些地方(尤其是在英国),phone 数字可以以零开头,但 number
类型不能存储前导零。此外,例如 123
和 0123
作为 phone 数字之间可能存在差异,如果您使用 number
类型。