Problem/Motivation

When a Patternkit JSON Schema string field is both listed in required at the parent object level and declares minLength: 1 on the property, the JSON Editor form displays two identical "Value required." error messages under the same field when the field is left empty.

The text field in the patternkit_example module's example pattern already reproduces this exactly. It is required and declares minLength: 1.

The duplication happens because two independent validators both emit the error_notempty message for the same field path:

  1. The JSON Editor built-in minLength validator: when minLength: 1 is set and the value is "", it emits error_notempty at the field path with property: 'minLength'.
  2. The Patternkit custom patternkitRequiredStringValidator in js/patternkit.jsoneditor.js: it iterates schema.required, finds the empty string field, and emits error_notempty at the same path with property: 'required'.

JSON Editor's _removeDuplicateErrors deduplicates on the triple (message, path, property). Because the two errors have different property values ('minLength' vs 'required'), the deduplication does not suppress the second message.

Steps to reproduce

  1. Enable patternkit_example.
  2. Place a Patternkit block using the Example pattern (@patternkit/atoms/example/src/example) on any layout.
  3. Open the block editor.
  4. Clear the Text field so it is empty.
  5. Click Update.
  6. Observe two "Value required." messages appear under the Text * field label.

Expected: One "Value required." message.

Proposed resolution

Add a guard condition inside the custom patternkitRequiredStringValidator loop in js/patternkit.jsoneditor.js. When a required string field's schema declares minLength as a number >= 1, the custom validator skips emitting error_notempty. The built-in minLength validator already emits it for this case.

The change is 10 lines, inline in the existing forEach callback. No new files or abstractions are introduced.

Behavior after fix:

  • Required field + minLength: 1 + empty value: one "Value required." (from the built-in minLength validator only)
  • Required field + no minLength + empty value: one "Value required." (from the custom validator, unchanged)
  • Required field + minLength >= 2 + empty value: one message from the built-in validator; custom validator skips
  • Non-empty value: no error (unchanged)

Remaining tasks

  • Review and test the merge request.
  • CI validation.

User interface changes

The inline validation display in Patternkit block editor forms changes from two "Value required." messages to one when a required string field also declares minLength: 1. The patternkit_example Example pattern's Text field is affected and can be used to verify the fix.

Introduced terminology

None.

API changes

None.

Data model changes

None.

Release notes snippet

Patternkit no longer displays a duplicate "Value required." validation message when a JSON Schema string field is both listed in required and declares minLength: 1. The patternkit_example Example pattern's Text field demonstrates the corrected behavior. Pattern authors do not need to change their schemas.

Issue fork patternkit-3593600

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

slucero created an issue. See original summary.

slucero’s picture

Status: Active » Needs review
slucero’s picture

Status: Needs review » Reviewed & tested by the community

Reviewed and approved internally at Red Hat. Ready for merge for inclusion in the 9.1.3 release.

  • slucero committed bad1f14f on 9.1.x
    fix: #3593600 Duplicate "Value required" error when required string...
slucero’s picture

Status: Reviewed & tested by the community » Fixed

Merged for inclusion in the 9.1.3 release.
See #3542304: Patternkit 9.1.3 Release Plan.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.