Make WordPress Core

Changeset 49063


Ignore:
Timestamp:
09/27/2020 07:01:18 PM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support the multipleOf JSON Schema keyword.

Props yakimun.
Fixes #51022.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r49054 r49063  
    15531553 *              Validate required properties.
    15541554 * @since 5.6.0 Support the "minProperties" and "maxProperties" keywords for objects.
     1555 *              Support the "multipleOf" keyword for numbers and integers.
    15551556 *
    15561557 * @param mixed  $value The value to validate.
    … …  
    16921693        }
    16931694
    1694         if ( in_array( $args['type'], array( 'integer', 'number' ), true ) && ! is_numeric( $value ) ) {
    1695                 /* translators: 1: Parameter, 2: Type name. */
    1696                 return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, $args['type'] ) );
     1695        if ( in_array( $args['type'], array( 'integer', 'number' ), true ) ) {
     1696                if ( ! is_numeric( $value ) ) {
     1697                        /* translators: 1: Parameter, 2: Type name. */
     1698                        return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, $args['type'] ) );
     1699                }
     1700
     1701                if ( isset( $args['multipleOf'] ) && fmod( $value, $args['multipleOf'] ) !== 0.0 ) {
     1702                        /* translators: 1: Parameter, 2: Multiplier. */
     1703                        return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be a multiple of %2$s.' ), $param, $args['multipleOf'] ) );
     1704                }
    16971705        }
    16981706
    … …  
    22992307                'exclusiveMinimum',
    23002308                'exclusiveMaximum',
     2309                'multipleOf',
    23012310                'minLength',
    23022311                'maxLength',
  • trunk/tests/phpunit/tests/rest-api/rest-controller.php

    r49053 r49063  
    282282                }
    283283
    284                 foreach ( array( 'minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum' ) as $property ) {
     284                foreach ( array( 'multipleOf', 'minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum' ) as $property ) {
    285285                        $this->assertArrayHasKey( $property, $args['someinteger'] );
    286286                }
  • trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r49053 r49063  
    434434                $this->assertWPError( $error );
    435435                $this->assertSame( 'param must be between 10 (inclusive) and 20 (inclusive)', $error->get_error_message() );
     436        }
     437
     438        /**
     439         * @ticket 51022
     440         *
     441         * @dataProvider data_multiply_of
     442         *
     443         * @param int|float $value
     444         * @param int|float $divisor
     445         * @param bool      $expected
     446         */
     447        public function test_numeric_multiple_of( $value, $divisor, $expected ) {
     448                $schema = array(
     449                        'type'       => 'number',
     450                        'multipleOf' => $divisor,
     451                );
     452
     453                $result = rest_validate_value_from_schema( $value, $schema );
     454
     455                if ( $expected ) {
     456                        $this->assertTrue( $result );
     457                } else {
     458                        $this->assertWPError( $result );
     459                }
     460        }
     461
     462        public function data_multiply_of() {
     463                return array(
     464                        array( 0, 2, true ),
     465                        array( 4, 2, true ),
     466                        array( 3, 1.5, true ),
     467                        array( 2.4, 1.2, true ),
     468                        array( 1, 2, false ),
     469                        array( 2, 1.5, false ),
     470                        array( 2.1, 1.5, false ),
     471                );
    436472        }
    437473
  • trunk/tests/phpunit/tests/rest-api/rest-test-controller.php

    r49053 r49063  
    4747                                'someinteger'    => array(
    4848                                        'type'             => 'integer',
     49                                        'multipleOf'       => 10,
    4950                                        'minimum'          => 100,
    5051                                        'maximum'          => 200,
Note: See TracChangeset for help on using the changeset viewer.