[wp-trac] [WordPress Trac] #56483: Weird oneOf behaviour upon validation of post meta value upon GET retrieval via REST API

WordPress Trac noreply at wordpress.org
Mon Sep 12 01:01:43 UTC 2022


#56483: Weird oneOf behaviour upon validation of post meta value upon GET retrieval
via REST API
-------------------------------+------------------------------
 Reporter:  joeyojoeyo12       |       Owner:  (none)
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  REST API           |     Version:  6.0.1
 Severity:  major              |  Resolution:
 Keywords:  reporter-feedback  |     Focuses:  rest-api
-------------------------------+------------------------------

Comment (by TimothyBlynJacobs):

 I'm sorry you're experiencing issues @joeyojoeyo12. Could you try and
 simplify your replication example to the bare minimum required to trigger
 the error? It is a bit difficult to follow such a large example.

 To clarify a couple of things. Yes, when using `oneOf`, the REST API
 ensures that ''exactly'' one schema matches the input value. If more than
 one schema matches successfully, then the validation will fail. If this
 isn't the behavior you are looking for, try using the `anyOf` keyword.
 This indicates that you want to match ''any'' of the provided schemas.

 Can you clarify where you saw `oneOf` overwritten to `anyOf`? As far as I
 known, we never assign a value of `anyOf` to a schema.

 Omitting `type` when setting your schema in `show_in_rest` won't actually
 remove the `type` definition because
 `WP_REST_Meta_Fields::get_registered_fields` defaults the `type` keyword
 to the top-level `type` you set in `register_meta`.

 While not required, specifying a `type` in addition to a `oneOf` or
 `anyOf` keyword is supported. However, that `type` must then be valid for
 all schemas used in `oneOf` or `anyOf`.

 Validation is supposed to continue after the `oneOf` or `anyOf` keywords
 are processed successfully. So if you are registering a meta key where we
 default `additionalProperties` to be `false`, then it would be expected
 behavior that your schema validation fails. I believe you should be able
 to work around this by also setting `additionalProperties` to `true` at
 the same level that you set `oneOf`.

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


More information about the wp-trac mailing list