【发布时间】:2021-07-22 05:31:46
【问题描述】:
我正在尝试根据 Mongo DB specification (mongo 3.6) 为 Mongo DB 集合名称添加路由时间(在 REST API 建模期间)正则表达式验证。
它说(从上面的文档复制粘贴):
Restriction on Collection Names
Collection names should begin with
- an underscore or a letter character
and cannot:
- contain the $.
- be an empty string (e.g. "").
- contain the null character.
- begin with the system. prefix. (Reserved for internal use.)
我的另一个限制(有点)是:它用于根据json-schema supported subset of the regexp 进行的 JSON 模式验证。它不是完整的正则表达式(例如,我不能使用 \d、\w(或者我不能使用 \b<...>\b)。
有了这个,到目前为止,我可以完成其他部分
- begin with the system. prefix. (Reserved for internal use.)
部分。
这是我目前在我的 REST API JSON 模式中的正则表达式(请参阅下面的模式):
'collectionName': {
description: "foo bar",
type: 'string',
minLength: 1,
maxLength: 120,
pattern: '(^[a-zA-Z0-9_][^$ \\0]*$)', <== this one.
example: 'MyCollection',
},
举几个例子进一步说明:
- 它不应该匹配类似-collection的东西,但应该匹配 _collection或collection
- 字符串包含空格或 $ - 不应匹配(例如,collection、collec$$tion)
- 类似这样的东西应该匹配:systemCollection
- 这不应匹配:system.collection // 作为系统。前缀是为 Mongo 保留的。
尽我所能以最好的方式澄清问题。
在这方面的任何帮助将不胜感激。
普拉迪普
【问题讨论】:
-
你能澄清一下什么没有按预期工作吗?我看不到您希望验证失败但实际未通过的输入示例。
-
整个 '- 从系统开始。字首。'正则表达式中没有规则。意思是:我之前提到的第 4 点,不应该匹配。请参考本节:“用几个例子进一步澄清”。
标签: regex jsonschema