Make WordPress Core

Changeset 47627


Ignore:
Timestamp:
04/27/2020 02:27:02 AM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support the (min|max)Length JSON Schema keywords.

Props sorenbronsted.
Fixes #48820.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r47550 r47627  
    13381338        }
    13391339
    1340         if ( 'string' === $args['type'] && ! is_string( $value ) ) {
    1341                 /* translators: 1: Parameter, 2: Type name. */
    1342                 return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ) );
     1340        if ( 'string' === $args['type'] ) {
     1341                if ( ! is_string( $value ) ) {
     1342                        /* translators: 1: Parameter, 2: Type name. */
     1343                        return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ) );
     1344                }
     1345
     1346                if ( isset( $args['minLength'] ) && mb_strlen( $value ) < $args['minLength'] ) {
     1347                        return new WP_Error(
     1348                                'rest_invalid_param',
     1349                                sprintf(
     1350                                        /* translators: 1: Parameter, 2: Number of characters. */
     1351                                        _n( '%1$s must be at least %2$s character long.', '%1$s must be at least %2$s characters long.', $args['minLength'] ),
     1352                                        $param,
     1353                                        number_format_i18n( $args['minLength'] )
     1354                                )
     1355                        );
     1356                }
     1357
     1358                if ( isset( $args['maxLength'] ) && mb_strlen( $value ) > $args['maxLength'] ) {
     1359                        return new WP_Error(
     1360                                'rest_invalid_param',
     1361                                sprintf(
     1362                                        /* translators: 1: Parameter, 2: Number of characters. */
     1363                                        _n( '%1$s must be at most %2$s character long.', '%1$s must be at most %2$s characters long.', $args['maxLength'] ),
     1364                                        $param,
     1365                                        number_format_i18n( $args['maxLength'] )
     1366                                )
     1367                        );
     1368                }
    13431369        }
    13441370
  • trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r47450 r47627  
    349349                $this->assertWPError( rest_validate_value_from_schema( array( 'raw' => array( 'a list' ) ), $schema ) );
    350350        }
     351
     352        /**
     353         * @ticket 48820
     354         */
     355        public function test_string_min_length() {
     356                $schema = array(
     357                        'type'      => 'string',
     358                        'minLength' => 2,
     359                );
     360
     361                // longer
     362                $this->assertTrue( rest_validate_value_from_schema( 'foo', $schema ) );
     363                // exact
     364                $this->assertTrue( rest_validate_value_from_schema( 'fo', $schema ) );
     365                // non-strings does not validate
     366                $this->assertWPError( rest_validate_value_from_schema( 1, $schema ) );
     367                // to short
     368                $this->assertWPError( rest_validate_value_from_schema( 'f', $schema ) );
     369                // one supplementary Unicode code point is not long enough
     370                $mb_char = mb_convert_encoding( '&#x1000;', 'UTF-8', 'HTML-ENTITIES' );
     371                $this->assertWPError( rest_validate_value_from_schema( $mb_char, $schema ) );
     372                // two supplementary Unicode code point is long enough
     373                $this->assertTrue( rest_validate_value_from_schema( $mb_char . $mb_char, $schema ) );
     374        }
     375
     376        /**
     377         * @ticket 48820
     378         */
     379        public function test_string_max_length() {
     380                $schema = array(
     381                        'type'      => 'string',
     382                        'maxLength' => 2,
     383                );
     384
     385                // shorter
     386                $this->assertTrue( rest_validate_value_from_schema( 'f', $schema ) );
     387                // exact
     388                $this->assertTrue( rest_validate_value_from_schema( 'fo', $schema ) );
     389                // to long
     390                $this->assertWPError( rest_validate_value_from_schema( 'foo', $schema ) );
     391                // non string
     392                $this->assertWPError( rest_validate_value_from_schema( 100, $schema ) );
     393                // two supplementary Unicode code point is long enough
     394                $mb_char = mb_convert_encoding( '&#x1000;', 'UTF-8', 'HTML-ENTITIES' );
     395                $this->assertTrue( rest_validate_value_from_schema( $mb_char, $schema ) );
     396                // three supplementary Unicode code point is to long
     397                $this->assertWPError( rest_validate_value_from_schema( $mb_char . $mb_char . $mb_char, $schema ) );
     398        }
    351399}
Note: See TracChangeset for help on using the changeset viewer.