Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Allow rest_validate_request_arg() to validate arrays - #2397

Closed
JPry wants to merge 6 commits into
WP-API:developfrom
JPry:validate_arrays
Closed

JPry wants to merge 6 commits into
WP-API:developfrom
JPry:validate_arrays

Conversation

@JPry

@JPry JPry commented Mar 21, 2016

Copy link
Copy Markdown
Contributor

Fixex #2194

This adds array validation to rest_validate_request_arg() as described in #2194. Unit tests have been adeed for the new type.

Comment thread plugin.php
}

if ( 'array' === $args['type'] ) {
// A comma-separated array of IDs could be acceptable instead of an array.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the right abstraction.

JSON Schema permits us to specify two different types for a given property. In the case of anything using wp_parse_id_list(), we need to specify type => [ 'string', 'array' ], and then properly validate an array of types.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON Schema permits us to specify two different types for a given property. In the case of anything using wp_parse_id_list(), we need to specify type => [ 'string', 'array' ], and then properly validate an array of types.

I just want to make sure I'm understanding you correctly. In the case of type => [ 'string', 'array' ], we should ideally be checking for an array, and then ensuring that each element is a string? Or as another example, type => [ 'integer', 'array' ] would mean checking for an array of integers?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In JSON Schema, I believe the way to define an arrays type is like so

    "options": {
            "type": "array",
            "minItems": 1,
            "items": { "type": "string" },
            "uniqueItems": true
        },

See http://json-schema.org/example2.html

@danielbachhuber

Copy link
Copy Markdown
Member

@JPry: @BE-Webdesign had already started a PR for this issue in #2361. Which of you wants to take this all of the way through?

@BE-Webdesign

Copy link
Copy Markdown
Member

Doesn't matter to me, I'm happy to refine my PR or have somebody do a much better one 😃!

@JPry

JPry commented Mar 21, 2016

Copy link
Copy Markdown
Contributor Author

I don't mind if @BE-Webdesign's PR goes forward instead of mine. I just happened to have a couple of hours available and wanted to help out, so I picked a couple of issues in the Beta 13 milestone.

@JPry

JPry commented Mar 21, 2016

Copy link
Copy Markdown
Contributor Author

I do have an hour or 2 this morning to work on this, so maybe I'll see how far I can get 😄

@joehoyle

Copy link
Copy Markdown
Member

@JPry is there any update that needs to be added here, or is this changed direction and needs to be closed out?

@JPry

JPry commented Oct 12, 2016

Copy link
Copy Markdown
Contributor Author

@joehoyle Give me a day or two to review this, and then we can close it out if nothing has changed.

JPry added 2 commits October 12, 2016 18:19
* develop: (200 commits)
  Fix extra space
  Fix code standards issues from our PHPCS rules
  Revert "Fixed styling issues. Maybe."
  Properly register "{taxonomy}_exclude" parameters in the schema
  Fixed styling issues. Maybe.
  Fix error when a single meta value attempts to update to the current value. Adds test.
  Correct @see url for WP_Query
  Revert 6e1f9ba
  Remove conditional check on the `unfiltered_html` cap for discussion
  Remove temporary fix for upstream bug 35614.
  Documentation and code standards for `WP_REST_Terms_Controller`.
  Update changelog with changes since beta14
  Bump plugin version to 2.0beta15.
  Test theory that the problem is caused by and admin updating a comment as a different user
  Add back string type checking to content values
  Pass current filter into wp_kses
  Switch to `wp_filter_kses()` in an attempt to avoid having to pass global allowed tags
  Handle multiple duplicate values correctly
  Correct sanitization kses functions and re-add failing update content test
  I hate PHP 5.3  and lower
  ...
@JPry

JPry commented Oct 13, 2016

Copy link
Copy Markdown
Contributor Author

I made some updates here based on this comment. If we're following the JSON schema as @joehoyle mentioned, then the changes I've made seem to be the right way forward.

At this point I'm looking for some feedback on the approach I'm using. If what I've done here isn't the right way to go about this, then I would appreciate some input about what a better method might be.

I'm also curious about the failed tests from when I merged develop into my branch. I didn't have a chance to examine the tests in-depth yet, but a cursory examination makes it seem like it may not be my changes that caused the failures.

@joehoyle

Copy link
Copy Markdown
Member

@JPry thanks for expanding on this, so I think this works, however i think it could be more elegant. If we have a function validate_value_by_schema( $object, $schema ), we could have this function recursively call it's self in the event of finding an array. This would also allow us to deep check json schema objects too, I think.

I have an idea of how this could work, I'm happy to adapt this to try that direction.

@JPry

JPry commented Oct 14, 2016

Copy link
Copy Markdown
Contributor Author

Thanks for the idea @joehoyle. I'd be willing to make some updates to this based on your idea and what @BE-Webdesign mentioned in slack today. For my own reference, here are the relevant links from slack:

https://wordpress.slack.com/archives/core-restapi/p1476455590007085
https://wordpress.slack.com/archives/core-restapi/p1476455898007096

Comment thread plugin.php
// Map values to the correct type, defaulting to string as the type.
$type = isset( $args['items']['type'] ) ? $args['items']['type'] : 'string';
foreach ( $value as &$_value ) {
settype( $_value, $type );

@BE-Webdesign BE-Webdesign Oct 14, 2016 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using settype we should use our own validation systems because settype can lead to the wrong information. We can get away with settype though as most of the endpoints only deal with arrays of strings or integers. If this PR plans to handle booleans then this will cause a problem when values like 'false' are passed into an array booleans.

'somecsv' => array(
'type' => 'array',
'allow_csv' => true,
),

@BE-Webdesign BE-Webdesign Oct 14, 2016 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to test against the actual JSON Schema here. So we need to have items added. In a PR I did that I ultimately did not like I had schema like this.

someIdList => array(
    'type' => 'array',
    'items' => array(
        'type' => 'integer'
    ),
   'minItems' => 1,
   'uniqueItems' => true,
),

Then the same thing for string lists. Currently in the endpoints most of the arrays are id lists and only a few are string lists.

Comment thread plugin.php
case 'boolean':
$function = 'rest_is_boolean';
break;

@BE-Webdesign BE-Webdesign Oct 14, 2016 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't worry about supporting boolean because there are no endpoint parameters that use an array of booleans. This PR won't handle booleans 100% properly so let's not check for them.

Comment thread plugin.php
case 'string':
$function = 'is_string';
break;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a default case so there is no fall through, currently in the API we use super loose validation and most things will pass for true by default. I believe this PR is returning false for all of these values, that fall through.

'default' => array(),
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The items schema needs to be added into all of the array parameters so that your validation is actually working. This is why all of the tests are failing because the schema is not set yet. Every include/exclude author_include/exclude parameter needs it and I believe user roles. There are some other ones that I can't remember.

@JPry JPry closed this May 26, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants