Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server side helper functions for DRY component consumptions #4905

Open
ttbarnes opened this issue Mar 26, 2024 · 0 comments
Open

Server side helper functions for DRY component consumptions #4905

ttbarnes opened this issue Mar 26, 2024 · 0 comments
Labels
awaiting triage Needs triaging by team feature request User requests a new feature

Comments

@ttbarnes
Copy link
Contributor

ttbarnes commented Mar 26, 2024

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:

  • Avoiding repitition.
  • Ensuring consistency.
  • Reducing lines of code in a service.
  • Reducing development time.

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.

@ttbarnes ttbarnes added awaiting triage Needs triaging by team feature request User requests a new feature labels Mar 26, 2024
@ttbarnes ttbarnes changed the title Server side helper functions Server side helper functions for DRY component consumptions Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting triage Needs triaging by team feature request User requests a new feature
Projects
None yet
Development

No branches or pull requests

1 participant