Skip to content

Fail required validation when a Content Blocks property has no blocks - #105

Merged
PerplexDaniel merged 1 commit into
PerplexDigital:masterfrom
talhamalik4025:fix/required-validation-on-empty-value
Sep 28, 2026
Merged

PerplexDaniel merged 1 commit into
PerplexDigital:masterfrom
talhamalik4025:fix/required-validation-on-empty-value

Conversation

@talhamalik4025

Copy link
Copy Markdown
Contributor

Fixes #104.

Problem

Ticking Mandatory on a Perplex.ContentBlocks property never produces a validation error, so a page publishes with zero blocks.

Umbraco's RequiredValidator treats a JSON-typed value as empty only when it is null or when the string collapses to {} or [] (StringExtensions.JsonEmpties). A ContentBlocksValue is a versioned envelope — {"version":4,"header":null,"blocks":[]} — which matches neither, so the required check always passes. ContentBlocksValidator does not cover it either, since it only walks the properties inside each existing block.

Change

ContentBlocksRequiredValidator normalises an empty value to {} and defers to the base validator, and ContentBlocksValueEditor overrides RequiredValidator to return it. This follows Umbraco's own RichTextRequiredValidator, which normalises a rich text value the same way before delegating.

A value counts as empty when it has no header and no blocks. Values that fail to deserialise are passed through untouched, so malformed data keeps the current behaviour rather than being reported as empty.

Verification

Ran ValidateRequired against the new validator and the stock RequiredValidator for the same inputs, on net10.0 against Umbraco 17. The stock column is the current behaviour.

case with fix stock
no blocks #validation_invalidEmpty (no error)
one block (no error) (no error)
header only (no error) (no error)
null #validation_invalidNull #validation_invalidNull
unparseable (no error) (no error)

dotnet build on Perplex.ContentBlocks.Core: 0 warnings, 0 errors with TreatWarningsAsErrors on.

Notes

  • ContentBlocksValueDeserializer.Deserialize runs Refine, which drops blocks whose element type no longer exists. A property holding only such blocks therefore validates as empty. That seemed right, since those blocks render nothing, but say the word if you would rather the check ran on the raw value.
  • This adds a second deserialisation per validation, alongside the one ContentBlocksValidator already performs. Happy to thread a shared value through instead if you prefer.
  • I have not touched RELEASE_NOTES.md, since the version this lands in is yours to decide.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@talhamalik4025
talhamalik4025 marked this pull request as ready for review September 21, 2026 06:24
@PerplexDaniel
PerplexDaniel merged commit e091ec8 into PerplexDigital:master Sep 28, 2026
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.

Mandatory flag on a Content Blocks property is silently ignored

2 participants