diff --git a/.github/workflows/dependency-review.yml b/.github/workflows/dependency-review.yml index fe461b4..d19e21b 100644 --- a/.github/workflows/dependency-review.yml +++ b/.github/workflows/dependency-review.yml @@ -1,20 +1,39 @@ # Dependency Review Action # -# This Action will scan dependency manifest files that change as part of a Pull Request, surfacing known-vulnerable versions of the packages declared or updated in the PR. Once installed, if the workflow run is marked as required, PRs introducing known-vulnerable packages will be blocked from merging. +# This Action will scan dependency manifest files that change as part of a Pull Request, +# surfacing known-vulnerable versions of the packages declared or updated in the PR. +# Once installed, if the workflow run is marked as required, PRs introducing known-vulnerable +# packages will be blocked from merging. # # Source repository: https://github.com/actions/dependency-review-action # Public documentation: https://docs.github.com/en/code-security/supply-chain-security/understanding-your-software-supply-chain/about-dependency-review#dependency-review-enforcement -name: 'Dependency Review' -on: [pull_request] +name: 'Dependency review' +on: + pull_request: + branches: [ "main" ] +# If using a dependency submission action in this workflow this permission will need to be set to: +# +# permissions: +# contents: write +# +# https://docs.github.com/en/enterprise-cloud@latest/code-security/supply-chain-security/understanding-your-software-supply-chain/using-the-dependency-submission-api permissions: contents: read + # Write permissions for pull-requests are required for using the `comment-summary-in-pr` option, comment out if you aren't using this option + pull-requests: write jobs: dependency-review: runs-on: ubuntu-latest steps: - - name: 'Checkout Repository' - uses: actions/checkout@v3 + - name: 'Checkout repository' + uses: actions/checkout@v4 - name: 'Dependency Review' - uses: actions/dependency-review-action@v2 + uses: actions/dependency-review-action@v4 + # Commonly enabled options, see https://github.com/actions/dependency-review-action#configuration-options for all available options. + with: + comment-summary-in-pr: always + # fail-on-severity: moderate + # deny-licenses: GPL-1.0-or-later, LGPL-2.0-or-later + # retry-on-snapshot-warnings: true diff --git a/govuk_frontend_wtf/gov_form_base.py b/govuk_frontend_wtf/gov_form_base.py index 2941a02..f35f4cb 100644 --- a/govuk_frontend_wtf/gov_form_base.py +++ b/govuk_frontend_wtf/gov_form_base.py @@ -28,7 +28,7 @@ def map_gov_params(self, field, **kwargs): "name": field.name, "label": {"text": field.label.text}, "attributes": {}, - "hint": {"text": field.description}, + "hint": {"text": field.description} if field.description else None, } if "value" in kwargs: diff --git a/govuk_frontend_wtf/main.py b/govuk_frontend_wtf/main.py index 81e60a4..dd76f9b 100644 --- a/govuk_frontend_wtf/main.py +++ b/govuk_frontend_wtf/main.py @@ -20,21 +20,29 @@ def init_app(self, app): def wtforms_errors(form, params={}): wtforms_params = {"titleText": "There is a problem", "errorList": []} - wtforms_params["errorList"] = flatten_errors(form.errors) + id_map = {} + for field_name in form._fields.keys(): + field = getattr(form, field_name, None) + if field and hasattr(field, "id"): + id_map[field_name] = field.id + + wtforms_params["errorList"] = flatten_errors(form.errors, id_map=id_map) return merger.merge(wtforms_params, params) -def flatten_errors(errors, prefix=""): +def flatten_errors(errors, prefix="", id_map={}): """Return list of errors from form errors.""" error_list = [] if isinstance(errors, dict): for key, value in errors.items(): # Recurse to handle subforms. - error_list += flatten_errors(value, prefix=f"{prefix}{key}-") + if key in id_map: + key = id_map[key] + error_list += flatten_errors(value, prefix=f"{prefix}{key}-", id_map=id_map) elif isinstance(errors, list) and isinstance(errors[0], dict): for idx, error in enumerate(errors): - error_list += flatten_errors(error, prefix=f"{prefix}{idx}-") + error_list += flatten_errors(error, prefix=f"{prefix}{idx}-", id_map=id_map) elif isinstance(errors, list): error_list.append({"text": errors[0], "href": "#{}".format(prefix.rstrip("-"))}) else: diff --git a/tests/fixtures/wtf_widgets_data.yaml b/tests/fixtures/wtf_widgets_data.yaml index 8c01270..d378d06 100644 --- a/tests/fixtures/wtf_widgets_data.yaml +++ b/tests/fixtures/wtf_widgets_data.yaml @@ -37,6 +37,44 @@ TestStringField: -