WordPress.org

Make WordPress Core

Opened 13 months ago

Closed 13 months ago

Last modified 13 months ago

#51782 closed defect (bug) (worksforme)

Required in REST API register_rest_route args should check for empty string

Reported by: khr2003 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: needs-patch
Focuses: rest-api Cc:

Description

When setting required key to true in register_rest_route it will only check for the existence of the key but not if value is empty.

I think the expectation is to check for both that the key is set with a value (that is not empty) has been set.

Change History (3)

#1 @TimothyBlynJacobs
13 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed
  • Version changed from 5.5.3 to 4.7

Hi @khr2003,

Welcome to trac and thanks for the ticket!

I understand this can be confusing, but it is the functionality required by the JSON Schema specification.

When you want a field to be required, and disallow an empty string, I'd recommend using the minLength keyword. For example:

array(
        'type'      => 'string',
        'minLength' => 1,
        'required'  => true,
)

You can use the minItems keyword for arrays, minimum for numbers, and minProperties for objects.

#2 @khr2003
13 months ago

@TimothyBlynJacobs Thanks, your suggestion worked.

I might add it to documentation to avoid confusion.

#3 @TimothyBlynJacobs
13 months ago

Good call, I'll make sure to add it when we refresh our documentation for 5.6

Note: See TracTickets for help on using tickets.