Make WordPress Core

Changeset 47753


Ignore:
Timestamp:
05/03/2020 07:18:40 PM (5 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support the uuid JSON Schema format.

This accepts a uuid of any version. A future commit could add support for restricting uuids to a specific version.

Props johnwatkins0.
Fixes #50053.

Location:
trunk
Files:
5 edited

Legend:

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

    r47627 r47753  
    13921392                    /* translators: %s: IP address. */
    13931393                    return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not a valid IP address.' ), $param ) );
     1394                }
     1395                break;
     1396            case 'uuid':
     1397                if ( ! wp_is_uuid( $value ) ) {
     1398                    /* translators: %s is the name of a JSON field expecting a valid uuid. */
     1399                    return new WP_Error( 'rest_invalid_uuid', sprintf( __( '%s is not a valid UUID.' ), $param ) );
    13941400                }
    13951401                break;
     
    15501556            case 'ip':
    15511557                return sanitize_text_field( $value );
     1558
     1559            case 'uuid':
     1560                return sanitize_text_field( $value );
    15521561        }
    15531562    }
  • trunk/tests/phpunit/tests/rest-api/rest-controller.php

    r47511 r47753  
    4444                        'format' => 'email',
    4545                    ),
     46                    'someuuid'    => array(
     47                        'type'   => 'string',
     48                        'format' => 'uuid',
     49                    ),
    4650                ),
    4751            )
     
    202206            'rest_invalid_date',
    203207            rest_validate_request_arg( '2010-18-18T12:00:00', $this->request, 'somedate' )
     208        );
     209    }
     210
     211    /**
     212     * @ticket 50053
     213     */
     214    public function test_validate_schema_format_uuid() {
     215        $this->assertTrue(
     216            rest_validate_request_arg( '123e4567-e89b-12d3-a456-426655440000', $this->request, 'someuuid' )
     217        );
     218
     219        $this->assertErrorResponse(
     220            'rest_invalid_uuid',
     221            rest_validate_request_arg( '123e4567-e89b-12d3-a456-426655440000X', $this->request, 'someuuid' )
    204222        );
    205223    }
     
    246264                'someemail',
    247265                'somehex',
     266                'someuuid',
    248267                'someenum',
    249268                'someargoptions',
     
    276295                    'someemail',
    277296                    'somehex',
     297                    'someuuid',
    278298                    'someenum',
    279299                    'someargoptions',
  • trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php

    r47450 r47753  
    9191    }
    9292
     93    /**
     94     * @ticket 50053
     95     */
     96    public function test_format_uuid() {
     97        $schema = array(
     98            'type'   => 'string',
     99            'format' => 'uuid',
     100        );
     101        $this->assertEquals( '44', rest_sanitize_value_from_schema( 44, $schema ) );
     102        $this->assertEquals( 'hello', rest_sanitize_value_from_schema( 'hello', $schema ) );
     103        $this->assertEquals(
     104            '123e4567-e89b-12d3-a456-426655440000',
     105            rest_sanitize_value_from_schema( '123e4567-e89b-12d3-a456-426655440000', $schema )
     106        );
     107    }
     108
    93109    public function test_type_array() {
    94110        $schema = array(
  • trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r47627 r47753  
    8484        $this->assertTrue( rest_validate_value_from_schema( '#FFF', $schema ) );
    8585        $this->assertWPError( rest_validate_value_from_schema( 'WordPress', $schema ) );
     86    }
     87
     88    /**
     89     * @ticket 50053
     90     */
     91    public function test_format_uuid() {
     92        $schema = array(
     93            'type'   => 'string',
     94            'format' => 'uuid',
     95        );
     96        $this->assertTrue( rest_validate_value_from_schema( '123e4567-e89b-12d3-a456-426655440000', $schema ) );
     97        $this->assertWPError( rest_validate_value_from_schema( '123e4567-e89b-12d3-a456-426655440000X', $schema ) );
     98        $this->assertWPError( rest_validate_value_from_schema( '123e4567-e89b-?2d3-a456-426655440000', $schema ) );
    8699    }
    87100
  • trunk/tests/phpunit/tests/rest-api/rest-test-controller.php

    r47450 r47753  
    7070                    'context' => array( 'view' ),
    7171                ),
     72                'someuuid'       => array(
     73                    'type'    => 'string',
     74                    'format'  => 'uuid',
     75                    'context' => array( 'view' ),
     76                ),
    7277                'someenum'       => array(
    7378                    'type'    => 'string',
Note: See TracChangeset for help on using the changeset viewer.