Can I indicate a regular expression requirement fo

2020-08-17 03:24发布

问题:

In my REST API, one of the submitted parameter values must be a code following the regex:

/[A-Z]{2}[0-9]{4}/

Is there any way, besides putting it in the description property of the parameter, for me to indicate that the value is not valid if it does not match my regular expression?

I was not able to find any mention of regex's in the Swagger doc, except as a way to identify Patterned Fields (which is not what I want to do).

回答1:

You can use a field named pattern in your api's parameters if is not a body or in your scheme definitions for properties, it use standard ECMA 262 : (http://json-schema.org/latest/json-schema-validation.html#rfc.section.3.3)

[UPDATE Jul. 2019]

OpenAPI 3 spec - https://swagger.io/docs/specification/data-models/data-types/#pattern



标签: swagger