Skip to content

fix: allow required fields with admin.condition in typescriptSchema#16800

Open
MurzNN wants to merge 1 commit into
payloadcms:mainfrom
Murz-forks:fix/16797-fix-required-field-schema
Open

fix: allow required fields with admin.condition in typescriptSchema#16800
MurzNN wants to merge 1 commit into
payloadcms:mainfrom
Murz-forks:fix/16797-fix-required-field-schema

Conversation

@MurzNN
Copy link
Copy Markdown

@MurzNN MurzNN commented May 29, 2026

What?

Added support for marking fields as required in TypeScript schema generation when both required: true and admin.condition are defined. Updated the field requirement check in configToJSONSchema() to explicitly validate the field.required property, and added comprehensive test coverage to validate the fix.

Why?

Fields configured with required: true and admin.condition were being incorrectly excluded from the required array during TypeScript type generation. This caused TypeScript to infer these fields as optional (field?) even though they were explicitly marked as required in the configuration, resulting in type mismatches and potential runtime errors.

How?

Core Fix (configToJSONSchema.ts):

  • Modified the field requirement check to include an explicit field.required === true condition alongside existing checks (isRequired and fieldSchema.required === true)
  • This allows fields with admin.condition to be included in the required array when they have required: true

Test Coverage (types):

  • Refactored integration tests to call configToJSONSchema() directly at runtime instead of reading pre-generated type files
  • Added three new integration tests that validate: (1) field appears in required array, (2) field maintains correct type and required status, (3) no regression to other required fields
  • Added four new type validation tests using tstyche framework to verify TypeScript types are correct
  • All tests validate actual schema generation behavior and fail when the fix is reverted

Related Issues

Fixes #16797

@MurzNN MurzNN changed the title fix(payload): allow required fields with admin.condition in typescriptSchema fix: allow required fields with admin.condition in typescriptSchema May 29, 2026
Comment thread test/types/types.spec.ts
Comment thread test/types/types.spec.ts
Copy link
Copy Markdown
Author

@MurzNN MurzNN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified and cleaned up tests, review please.

Comment thread test/types/types.spec.ts
Comment thread test/types/types.spec.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants