WordPress.org

Make WordPress Core

Opened 5 weeks ago

Last modified 4 weeks ago

#49960 new enhancement

REST API: Support sanitize callbacks for nested properties

Reported by: ocean90 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: REST API Keywords: needs-patch needs-unit-tests
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 (1)

#1 @TimothyBlynJacobs
4 weeks ago

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 of sanitize_callback or validate_callback and 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 to rest_validate_value_from_schema since it handles schema traversing. Maybe we need a general purpose schema traversing function?

Note: See TracTickets for help on using tickets.