Reputation: 377
I'm using OpenApi specification, this is an example of code that generates the class:
CreateUsersRequest:
required:
- userIds
type: object
properties:
userIds:
uniqueItems: true
minItems: 1
type: array
items:
type: string
format: uuid
I want to prohibit sending the following request:
{
"userIds": [
""
]
}
{
"userIds": [
null
]
}
In case I used javax.validation
it would look like:
@NotNull
private List<@NotEmpty UUID> userIds;
Is there any ways to do the same using api.yml
file ?
Upvotes: 2
Views: 2971
Reputation: 53441
As indicated in the documentation:
OpenAPI 3.0 does not have an explicit
null
type as in JSON Schema, but you can usenullable: true
to specify that the value may benull
. Note thatnull
is different from an empty string""
.
To prevent the empty string you could provide a pattern
regex.
Please, try the following:
CreateUsersRequest:
required:
- userIds
type: object
properties:
userIds:
uniqueItems: true
minItems: 1
type: array
items:
type: string
format: uuid
nullable: false
pattern: ^[a-z|\-]+$
Please, test the code carefully, I realized you used format: uuid
, so I am not absolutely confident about the combination of format
and pattern
.
If necessary, you could try providing your own uuid
type. For example:
Uuid:
type: string
pattern: '^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$'
nullable: false
And:
CreateUsersRequest:
required:
- userIds
type: object
properties:
userIds:
uniqueItems: true
minItems: 1
type: array
items:
$ref: "#/components/schemas/Uuid"
Upvotes: 1