JSON Field Validation Improvements - #4558
Draft
kaise-lafrai wants to merge 1 commit into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 in
ProperJsonValidatorthe 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:
FormPostValidateservice 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]ProperJsonValidatorby extracting field context from validation errors and attaching it to constraint violations, enabling precise error mapping in the form. [1] [2].extractFieldFromPointer) found in theProperJsonValidatoris 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 usingsetParameterand later used in theFormPostValidateto set get the element and set the error. 1User Interface Improvements:
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
creatorsarray (with at least two authors), each having required fields (givenName,familyName,privateEmail), and added new fields likenumberwith validation constraints. [1] [2]Form Widget Integration:
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
QA Steps
Checklist before requesting review
If any of these are left unchecked, please provide an explanation