【发布时间】:2020-11-30 21:29:36
【问题描述】:
我正在使用正则表达式来验证 json 模式中的属性。
架构如下:
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"required": [
"library_version"
],
"properties": {
"library_version": {
"$id": "#/properties/library_version",
"type": "string",
"title": "Library version",
"description": "The library version (e.g. 0.0.1) used to create this file.",
"pattern": "^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$",
"message": {
"required": "Library version is a required property",
"pattern": "The version must be a semantic form, like 0.0.1 (see https://semver.org/)"
},
"examples": [
"0.0.1"
]
}
}
}
大的正则表达式用于semver - c.f. here。
使用jsonlint 我得到:
Error: Parse error on line 14:
...le.", "pattern": "^(0|[1-9]\d*)\.(0|[
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
当然我不必在 json 模式中转义正则表达式模式?字符串应该被视为文字,对吧?有谁知道这里到底发生了什么?
【问题讨论】:
标签: json regex schema jsonschema