Make WordPress Core


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

REST API: Fix multi-type schemas with integer fields.

In [48306] support for multi-typed schemas was improved to first detect the data type of the value before applying further validation. The integer data type was detected using the new rest_is_integer function. This function used logic, however, that assumed that the value had already passed an is_numeric check. This meant that if integer and string were both acceptable types, the value would always be considered an integer causing the later accurate type validation to fail.

This commit fixes the rest_is_integer logic to include an is_numeric check.

Props rtagliento.
Fixes #51146.

File:
1 edited

Legend:

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

    r48365 r48881  
    10411041        $this->assertTrue( rest_validate_value_from_schema( $data, $schema ) );
    10421042    }
     1043
     1044    /**
     1045     * @ticket 50300
     1046     */
     1047    public function test_string_or_integer() {
     1048        $schema = array(
     1049            'type' => array( 'integer', 'string' ),
     1050        );
     1051
     1052        $this->assertTrue( rest_validate_value_from_schema( 'garbage', $schema ) );
     1053        $this->assertTrue( rest_validate_value_from_schema( 15, $schema ) );
     1054        $this->assertTrue( rest_validate_value_from_schema( '15', $schema ) );
     1055        $this->assertTrue( rest_validate_value_from_schema( '15.5', $schema ) );
     1056        $this->assertWPError( rest_validate_value_from_schema( 15.5, $schema ) );
     1057    }
     1058
    10431059}
Note: See TracChangeset for help on using the changeset viewer.