REST API: Improve error messages for number relational validation.
Props jblz. Fixes #39054. git-svn-id: https://develop.svn.wordpress.org/trunk@39896 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
ce89e8dd48
commit
18eab18694
|
@ -1068,18 +1068,18 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) {
|
||||||
if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) {
|
if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) {
|
||||||
if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) {
|
if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) {
|
||||||
/* translators: 1: parameter, 2: minimum number */
|
/* translators: 1: parameter, 2: minimum number */
|
||||||
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than %2$d (exclusive)' ), $param, $args['minimum'] ) );
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than %2$d' ), $param, $args['minimum'] ) );
|
||||||
} elseif ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) {
|
} elseif ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) {
|
||||||
/* translators: 1: parameter, 2: minimum number */
|
/* translators: 1: parameter, 2: minimum number */
|
||||||
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than %2$d (inclusive)' ), $param, $args['minimum'] ) );
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than or equal to %2$d' ), $param, $args['minimum'] ) );
|
||||||
}
|
}
|
||||||
} elseif ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) {
|
} elseif ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) {
|
||||||
if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) {
|
if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) {
|
||||||
/* translators: 1: parameter, 2: maximum number */
|
/* translators: 1: parameter, 2: maximum number */
|
||||||
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be less than %2$d (exclusive)' ), $param, $args['maximum'] ) );
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be less than %2$d' ), $param, $args['maximum'] ) );
|
||||||
} elseif ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) {
|
} elseif ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) {
|
||||||
/* translators: 1: parameter, 2: maximum number */
|
/* translators: 1: parameter, 2: maximum number */
|
||||||
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be less than %2$d (inclusive)' ), $param, $args['maximum'] ) );
|
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be less than or equal to %2$d' ), $param, $args['maximum'] ) );
|
||||||
}
|
}
|
||||||
} elseif ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) {
|
} elseif ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) {
|
||||||
if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
|
if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
|
||||||
|
|
|
@ -186,7 +186,7 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas
|
||||||
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
|
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
|
||||||
$data = $response->get_data();
|
$data = $response->get_data();
|
||||||
$first_error = array_shift( $data['data']['params'] );
|
$first_error = array_shift( $data['data']['params'] );
|
||||||
$this->assertContains( 'page must be greater than 1 (inclusive)', $first_error );
|
$this->assertContains( 'page must be greater than or equal to 1', $first_error );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_get_items_include_query() {
|
public function test_get_items_include_query() {
|
||||||
|
|
|
@ -19,6 +19,7 @@ class WP_Test_REST_Schema_Validation extends WP_UnitTestCase {
|
||||||
);
|
);
|
||||||
$this->assertTrue( rest_validate_value_from_schema( 1, $schema ) );
|
$this->assertTrue( rest_validate_value_from_schema( 1, $schema ) );
|
||||||
$this->assertTrue( rest_validate_value_from_schema( 2, $schema ) );
|
$this->assertTrue( rest_validate_value_from_schema( 2, $schema ) );
|
||||||
|
$this->assertWPError( rest_validate_value_from_schema( 0.9, $schema ) );
|
||||||
$this->assertWPError( rest_validate_value_from_schema( 3, $schema ) );
|
$this->assertWPError( rest_validate_value_from_schema( 3, $schema ) );
|
||||||
$this->assertWPError( rest_validate_value_from_schema( true, $schema ) );
|
$this->assertWPError( rest_validate_value_from_schema( true, $schema ) );
|
||||||
}
|
}
|
||||||
|
@ -31,6 +32,7 @@ class WP_Test_REST_Schema_Validation extends WP_UnitTestCase {
|
||||||
);
|
);
|
||||||
$this->assertTrue( rest_validate_value_from_schema( 1, $schema ) );
|
$this->assertTrue( rest_validate_value_from_schema( 1, $schema ) );
|
||||||
$this->assertTrue( rest_validate_value_from_schema( 2, $schema ) );
|
$this->assertTrue( rest_validate_value_from_schema( 2, $schema ) );
|
||||||
|
$this->assertWPError( rest_validate_value_from_schema( 0, $schema ) );
|
||||||
$this->assertWPError( rest_validate_value_from_schema( 3, $schema ) );
|
$this->assertWPError( rest_validate_value_from_schema( 3, $schema ) );
|
||||||
$this->assertWPError( rest_validate_value_from_schema( 1.1, $schema ) );
|
$this->assertWPError( rest_validate_value_from_schema( 1.1, $schema ) );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue