WordPress.org

Make WordPress Core

Opened 6 months ago

Last modified 8 days ago

#48821 new enhancement

Add additional array validation keywords to rest_validate_value_from_schema

Reported by: TimothyBlynJacobs Owned by:
Milestone: 5.5 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

JSON Schema describes minItems, maxItems and uniqueItems keywords to assist in validating array types: https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.4.1 I think this would be helpful to add support for.

Change History (8)

#1 @TimothyBlynJacobs
3 months ago

  • Milestone changed from Awaiting Review to 5.5

#2 @sorenbronsted
5 weeks ago

I will work on this

#3 @sorenbronsted
5 weeks ago

  • Keywords dev-feedback added

In connection with working with these validations, the method rest_validate_value_from_schema is getting rather long. I would like to propose that the validation is refactored into smaller functions, all called from rest_validate_value_from_schema. Any objections?

#4 @TimothyBlynJacobs
5 weeks ago

rest_validate_value_from_schema is definitely getting rather long, but let's tackle that in a separate ticket.

This ticket was mentioned in PR #253 on WordPress/wordpress-develop by sorenbronsted.


3 weeks ago

Added min,max and unique to items

Trac ticket: https://core.trac.wordpress.org/ticket/48821

#6 @sorenbronsted
3 weeks ago

  • Keywords has-patch has-unit-tests added; dev-feedback removed

#7 @prbot
8 days ago

sorenbronsted commented on PR #253:

We should also do research about what the most correct way to check uniqueness would be. SORT_REGULAR does an == comparison which is of particular importance for [arrays and objects](https://www.php.net/manual/en/language.operators.array.php).

For instance with the given schema and values, should those be considered unique or not?

`json
{

"type": "array",
"uniqueItems": true,
"items": {

"type": "array",
"items": {

"type": "string"

}

}

}
`

`json
[

[

"a",
"b",
"c"

],
[

"b",
"a",
"c"

]

]
`

Or what about

`json
{

"type": "array",
"uniqueItems": true,
"items": {

"type": [

"number",
"integer"

]

}

}
`

`json
[

1.0,
1

]
`

We should look at the spec, tests and other validators.

I have looked into justinrainbow approach and he uses var_exports and thereby converts everything to strings, but in my opinion he fails on numbers because [1,1.0] is unique according to justinrainbow but I think not, and the spec for uniqueItems is rather vague on what is unique means.
I will use justinrainbow approach but use the flag SORT_NUMERIC when the array type is declared numeric.
Thank you for your patience and feedback on my contributions :-)

#8 @prbot
8 days ago

TimothyBJacobs commented on PR #253:

Could you also take a look at https://github.com/ajv-validator/ajv/?

Thank you for your patience and feedback on my contributions :-)

No problem at all, thanks for taking on these tickets!

Note: See TracTickets for help on using tickets.