Skip to content

JSON Field Validation Improvements - #4558

Draft
kaise-lafrai wants to merge 1 commit into
2.xfrom
validation-service
Draft

JSON Field Validation Improvements#4558
kaise-lafrai wants to merge 1 commit into
2.xfrom
validation-service

Conversation

@kaise-lafrai

@kaise-lafrai kaise-lafrai commented Sep 22, 2025

Copy link
Copy Markdown
Contributor

Fixes #4530

Describe your changes

This PR introduces improvements to JSON Field Validation.

Our forms contain a single field called field-json-metadata, and when an error is caught in the entity-layer inProperJsonValidator the user will see the all the fields turn red. This is not ideal and we need to be able to highlight the field that has the error so the user can quickly identify and resolve the issue.

It's important to understand the order of validation. First form validation occurs, then entity validation. Because entity validation catches integrity issues on the node, it adds violations into the context and when the form reloads it highlights the entire field-json-metadata field.

We have been relying mostly on schemaValidation in Opis\JsonSchema to validate the json string against the json schema. The errors are caught in ProperJsonValidator where they are added as violations to the context. We also have have element and form validation scattered around in the helper classes in json_form_widget module. What I am proposing is that we rely only on the json schema validation as the sole validator.

The following is generated by copilot and reviewed by @kaise-lafrai

Validation and Error Handling Enhancements:

  • Introduced a new FormPostValidate service and handler (FormPostValidate.php) to process validation errors after constraint validation, attach the error styling library, and set errors on the correct form elements. [1] [2] [3]
  • Improved error reporting in ProperJsonValidator by extracting field context from validation errors and attaching it to constraint violations, enabling precise error mapping in the form. [1] [2].
  • NOTE: The code within the method (extractFieldFromPointer) found in the ProperJsonValidator is not ideal when it comes to handling dataPointers that are complex like nested arrays. I think this can be improved so were not assuming a structure that can change in the future. For example, the dataPointer to the error is an array with the following index ['creator','0','email'] but we need the full path to the element so were building that to be something like ['creator','creator','0','creator','0','email'] this then gets stored using setParameter and later used in the FormPostValidate to set get the element and set the error. 1

User Interface Improvements:

  • Added a CSS file (style.css) and library to visually highlight fieldsets and legends with validation errors, making it easier for users to identify problematic fields. [1] [2]

Schema and Data Structure Updates for TESTING PURPOSES ONLY

  • Extended the dataset JSON schema to require a creators array (with at least two authors), each having required fields (givenName, familyName, privateEmail), and added new fields like number with validation constraints. [1] [2]

Form Widget Integration:

  • Updated the JSON form widget to support late validation handling, ensuring entity constraints are validated and errors are passed to the new post-validation handler for accurate feedback.
  • NOTE: It was discussed that maybe this get moved over into the metastore module.

These changes collectively provide more robust and user-friendly validation for complex JSON forms, especially for forms with nested or array-based fields.

TO DO

  • Confirm this approach works and is a good practice.
  • Confirm that this approach does not affect other custom validation that could occur during validation that is not field related. If it does we may need to adjust or pivot from this approach.
  • Since we are planning to decouple json_form_widget, we should look at this code in that lense and see if we can do anything to help move us in that direction in the future.
  • The error messages provided by the json schema validation are generic and don't provide enough info at times about the specific field that has the error. Look into adjusting the message/parameters that can be used to modify the error message.
  • Right now the first error that is caught throws an exception and displays to the user after saving the form, we may want to change that so all the errors that are currently present are displayed to the user when they save the form.
  • Need tests.

QA Steps

  • Add manual QA steps in checklist format for a reviewer to perform. Be as specific as possible, provide examples if appropriate.

Checklist before requesting review

If any of these are left unchecked, please provide an explanation

  • I have updated or added tests to cover my code
  • I have updated or added documentation

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.

Bug - Schema MinItems not being respected

1 participant