Hi everyone,
I’ve created a POST endpoint that receives an object as a parameter, and everything is working well so far.
Now, I’d like to add validations (such as pattern, required, etc.) to specific fields in the object, based on a schema that I already have — but I can’t figure out how to properly add them to the API.
A screenshot is attached for reference.
I’d really appreciate your help, thanks in advance!
Hello hava,
The swagger is generated automatically by the platform, as such you don't need to define any validations on the swagger itself.
Unfortunately, I'm afraid it is not possible to do what you pretend. Like mentioned before, the only thing you can do is define mandatory fields.
Other than that (regex, values ranges, etc.), you'll have to implement these validations on the server side and document them manually if you need to.
Kind regards,
Rui Barradas
Hope you're doing well.
If you double click on the method (SetEmpData), you will be able to define the logic of the API.
There you can add validations, verify specific fields, call server actions, and place your logic.
What type of validations do you pretend to implement?
Hi, thank you for your response!
What I’d like to implement are validations like:
Making certain fields required
Applying a regex pattern to specific fields (e.g., validating a national ID format, min or max length)
I already have a schema that defines these validation rules, and I’m trying to apply them either directly in the input parameter definition or inside the logic of the API.
I did double-click on the SetEmpData method and defined the logic I want to execute based on the input received from the POST request — that part is working fine.
My question is:
Is there a way to apply these validations declaratively (for example, through input field settings or attributes), or do I need to handle all validations manually in the logic?
Thanks again!
Hi @hava sh,
As far as I know, you need to write the validations in the API manually.
Hope this helps
Thanks
Thanks for the clarification!
Just to make sure I'm doing this right —
Where exactly should I write the validations?
In the SetEmpData method, I currently have logic that runs after I receive the data from the POST request (e.g., saving the data, processing it, etc.).
If you could share a quick example, that would be really helpful.
You can add the validation in the SetEmpData method before executing any data saving or processing. Once all validation conditions are met, you can proceed with saving or processing the data.
Similarly, you can implement the validations first and then add the logic afterward.
Hi, Thank you for your previous response.
I just want to clarify something – will the server-side validations (such as required checks, regex, or value ranges) that we have in the API also appear in Swagger UI, or do I need to define the validations in Swagger separately? I need the validations to be displayed in Swagger UI, not only on the server side.
Is there a way to make this happen in Swagger so that developers can see the validations at the documentation level and not only when making the API calls?
Thanks a lot!
For instance, if we look into the screenshot of POST request in Swagger, it indicates that the field is required. This signifies that it is a mandatory field.