plugin development – oneOf two possible objects in WP REST API?

Question

I’m trying to define an endpoint that accepts two possible objects upon input, and currently tried to do so via:

'args'                => [
                                    'type' => 'object',
                                    'oneOf' => [
                                        [
                                            'title'      => 'first_option',
                                            'type'       => 'object',
                                            'properties' => [
                                                'first_name'    => [
                                                    'type'              => 'string',
                                                    'required'          => true,
                                                    'validate_callback' => 'rest_validate_request_arg',
                                                    'sanitize_callback' => 'rest_sanitize_request_arg'
                                                ],
                                                'languages'     => [
                                                    'type'              => 'array',
                                                    'uniqueItems'       => true,
                                                    'items'             => [
                                                        'type'    => 'string',
                                                        'pattern' => 'whatever'
                                                    ],
                                                    'minItems'          => 1,
                                                    'required'          => true,
                                                    'validate_callback' => 'rest_validate_request_arg',
                                                    'sanitize_callback' => 'rest_sanitize_request_arg'
                                                ]
                                            ],
                                            'additionalProperties' => false,
'required'          => true,
                                                    'validate_callback' => 'rest_validate_request_arg',
                                                    'sanitize_callback' => 'rest_sanitize_request_arg'
                                        ],
                                        [
                                            'title'      => 'second_option',
                                            'type'       => 'object',
                                            'properties' => [
                                                'age'        => [
                                                    'type'              => 'integer',
                                                    'required'          => true,
                                                    'validate_callback' => 'rest_validate_request_arg',
                                                    'sanitize_callback' => 'rest_sanitize_request_arg'
                                                ],
                                                'gender'     => [
                                                    'type'              => 'string',
                                                    'enum'              => [
                                                        'm',
                                                        'f'
                                                    ],
                                                    'required'          => true,
                                                    'validate_callback' => 'rest_validate_request_arg',
                                                    'sanitize_callback' => 'rest_sanitize_request_arg'
                                                ]
                                            ],
                                            'additionalProperties' => false,
'required'          => true,
                                                    'validate_callback' => 'rest_validate_request_arg',
                                                    'sanitize_callback' => 'rest_sanitize_request_arg'
                                        ]
                                    ],
'required'          => true,
                                                    'validate_callback' => 'rest_validate_request_arg',
                                                    'sanitize_callback' => 'rest_sanitize_request_arg'
                                ]

So I want the request to either accept a

  • first_name : string
  • languages : array

or a

  • age : integer
  • gender : string

Payload. This totally does not seem to work; as stuff like this gets through, no problem:

{
  "hello": "there",
  "all": "good?"
}

What am I doing wrong? I’ve seen this post, but it does not talk about objects, but an array of objects. It also seems like somewhat of a workaround; not coded in the way WP intends you to.

So, how can I correctly implement a WP REST Schema for one of two possible objects as the payload?

UPDATE

'args'                => [
                                    'data' => [
                                        'type'  => 'array',
                                        'items' => [
                                            'oneOf' => [
                                                [
                                                    'title'      => 'first_option',
                                                    'type'       => 'object',
                                                    'properties' => [
                                                        'first_name' => [
                                                            'type' => 'string',
                                                            'enum' => [
                                                                'first name',
                                                            ],
                                                            'required' => true
                                                        ],
                                                        'last_name'  => [
                                                            'type' => 'string',
                                                            'enum' => [
                                                                'last_name'
                                                            ],
                                                            'required' => true
                                                        ]
                                                    ],
                                                    'additionalProperties' => false
                                                ],
                                                [
                                                    'title'      => 'second_option',
                                                    'type'       => 'object',
                                                    'properties' => [
                                                        'age'    => [
                                                            'type'    => 'integer',
                                                            'minimum' => 18,
                                                            'required' => true
                                                        ],
                                                        'gender' => [
                                                            'type' => 'string',
                                                            'enum' => [
                                                                'm',
                                                                'w'
                                                            ],
                                                            'required' => true
                                                        ],
                                                    ],
                                                    'additionalProperties' => false
                                                ],
                                            ],
                                        ],
                                        'minItems' => 1,
                                        'maxItems' => 1,
                                        'required'          => true,
                                        'validate_callback' => 'rest_validate_request_arg',
                                        'sanitize_callback' => 'rest_sanitize_request_arg'
                                    ]
                                ]

This seems to work if you send an array as a payload. Trying to do it only as object now.

0
DevelJoe 4 weeks 2022-08-28T09:19:11-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse