[wp-trac] [WordPress Trac] #46191: REST API parameter validation should handle multiple error messages/codes.

WordPress Trac noreply at wordpress.org
Tue Feb 2 00:12:19 UTC 2021


#46191: REST API parameter validation should handle multiple error messages/codes.
--------------------------------------+-----------------------
 Reporter:  tmfespresso               |       Owner:  (none)
     Type:  defect (bug)              |      Status:  new
 Priority:  normal                    |   Milestone:  5.7
Component:  REST API                  |     Version:  4.4
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:  rest-api
--------------------------------------+-----------------------
Changes (by TimothyBlynJacobs):

 * milestone:  5.8 => 5.7


Comment:

 After much brainstorming with @xkon, we came to the conclusion that
 combining errors like I was in `WP_REST_Request` is `_doing_it_wrong`.
 When you are manually constructing a `WP_Error` object, you wouldn't wind
 up in that scenario since each error check is distinct from each other.

 The issue here is that the `WP_REST_Request` code is trying to be generic
 and combine all the errors from validation even though they are
 unconnected error instances and due to how schema validation works, likely
 to have duplicate error codes.

 So the proper fix for this is to instead account for it when building the
 validation error. Now, the previous example will look like this.

 {{{
 {
   "code": "rest_invalid_param",
   "message": "Invalid parameter(s): enum_a, enum_b",
   "data": {
     "status": 400,
     "params": {
       "enum_a": "enum_a is not one of a, b, c.",
       "enum_b": "enum_b is not one of d, e, f."
     },
     "details": {
       "enum_a": {
         "code": "rest_not_in_enum",
         "message": "enum_a is not one of a, b, c.",
         "data": {
           "enum": ["a", "b", "c"]
         }
       },
       "enum_b": {
         "code": "rest_not_in_enum",
         "message": "enum_b is not one of d, e, f.",
         "data": {
           "enum": ["d", "e", "f"]
         }
       }
     }
   }
 }
 }}}

 The details is built using `rest_convert_error_to_response` so it has a
 consistent error format.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/46191#comment:11>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list