I have a field which i would like to perform length validation and it is a header parameter. In OpenAPI 3.0.x i tried different keywords like minimum, maximum, maxlength, and pattern. It doesn't work.
The field is a string allowing alphanumeric characters.
clientId:
name: clientId
in: header
description: unique identifier
required: true
schema:
type: string
format: random value of 20 alphanumeric characters
is their a way to add the validation?
I have a field which i would like to perform length validation and it is a header parameter. In OpenAPI 3.0.x i tried different keywords like minimum, maximum, maxlength, and pattern. It doesn't work.
The field is a string allowing alphanumeric characters.
clientId:
name: clientId
in: header
description: unique identifier
required: true
schema:
type: string
format: random value of 20 alphanumeric characters
is their a way to add the validation?
Share Improve this question edited Feb 5 at 13:52 Jeremy Fiel 3,2622 gold badges11 silver badges25 bronze badges asked Feb 5 at 13:34 sharonmsharonm 1271 gold badge1 silver badge8 bronze badges 6- 1 How do you validate ? Because OpenAPI itself does not do anything. Do you use it to generate your code ? – Chasca Commented Feb 9 at 13:25
- Yes we generate the models and controller, we then write the business logic only in service layer. i have added validations for path and query parameters in the swagger and it works. But the header parameters doesnt take the validations included in the file. – sharonm Commented Feb 10 at 10:10
- 1 What server are you using ? You say you use Hibernate Validator, so you programmed the validation ? – Chasca Commented Feb 10 at 10:18
- deployed on physical servers. – sharonm Commented Feb 10 at 10:46
- I meant do you use Spring MVC, jetty, ... ? You say you use Hibernate Validator, how so ? You coded the validation ? – Chasca Commented 2 days ago
1 Answer
Reset to default 0Which validator are you using?
minimum
and maximum
are only for type: integer
or type: number
You can definitely use pattern
for type: string
parameters:
- name: clientId
in: header
required: true
schema:
type: string
maxLength: 20
pattern: '^[a-zA-Z0-9]+$'
alternatively, you can use the content
keyword i/o schema
.
parameters:
- name: clientId
in: header
required: true
content:
'text/plain':
schema:
type: string
maxLength: 20
pattern: '^[a-zA-Z0-9]+$'