Validating incoming create requests

System Information
  • Strapi Version: 3.0.6
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

Hi @all,

in our scenario a website stores data over a strapi endpoint to a Contenttype via POST request. If there is such an request, surley you know, the create controller is being started. The controller uses the incoming data and sends it further to another plattform. Thats the scenario and it works fine. But:
We would like to validate the incoming strapi data before the request body is stored in our content type in strapi to know whether there are invalid entrys made by user at the website or not. And if yes, which field contains this wrong data.
Our first try implementing the EntityValidator fails (Backend customization - Strapi Developer Documentation). So where is a better documentation for using this validator? And the second question is: What other/are there possibilities to check the validity of the incoming request (body)/strapi data to be stored.

Thanks for your answer.

mgords

Hi @mgords, I guess someone can answer this better, but I think inside the create method in the controller you can create your own functions to do the validation as you have access to the context of what is been sent. With this, you can choose what to do and validate it. I’m guessing technically you can create some middleware to do it to.

@Eventyret Thanks for your fast response. One possibility to do as you wrote could be the use of Ajv JSON schema validator. But this means considerable expenditure and more technical risks if something went wrong with the new, own code. So the use of an strapi internal validation function could be the more safer way.

Hi @mgords, I’d like to find a few things out:

Thats the scenario and it works fine. But:
We would like to validate the incoming strapi data before the request body is stored in our content type in strapi to know whether there are invalid entrys made by user at the website or not.

and

Thanks for your fast response. One possibility to do as you wrote could be the use of Ajv JSON schema validator. But this means considerable expenditure and more technical risks if something went wrong with the new, own code.

Are you trying to validate the input manually before it gets saved to the database? Or confirm that the checks Strapi has put in place run as expected?

Hi @Richard_Nsama , sorry for that late answer. Yes, our goal was to validate the input manually before storing in strapi. But at this time checks done by strapi are working fine (for example the check of a iso-compatible date). So we dont want to do anything here at this moment.

1 Like

I see. In future, you could utilize model lifecycles beforeCreate hook to make sure the information in the ctx.request.body (i.e the data being sent in the request) is valid (according to your defined spec)

Thank you very much for that helpful advice. I will take into account these hooks in my future developments.