'How can i set body params validation on Wordpress REST API default endpoint?

I know that Wordpress natively provides REST api endpoints such as:

POST /wp/v2/posts
POST /wp/v2/{custom_post_type_slug}

which allows you to create a new post.

I noticed that, while in the backoffice (ADMIN UI) there is a validation (I cannot create a post without defining a title), on the REST API side it is possible to create an article even leaving the request body empty.

I would like to understand how to add validation on some fields to this standard endpoint (for example the title must be mandatory like other fields).

I would like to do this also for other "custom post types", in order to take advantage of the autogeneration of the endpoints for the CRUD (show_in_rest => true), without having to manually register these endpoints via (register_rest_route).

Is it possible to set validation on these endpoints in a simple way without creating custom ones from scratch?

Thanks in advance to everyone!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source