Make WordPress Core

Changes between Initial Version and Version 1 of Ticket #57312


Ignore:
Timestamp:
12/11/2022 03:36:21 PM (19 months ago)
Author:
TimothyBlynJacobs
Comment:

Note: There is a typo on the schema. Your patternProperty definition is using a $ for the start of string declaration instead of a ^. After correcting that, this is the error I'm getting.

my_data[abcde][0] does not match pattern ^[A-Z][a-z]{5}[0-9]$

The REST API needs to support type juggling so when you declare a list of types to support, it will find the first type based match and continue validating against that.

The validator is thus able to accept none as an array and will then proceed to use array validation for which the items keyword applies. However, what you'll want to do is flip the order so a string type will match first.

The pattern keyword only applies to string types, so if an array is passed, it won't try to assert that it matches the ^none$ regex. However, enum can apply to any type. So by setting an enum of [ "none" ] you are declaring that my_data must be a dictionary with 5 letter keys where each value must be "none".

The corrected schema is:

[
	'type'                 => 'object',
	'patternProperties'    => [
		'^[a-z]{5}$' => [
			'type'        => [
				'string',
				'array',
			],
			'pattern'     => '^none$',
			'items'       => [
				'type'    => 'string',
				'pattern' => '^[A-Z][a-z]{5}[0-9]$',
			],
			'uniqueItems' => true,
			'required'    => true,
		],
	],
	'additionalProperties' => false,
	'minProperties'        => 1,
	'required'             => true,
]

However, I'd recommend using oneOf in this case to have a simpler to understand schema.

[
	'type'                 => 'object',
	'patternProperties'    => [
		'^[a-z]{5}$' => [
			'oneOf' => [
				[
					'type' => 'string',
					'enum' => [ 'none' ]
				],
				[
					'type'        => 'array',
					'items'       => [
						'type'    => 'string',
						'pattern' => '^[A-Z][a-z]{5}[0-9]$',
					],
					'uniqueItems' => true,
				]
			],
		],
	],
	'additionalProperties' => false,
	'minProperties'        => 1,
	'required'             => true,
]

It has the side-effect of allowing a type upgrade from a string value to an array. For instance, "Aabcde1" is accepted, not only [ "Aabcde1" ].

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #57312 – Description

    initial v1  
    22
    33- pattern properties
    4 - with values being either a '^none$' string or a an array of strings matching another regex, like '^[A-Z][a-z]{5}[0-9]$'.
     4- with values being either a `^none$` string or a an array of strings matching another regex, like `^[A-Z][a-z]{5}[0-9]$`.
    55
    66Try to submit your data via the js FormData API which holds an object that should validate against sth like this:
     
    88
    99{{{
    10 'my_data'    => [
    11                             'type'                => 'object',
    12                             'patternProperties'    => [
    13                                 '$[a-z]{5}$' => [
    14                                     'type'        => [
    15                                         'array',
    16                                         'string'
    17                                     ],
    18                                     'pattern' => '^none$',
    19                                     'items'      => [
    20                                         'type'    => 'string',
    21                                         'pattern' => '^[A-Z][a-z]{5}[0-9]$'
    22                                     ],
    23                                     'uniqueItems' => true,
    24                                     'required'    => true
    25                                 ]
    26                             ],
    27                             'additionalProperties' => false,
    28                             'minProperties'        => 1,
    29                             'required'             => true
    30                         ]
     10[
     11    'type' => 'object',
     12    'patternProperties' => [
     13        '$[a-z]{5}$' => [
     14            'type' => [
     15                'array',
     16                'string',
     17            ],
     18            'pattern' => '^none$',
     19            'items' => [
     20                'type' => 'string',
     21                'pattern' => '^[A-Z][a-z]{5}[0-9]$',
     22            ],
     23            'uniqueItems' => true,
     24            'required' => true,
     25        ],
     26    ],
     27    'additionalProperties' => false,
     28    'minProperties' => 1,
     29    'required' => true,
     30]
    3131}}}
    3232
     
    4242}}}
    4343
    44 It will result in a Bad Request error, telling you that my_data[abcde] does not match with ^none$.
     44It will result in a Bad Request error, telling you that `my_data[abcde]` does not match with `^none$`.
    4545
    46 I've tried to use enum = ['none'] instead of pattern => '^none$', which is when it also responds with a 400 Bad Request, saying that my_data[abcde] is not none.
     46I've tried to use `"enum": ["none"]` instead of `"pattern": "^none$"`, which is when it also responds with a 400 Bad Request, saying that `my_data[abcde]` is not none.
    4747
    4848Seems like a bug?