Make pre-save entity validation more commonplace (e.g., the default)

Created on 18 August 2022, over 2 years ago
Updated 11 May 2023, over 1 year ago

Problem/Motivation

As Drupal becomes more of a legitimate framework in its own right, we should be more assertive about data integrity and failing early.

Currently, content entities have the (not that well documented) ability to require validation prior to save, however it's only really used by the default entity forms. Drupal core's JSON:API implementation similarly validates entities prior to saving them, but through a different mechanism/code path.

Sure, there are plenty of cases where you might start with an invalid (e.g., empty or mostly empty) entity and through the UI or whatever, a user will flesh it out. In JSON:API this is handled by being able to PATCH individual fields, which do not trigger a validation of the whole entity. Form API has conditional validation already and could be extended easy enough to do something similar.

There is an outdated @todo here that considers making missed validation (not to be confused with failed validation) an assertion instead of an exception, but that's kinda going the opposite direction.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🌱 Plan
Status

Active

Version

10.1 ✨

Component
EntityΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Related: Drupal's migration system does not validate content entities upon migrating them, unless you manually opt in to that. (See \Drupal\migrate\Plugin\migrate\destination\EntityContentBase).

    I personally am strongly in favor of this, I've run into this the first time while working on Drupal's REST API in early 2016.

    But that's also when I learned there are Reasons it works this way and that it's impossible to change. At least at the time. We've learned a lot about evolving a codebase without breaking BC. Maybe this is the kind of change we'd give people 2 major releases worth of time to adopt instead of 1.

  • πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

    This immediately made me think of this: #3118590: Media library allows adding videos from providers other than Youtube/Vimeo using Media embed β†’

    Leveraging entity validation would likely be the best solution to that one.

  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Maybe this is the kind of change we'd give people 2 major releases worth of time to adopt instead of 1.

    I think this is the kind of thing we could have "on" by default on new installs and opt-in for existing sites? That addresses much of the BC concern?

    At its core that issue results from relying on form validation instead of entity validation.

    BINGO. This is the issue. Too much logic in the form API layer instead of in the core entity APIs.

  • πŸ‡¬πŸ‡§United Kingdom catch

    I think this is the kind of thing we could have "on" by default on new installs and opt-in for existing sites? That addresses much of the BC concern?

    I'm not sure that would work - the bc concern is more that modules/migrations will be out there relying on the existing behaviour, and then when that module is installed on a new site, it will suddenly break. It would help custom code since that's generally maintained on existing sites, but not contrib/distributions.

    I haven't looked into what it would take, but I'm wondering about something like [#3201242] - i.e. all entity saves are required to opt in or out of validation, and then in a major release throw an exception if it's not specified. Then there's no default to change at all. The issue with that is the amount of actual change required is higher than just changing the default.

    Maybe a hybrid would be to trigger_error if we're setting the default, with a notice that the default will change. Then modules can either ignore that deprecation message if they don't mind, or explicitly opt in or out to get rid of it. But then in the major release we'd remove the deprecation message and change the default, no exception throwing.

  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    That's an interesting thought; the logic is kinda-sorta in line with what I developed in my trait which I attach to entity classes I want to force validation on: https://tech.kinksters.dating/posts/2022-08-18-trait-always-validate-ent...

Production build 0.71.5 2024