I have asked this before at Stackoverflow, now I'm here to be more wp specific and bring more context - as I did some advance on this.
Seeing this use of oneOf has made me change my schema to this structure below:
array(
'type' => 'object',
'show_in_rest' => array(
'schema' => array(
'type' => 'object',
'properties' => array(
'tipo' => array(
'type' => 'string',
'name' => 'tipo',
'enum' => array(
'...',
),
),
'data' => array(
'type' => 'array',
'items' => array(
'type' => 'object',
'oneOf' => array(
array(
'properties' => array(
'dia' => array(
'type' => 'string',
'name' => 'dia',
),
'hora_inicio' => array(
'type' => 'string',
'name' => 'hora_inicio',
),
'hora_fim' => array(
'type' => 'string',
'name' => 'hora_fim',
),
),
'name' => 'data_unica',
'title' => 'Data única',
),
array(
'properties' => array(
'periodicidade' => array(
'type' => 'string',
'name' => 'periodicidade',
'enum' => array(
'...'
),
),
'dia_inicial' => array(
'type' => 'string',
'name' => 'dia_inicial',
),
'dia_final' => array(
'type' => 'string',
'name' => 'dia_final',
),
'hora_inicial' => array(
'type' => 'string',
'name' => 'hora_inicial',
),
'hora_final' => array(
'type' => 'string',
'name' => 'hora_final',
),
),
'name' => 'data_corrente',
'title' => 'Data corrente',
),
),
),
),
),
),
),
);
The request is:
meta: {
"datas": {
"tipo": "UNICA",
"data": [{
"dia": "2022-10-21",
"hora_inicio": "",
"hora_fim": ""
}]
}
}
And the response:
{
"code": "rest_additional_properties_forbidden",
"message": "dia is not a valid property of Object.",
"data": {
"status": 400
}
}
What am I doing wrong?
register_meta()
call for a metakey nameddatas
- is that correct? Or is this being used somewhere else? Which endpoint are you sending this request to?register_meta
and using a block that renders a form and mount that requestoneOf
to me. The only weird bit I see across the whole schema is usingname
attributes throughout, which do not serve a purpose in JSON Schema, nor do they in WordPress to the best of my knowledge. One thing you might try is removing"type": "object"
from the"items"
definition and adding it to each of the possible sub-schemas. I don't have a strong basis for this - I think either is technically valid. Failing that, I will try to replicate this myself in the next few days if no one else sorts it out!name
attribute to use the same schema to dinamically generate components for the block, but I probably could use the key instead the value for this. My first attempt was without the type foritems
, but this throws an error indebug.log
sayind that meta was registered incorrectly.