Make WordPress Core


Ignore:
Timestamp:
07/05/2020 12:13:37 AM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Make multi-typed schemas more robust.

A multi-type schema is a schema where the type keyword is an array of possible types instead of a single type. For instance, [ 'object', 'string' ] would allow objects or string values.

In [46249] basic support for these schemas was introduced. The validator would loop over each schema type trying to find a version that matched. This worked for valid values, but for invalid values it provided unhelpful error messages. The sanitizer also had its utility restricted.

In this commit, the validators and sanitizers will first determine the best type of the passed value and then apply the schema with that set type. In the case that a value could match multiple types, the schema of the first matching type will be used.

To maintain backward compatibility, if unsupported schema types are used, the value will always pass validation. A doing it wrong notice is issued in this case.

Fixes #50300.
Props pentatonicfunk, dlh, TimothyBlynJacobs.

File:
1 edited

Legend:

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

    r48300 r48306  
    342342        $this->assertNull( rest_sanitize_value_from_schema( null, $schema ) );
    343343        $this->assertEquals( '2019-09-19T18:00:00', rest_sanitize_value_from_schema( '2019-09-19T18:00:00', $schema ) );
    344         $this->assertNull( rest_sanitize_value_from_schema( 'lalala', $schema ) );
     344        $this->assertEquals( 'lalala', rest_sanitize_value_from_schema( 'lalala', $schema ) );
    345345    }
    346346
     
    395395        $this->assertEquals( 'My Value', rest_sanitize_value_from_schema( 'My Value', $schema ) );
    396396        $this->assertEquals( array( 'raw' => 'My Value' ), rest_sanitize_value_from_schema( array( 'raw' => 'My Value' ), $schema ) );
    397         $this->assertNull( rest_sanitize_value_from_schema( array( 'raw' => 1 ), $schema ) );
     397        $this->assertEquals( array( 'raw' => '1' ), rest_sanitize_value_from_schema( array( 'raw' => 1 ), $schema ) );
    398398    }
    399399
     
    424424        $this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => 0 ), $schema ) );
    425425
    426         $this->assertNull( rest_sanitize_value_from_schema( array( 'raw' => 'something non boolean' ), $schema ) );
     426        $this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => 'something non boolean' ), $schema ) );
     427    }
     428
     429    /**
     430     * @ticket 50300
     431     */
     432    public function test_multi_type_with_no_known_types() {
     433        $this->setExpectedIncorrectUsage( 'rest_handle_multi_type_schema' );
     434        $this->setExpectedIncorrectUsage( 'rest_sanitize_value_from_schema' );
     435
     436        $schema = array(
     437            'type' => array( 'invalid', 'type' ),
     438        );
     439
     440        $this->assertEquals( 'My Value', rest_sanitize_value_from_schema( 'My Value', $schema ) );
     441    }
     442
     443    /**
     444     * @ticket 50300
     445     */
     446    public function test_multi_type_with_some_unknown_types() {
     447        $this->setExpectedIncorrectUsage( 'rest_handle_multi_type_schema' );
     448        $this->setExpectedIncorrectUsage( 'rest_sanitize_value_from_schema' );
     449
     450        $schema = array(
     451            'type' => array( 'object', 'type' ),
     452        );
     453
     454        $this->assertEquals( 'My Value', rest_sanitize_value_from_schema( 'My Value', $schema ) );
     455    }
     456
     457    /**
     458     * @ticket 50300
     459     */
     460    public function test_multi_type_returns_null_if_no_valid_type() {
     461        $schema = array(
     462            'type' => array( 'number', 'string' ),
     463        );
     464
     465        $this->assertNull( rest_sanitize_value_from_schema( array( 'Hello!' ), $schema ) );
    427466    }
    428467}
Note: See TracChangeset for help on using the changeset viewer.