Server side helper functions for DRY component consumptions #4905
Labels
feature request
User requests a new feature
tooling
validation errors
Help users to recover from validation errors
Context
I've worked on many GOV services and noticed that some core pieces of logic required to consume some GOV components are duplicated throughout the services.
For example, to generate a validation error(s) and pass an array to the Error summary component, the same pieces of logic are required in individual services.
These pieces of logic basically consume a field ID and error message and are then mapped into a data structure required for a GOV component.
I imagine a large majority of services built with NodeJS must have similar pieces of logic.
Alternatives
It would be great to have DRY, generic helper functions in
govuk-frontend
to generate such data structures.This would improve multiple services and developer experiences by:
For example, there could be the following functions:
generateValidationError
- generate a data structure for a single validation error (E.g, for a form that has 1 form input).generateValidationErrors
- generate a data structure for multiple validation errors (E.g, for a form with multiple inputs).After calling these functions, the resulting data can then be easily passed to various GOV components to render validation errors.
With a "helpers" pattern in place, other helpers could also be added, for example to generate summary lists and task statuses, without individual services having to write their own logic. I want to propose a simple example first.
Is there appetite for such helper functions?
Additional information (if applicable)
I've created a proof of concept in my forked repo.
This demonstrates an example for a
generateValidationError
helper function and in the PR description, how this could be consumed in a service.Very interested to hear any thoughts on this. Would love to pursue and create a real PR.
If there is appetite for this, perhaps some new pattern pages could be created, for example to demonstrate how to generate validation with such helper functions.
A good example of how this would help a service is for a UKEF - Export Insurance Policies, where we currently have 98 imports for a
generateValidationErrors
function.The text was updated successfully, but these errors were encountered: