Skip to content

fix: don't reject filled required manyToOne relations when scheduling - #240

Merged
TMSchipper merged 1 commit into
masterfrom
bugfix/required-to-one-relation-validation
Nov 14, 2026
Merged

TMSchipper merged 1 commit into
masterfrom
bugfix/required-to-one-relation-validation

Conversation

@TMSchipper

@TMSchipper TMSchipper commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Scheduling a publish for an entry with a required manyToOne relation always failed, even when the relation was filled:

POST /publisher/actions → 400 ValidationError
{"errors":[{"path":["author"],"message":"This field is required"}]}

collectRequiredMissing in server/middlewares/validate-before-scheduling.js decided whether a relation holds an array with a fallback attr.relation.toLowerCase().includes('many'). That also matches manyToOne, so the populated single object was checked as an array (!Array.isArray(value) → "empty") and the action could never be scheduled.

Fix

  • Use Strapi's own helper relations.isAnyToMany(attr) from @strapi/utils (the same check the Content Manager uses to populate relations) instead of string matching.
  • Polymorphic relations that hold an array (morphMany, morphToMany) are added explicitly, because isAnyToMany only covers oneToMany/manyToMany.
  • To-one relations (manyToOne, oneToOne, morphOne, morphToOne) are now checked for null/undefined; to-many relations for an empty array. Empty required relations are still rejected.

Regression coverage (playground)

  • New Author collection type (no draft & publish). The files are identical to the ones on bugfix/212-restrict-plugin-users-without-permission, so they won't conflict.
  • Article gets a required manyToOne author relation; the seed creates an author and connects it to the draft articles.
  • CLAUDE.md documents that this relation must stay required.

Testing

  • yarn playground:smoke without the fix: fails with the author validation error above (bug reproduced).
  • With the fix:
    • yarn lint:check, yarn build, yarn verify ✔
    • yarn playground:smoke (fresh DB, incl. admin build) ✔: publish/unpublish of Article + Homepage, actions removed, all four hooks ran
    • One-off API check: draft article without author → 400 with an error on author; article with author → 201
    • yarn consumer-test with Strapi minimum and current ✔ (Strapi 5.34.0 and 5.61.0, Node 22)

Notes

  • Existing local playground databases keep their previously seeded articles without an author; run yarn playground:reset (or set an author) before scheduling those.
  • Overlaps with the playground changes on the Restrict plugin to users without "Publish" permissions #212 branch (article/schema.json, playground/src/index.js); whichever merges second needs a small conflict resolution. After both are merged, author stays required.
  • Possible follow-ups (not in this PR): add the negative case (empty required relation → rejected) to the smoke test once the Restrict plugin to users without "Publish" permissions #212 smoke-test restructure has landed, and consider checking emptiness by value shape or Strapi's strictRelations validator option instead of classifying relation types.

The required-relation check before scheduling a publish decided whether a
relation holds an array with `relation.includes('many')`, which also matches
manyToOne. A filled manyToOne relation (a single object) was then reported as
"This field is required", so such entries could never be scheduled.

- Use `relations.isAnyToMany` from @strapi/utils plus morphMany/morphToMany
  to detect to-many relations; to-one relations are checked for null
- Playground: add an Author type and a required manyToOne `author` relation
  on Article as regression coverage, seeded for the draft articles
- CLAUDE.md: document that the playground relation must stay required
@TMSchipper
TMSchipper force-pushed the bugfix/required-to-one-relation-validation branch from bb85097 to 813a63f Compare November 14, 2026 13:08
@TMSchipper
TMSchipper merged commit e5813a1 into master Nov 14, 2026
4 checks passed
@TMSchipper
TMSchipper deleted the bugfix/required-to-one-relation-validation branch November 14, 2026 13:13
TMSchipper added a commit that referenced this pull request Nov 15, 2026
- Add regression tests for filled/empty required to-one, to-many and morph
  relations (#240), deep populate and component/dynamic zone paths
- Run the action service and controller on Strapi's real core service and
  controller (tests/setup.js loads the CommonJS build of @strapi/strapi)
- Test helper: content-manager services by name, chainable populate-builder,
  strapi.contentTypes, a cron fake that behaves like Strapi's
- Tighten the publication service, controller, route, registry and bootstrap
  tests; add admin translation helper tests; include admin utils in coverage
- Lint vitest.config.mjs, group vitest updates in Dependabot, run the unit
  tests in the publish workflow, document the test scope and Node ^22.12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant