Make WordPress Core


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

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

Props sorenbronsted.
Fixes #48820.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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( 'က', '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( 'က', '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.