为什么这个来自 json 模式的正则表达式模式没有在 swagger 中验证?
Why is this regex pattern from json schema not validated in swagger?
我正在使用 json 架构来验证我使用 Open API 3.0 的 Swagger 定义的输入。在 Swagger 编辑器中,如果我尝试在数字字段中引入字符串,则验证工作完美。但是,我注意到名称 属性 的模式不起作用,因为当我插入小写字符时,swagger 认为它是有效数据,这是不正确的。
我注意到如果我使用 minLength 来验证字符串的长度,我会遇到同样的问题。此外,json 架构工作正常,因为如果我使用小写字符,它会正确验证 json 对象。
这是我的 Swagger 定义:
openapi: 3.0.0
info:
version: '1.0.0'
title: 'EXAMPLE1'
description: 'Example API to test jsonschema'
termsOfService: https://smasrtbear.com/terms-of-use
contact:
name: something
url: smartbear.com
email: aaa@asdad.net
license:
name: SmartBear License
url: http://license.foo.com
servers:
- url: https://dev.foo.com
description: Dev Server
- url: https://prod.foo.com
description: Prod Server
paths:
/example:
get:
description: To get some information
parameters:
- name: id
in: query
description: Some Id example
schema:
$ref: 'http://localhost:5555/mytest#/properties/id'
- name: name
in: query
description: some name for example
schema:
$ref: 'http://localhost:5555/mytest#/properties/name'
- name: price
in: query
description: some price for example
schema:
$ref: 'http://localhost:5555/mytest#/properties/price'
responses:
200:
description: Successful example
content:
application/json:
schema:
type: array
items:
properties:
id:
type: integer
example: 4
name:
type: string
example: John Smith
price:
type: integer
example: 114
请注意,我正在使用 #ref 连接到远程 json 模式
这是架构。
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The Root Schema",
"required": [
"id",
"name",
"price"
],
"properties": {
"id": {
"$id": "/properties/id",
"type": "integer",
"title": "The Id Schema",
"default": 0,
"examples": [
1
]
},
"name": {
"$id": "/properties/name",
"type": "string",
"title": "The Name Schema",
"default": "",
"examples": [
"A GREEN DOOR"
],
"pattern": "^([A-Z]*)$"
},
"price": {
"$id": "/properties/price",
"type": "number",
"title": "The Price Schema",
"default": 12,
"examples": [
12
]
}
}
}
swagger这个应该是无效的
{
"id": 4,
"name": "abcdefg",
"price": 114
}
会不会是OpenApi3.0的问题?
有什么建议吗?
我从 Swagger Editor 切换到 SwaggerHub。
我正在使用 json 架构来验证我使用 Open API 3.0 的 Swagger 定义的输入。在 Swagger 编辑器中,如果我尝试在数字字段中引入字符串,则验证工作完美。但是,我注意到名称 属性 的模式不起作用,因为当我插入小写字符时,swagger 认为它是有效数据,这是不正确的。
我注意到如果我使用 minLength 来验证字符串的长度,我会遇到同样的问题。此外,json 架构工作正常,因为如果我使用小写字符,它会正确验证 json 对象。
这是我的 Swagger 定义:
openapi: 3.0.0
info:
version: '1.0.0'
title: 'EXAMPLE1'
description: 'Example API to test jsonschema'
termsOfService: https://smasrtbear.com/terms-of-use
contact:
name: something
url: smartbear.com
email: aaa@asdad.net
license:
name: SmartBear License
url: http://license.foo.com
servers:
- url: https://dev.foo.com
description: Dev Server
- url: https://prod.foo.com
description: Prod Server
paths:
/example:
get:
description: To get some information
parameters:
- name: id
in: query
description: Some Id example
schema:
$ref: 'http://localhost:5555/mytest#/properties/id'
- name: name
in: query
description: some name for example
schema:
$ref: 'http://localhost:5555/mytest#/properties/name'
- name: price
in: query
description: some price for example
schema:
$ref: 'http://localhost:5555/mytest#/properties/price'
responses:
200:
description: Successful example
content:
application/json:
schema:
type: array
items:
properties:
id:
type: integer
example: 4
name:
type: string
example: John Smith
price:
type: integer
example: 114
请注意,我正在使用 #ref 连接到远程 json 模式
这是架构。
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The Root Schema",
"required": [
"id",
"name",
"price"
],
"properties": {
"id": {
"$id": "/properties/id",
"type": "integer",
"title": "The Id Schema",
"default": 0,
"examples": [
1
]
},
"name": {
"$id": "/properties/name",
"type": "string",
"title": "The Name Schema",
"default": "",
"examples": [
"A GREEN DOOR"
],
"pattern": "^([A-Z]*)$"
},
"price": {
"$id": "/properties/price",
"type": "number",
"title": "The Price Schema",
"default": 12,
"examples": [
12
]
}
}
}
swagger这个应该是无效的
{ "id": 4, "name": "abcdefg", "price": 114 }
会不会是OpenApi3.0的问题? 有什么建议吗?
我从 Swagger Editor 切换到 SwaggerHub。