Make WordPress Core


Ignore:
Timestamp:
07/04/2020 07:51:10 PM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Only validate the format keyword if the type is a string.

This allows for using multi-type support with a string that has a format. For backwards compatibility support, the format validation will still apply if the type is not specified, or it is invalid.

Two new doing it wrong notices are issued when omitting a type, or using an invalid type.

Props ryotsun.
Fixes #50189.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r48190 r48300  
    138138    }
    139139
     140    /**
     141     * @ticket 50189
     142     */
     143    public function test_format_validation_is_skipped_if_non_string_type() {
     144        $schema = array(
     145            'type'   => 'array',
     146            'items'  => array(
     147                'type' => 'string',
     148            ),
     149            'format' => 'email',
     150        );
     151        $this->assertTrue( rest_validate_value_from_schema( 'email@example.com', $schema ) );
     152        $this->assertTrue( rest_validate_value_from_schema( 'email', $schema ) );
     153    }
     154
     155    /**
     156     * @ticket 50189
     157     */
     158    public function test_format_validation_is_applied_if_missing_type() {
     159        $this->expectException( 'PHPUnit_Framework_Error_Notice' ); // For the undefined index.
     160        $this->setExpectedIncorrectUsage( 'rest_validate_value_from_schema' );
     161
     162        $schema = array( 'format' => 'email' );
     163        $this->assertTrue( rest_validate_value_from_schema( 'email@example.com', $schema ) );
     164        $this->assertWPError( rest_validate_value_from_schema( 'email', $schema ) );
     165    }
     166
     167    /**
     168     * @ticket 50189
     169     */
     170    public function test_format_validation_is_applied_if_unknown_type() {
     171        $this->setExpectedIncorrectUsage( 'rest_validate_value_from_schema' );
     172
     173        $schema = array(
     174            'format' => 'email',
     175            'type'   => 'str',
     176        );
     177        $this->assertTrue( rest_validate_value_from_schema( 'email@example.com', $schema ) );
     178        $this->assertWPError( rest_validate_value_from_schema( 'email', $schema ) );
     179    }
     180
    140181    public function test_type_array() {
    141182        $schema = array(
     
    323364
    324365    public function test_type_unknown() {
     366        $this->setExpectedIncorrectUsage( 'rest_validate_value_from_schema' );
     367
    325368        $schema = array(
    326369            'type' => 'lalala',
Note: See TracChangeset for help on using the changeset viewer.