Opened 6 years ago
Closed 4 years ago
#49960 closed enhancement (fixed)
REST API: Support sanitize callbacks for nested properties
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 5.9 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | REST API | Keywords: | has-patch has-unit-tests commit needs-refresh |
| Focuses: | Cc: |
Description
Example schema:
'foobar' => [ 'required' => true, 'type' => 'object', 'properties' => [ 'foo' => [ 'required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ], 'bar' => [ 'required' => true, 'type' => 'string', 'sanitize_callback' => [ static::class, 'sanitize_bar' ], ], ], ],
Unfortunately, the required and sanitize_callback values are currently not used for properties. required is now handled in #48818. With support for sanitize_callback we'd have another way to enforce a specific format without much hassle.
Related:
Change History (11)
#2
@
5 years ago
- Keywords reporter-feedback added
@ocean90 Could you share some examples of what kind of validation you would want to do in these instances?
I've been thinking that this might be better served by an API to register a JSON Schema format.
#3
@
5 years ago
- Keywords reporter-feedback removed
@TimothyBlynJacobs Since I just stumbled upon this again, the most common so far is limiting a field to only plain strings without any HTML like sanitize_text_field() (or sanitize_textarea_field() does for us.
Maybe it's common enough to be added as a default text-field format?
This ticket was mentioned in PR #1187 on WordPress/wordpress-develop by ocean90.
5 years ago
#5
- Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
Probably going to create a separate ticket for this enhancement but it's related to #WP49960.
So far the formats are only used in rest_sanitize_value_from_schema() because I'm not sure how a validation should look like. Do we need one?
Trac ticket: https://core.trac.wordpress.org/ticket/49960
#6
@
5 years ago
- Keywords commit added
- Milestone changed from Awaiting Review to 5.8
- Owner set to rachelbaker
- Status changed from new to accepted
#7
@
5 years ago
- Milestone changed from 5.8 to 5.9
Today is 5.8 feature freeze. Unfortunately this one ran out of time.
Punting to 5.9 as there has been good recent momentum.
This would definitely be helpful. I'm not sure what the best way to go about it is.
We have
rest_validate_request_from_schema, but that doesn't have any knowledge of the idea ofsanitize_callbackorvalidate_callbackand I'd be hesitant to add that to what is a generic JSON schema validator/sanitizer.Perhaps it could live in
rest_validate_request_arg? Though for similar reasons as mentioned in #48818, it'd be much simpler to add it torest_validate_value_from_schemasince it handles schema traversing. Maybe we need a general purpose schema traversing function?