I'm using Oat++ framework (https://oatpp.io/) to build a REST API. I want to manage user roles, and I want to have an endpoint to update a single field of a user record - the user role. To achieve this, I've created an end-point:
ENDPOINT("PUT", "/users/{userId}/role/{role}", updateUserRole,
PATH(oatpp::String, userId),
PATH(oatpp::String, role))
{
// TODO - Set user role here.
return createResponse(Status::CODE_200, "OK");
}
Now, I want to validate that the user role has a correct value. Specifically, that its value belongs to the set of allowed values. I could create some sort of a static set with predefined values and check if it contains the received value, but I wonder if there is a better way to this.
I've found that Oatpp has some oatpp::Enum type but I can't find any examples using enum in the endpoint as a path parameter.
So, is it possible to use oatpp::Enum as a path parameter? If yes, then what is the correct way of using it?
Thanks in advance!
Declare an
Enumin the DTO code-gen section:Declare an endpoint:
oatpp::Enumwill do all validations for you.Valid request:
Invalid request:
You can also use
oatpp::Enum<UserRoles>::AsNumberif you want to receive integer values.