Make WordPress Core


Ignore:
Timestamp:
10/20/2020 06:22:39 PM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Add support for the oneOf and anyOf keywords.

This allows for REST API routes to define more complex validation requirements as JSON Schema instead of procedural validation.

The error code returned from rest_validate_value_from_schema for invalid parameter types has been changed from the generic rest_invalid_param to the more specific rest_invalid_type.

Props yakimun, johnbillion, TimothyBlynJacobs.
Fixes #51025.

File:
1 edited

Legend:

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

    r49082 r49246  
    588588        $this->assertWPError( rest_sanitize_value_from_schema( $data, $schema ) );
    589589    }
     590
     591    /**
     592     * @ticket 51025
     593     */
     594    public function test_any_of() {
     595        $schema = array(
     596            'anyOf' => array(
     597                array(
     598                    'type'       => 'integer',
     599                    'multipleOf' => 2,
     600                ),
     601                array(
     602                    'type'      => 'string',
     603                    'maxLength' => 1,
     604                ),
     605            ),
     606        );
     607
     608        $this->assertSame( 4, rest_sanitize_value_from_schema( '4', $schema ) );
     609        $this->assertSame( '5', rest_sanitize_value_from_schema( '5', $schema ) );
     610        $this->assertWPError( rest_sanitize_value_from_schema( true, $schema ) );
     611        $this->assertWPError( rest_sanitize_value_from_schema( '11', $schema ) );
     612    }
     613
     614    /**
     615     * @ticket 51025
     616     */
     617    public function test_one_of() {
     618        $schema = array(
     619            'oneOf' => array(
     620                array(
     621                    'type'       => 'integer',
     622                    'multipleOf' => 2,
     623                ),
     624                array(
     625                    'type'      => 'string',
     626                    'maxLength' => 1,
     627                ),
     628            ),
     629        );
     630
     631        $this->assertSame( 10, rest_sanitize_value_from_schema( '10', $schema ) );
     632        $this->assertSame( '5', rest_sanitize_value_from_schema( '5', $schema ) );
     633        $this->assertWPError( rest_sanitize_value_from_schema( true, $schema ) );
     634        $this->assertWPError( rest_sanitize_value_from_schema( '11', $schema ) );
     635        $this->assertWPError( rest_sanitize_value_from_schema( '4', $schema ) );
     636    }
    590637}
Note: See TracChangeset for help on using the changeset viewer.