swagger-express-middleware icon indicating copy to clipboard operation
swagger-express-middleware copied to clipboard

Query Parameter Validation should enforce RegEx patterns for non-string parameters

Open mihir83in opened this issue 6 years ago • 3 comments

"parameters" : [ { "name" : "id", "in" : "query", "pattern": "^\\d{5}$", "description" : "id value", "required" : true, "type" : "integer", "format" : "int64" } ],

This parameter never gets validated according to the pattern, I've tried looking into codebase to see if there's something related to pattern and couldnt find it.

The pattern validation works when tried from swagger-ui

mihir83in avatar Oct 16 '18 18:10 mihir83in

Parameters are validated by the parseParameter() function, which ultimately delegates to the jsonValidate() function. JSON Schema validation is done by tv4.

In your case, it appears that the problem is that you have a pattern property on an integer parameter. Patterns only apply to string parameters.

JamesMessinger avatar Oct 16 '18 18:10 JamesMessinger

Parameters are validated by the parseParameter() function, which ultimately delegates to the jsonValidate() function. JSON Schema validation is done by tv4.

In your case, it appears that the problem is that you have a pattern property on an integer parameter. Patterns only apply to string parameters.

Confirmed, the pattern works with string parameters. is there any way to validate integer request parameter with regex pattern ?

mihir83in avatar Oct 16 '18 20:10 mihir83in

Unfortunately, the JSON Schema spec says that the pattern property only applies to string properties. However, I can definitely see an argument that Swagger Express Middleware should support this, since the incoming query parameter is a string until Swagger Express Middleware converts it to an integer.

I'm going to re-open this issue and mark it as an enhancement. No promises as to when I'll get to it though. Although I'd gladly welcome a PR.

JamesMessinger avatar Oct 17 '18 10:10 JamesMessinger