[wp-trac] [WordPress Trac] #47906: REST API validation schema should support regular expressions for strings.
WordPress Trac
noreply at wordpress.org
Tue Aug 20 18:48:02 UTC 2019
#47906: REST API validation schema should support regular expressions for strings.
----------------------------+-----------------------------
Reporter: manzoorwani.jk | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: REST API | Version:
Severity: normal | Keywords:
Focuses: rest-api |
----------------------------+-----------------------------
Currently, the default validation schema
([https://core.trac.wordpress.org/browser/tags/5.2/src/wp-includes/rest-
api.php#L1200 for strings]) is limited to `type`, `enum` and `format`
(date-time, email & ip). It would be useful to have RegEx support out of
the box. So, instead of just
{{{#!php
<?php
if ( 'string' === $args['type'] && ! is_string( $value ) ) {
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is
not of type %2$s.' ), $param, 'string' ) );
}
}}}
we can do like this
{{{#!php
<?php
if ( 'string' === $args['type'] ) {
if ( ! is_string( $value ) ) {
return new WP_Error( 'rest_invalid_param', sprintf( __(
'%1$s is not of type %2$s.' ), $param, 'string' ) );
}
if ( isset( $args['matches'] ) && ! preg_match( $args['matches'],
$value ) ) {
return new WP_Error( 'rest_invalid_param', sprintf( __(
'%1$s does not match the pattern.' ), $param ) );
}
}
}}}
This way, we can specify the schema like this
{{{#!php
<?php
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'some_callback',
'args' => array(
'id' => array(
'type' => 'string',
'required' => true,
'matches' => '/\A[a-z0-9_]{1,32}\Z/i',
),
),
)
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/47906>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list