Sunday, August 28, 2022
HomeWordPress Developmentplugin growth - oneOf two doable objects in WP REST API?

plugin growth – oneOf two doable objects in WP REST API?


I am attempting to outline an endpoint that accepts two doable objects upon enter, and at the moment tried to take action through:

'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 need the request to both settle for a

  • first_name : string
  • languages : array

or a

  • age : integer
  • gender : string

Payload. This completely doesn’t appear to work; as stuff like this will get by means of, no drawback:

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

What am I doing incorrect? I’ve seen this submit, nevertheless it doesn’t discuss objects, however an array of objects. It additionally looks like considerably of a workaround; not coded in the best way WP intends you to.

So, how can I accurately implement a WP REST Schema for considered one of two doable objects because 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 appears to work when you ship an array as a payload. Making an attempt to do it solely as object now.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments