Problem/Motivation

I do some validation inside of entity validators that isn't possible in field validators, such as comparing field values against one another or checking business logic to determine if the entity is a duplicate (not duplicate UUID, but effectively duplicate content.)

In all cases, if validation fails you get a 422 Unprocessable Entity status code back. This is generally great.

Except if I need to differentiate between invalid fields (the general use case) and the duplicate content case.

I've whipped up the attached MR to very conservatively let violations determine the HTTP status code. If all the violations set a code, and it is the same for all, then use that. Otherwise, default to the current behavior.

This would make json:api a more fluent HTTP API, but totally at the discretion of the developer.

Proposed resolution

Remaining tasks

If this looks like it will actually go into core/be entertained, write tests.

User interface changes

API changes

Potential for changed HTTP status code.

Data model changes

Release notes snippet

Issue fork drupal-3280019

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bradjones1 created an issue. See original summary.

bradjones1’s picture

Issue summary: View changes
Status: Active » Needs review

It's worth noting that you have to go out of your way to generate a violation with a code. I'm not aware of any such instances in core.

ranjith_kumar_k_u’s picture

StatusFileSize
new2.81 KB
new957 bytes

Status: Needs review » Needs work

The last submitted patch, 4: 3280019-4.patch, failed testing. View results

bradjones1’s picture

Thanks for the reroll, it would be nice to stick to MR though since that's what the issue started with.

Test failures seem easy enough to fix.

bradjones1’s picture

Soooo, Symfony's core validators set codes to basically an Enum, represented by a UUID. See CountValidator.

I added an is_numeric() to this, but it's still not the "right" solution. It would be nice to still do the underlying goal - allow validation errors to bubble up a status code/have that status inferred somehow, but this would need more thought as to specific implementation.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.