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 type
s 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" ]
.