Skip to content

feat(#528): validate branding and partners configuration before upload - #836

Open
AchrafReyani wants to merge 3 commits into
medic:mainfrom
AchrafReyani:feat/validate-branding-partners
Open

feat(#528): validate branding and partners configuration before upload#836
AchrafReyani wants to merge 3 commits into
medic:mainfrom
AchrafReyani:feat/validate-branding-partners

Conversation

@AchrafReyani

Copy link
Copy Markdown

Description

upload-branding and upload-partners currently accept whatever is in branding.json / partners.json and upload it verbatim as the branding / partners doc. cht-core assumes a specific structure for these docs (api/src/services/branding.js, the admin images-branding / images-partners controllers): a string title, and a resources object whose values are attachment names. A malformed doc breaks the admin images pages without any indication of the cause (see the issue).

This PR validates both configuration files before anything is uploaded:

  • Schema validation (joi, same approach as validate-app-settings.js):
    • branding.json: title is a required non-empty string; resources (optional) is an object whose logo / favicon / icon values are non-empty strings — the three keys cht-core reads.
    • partners.json: resources is a required object mapping partner name → non-empty string.
  • Attachment check: every resources value must reference a file that exists in the branding/ or partners/ directory, otherwise cht-core would look up an attachment that was never uploaded.

On failure the action throws a descriptive error and nothing is written, e.g.

Invalid branding configuration in /project/branding.json: "title" must be a string; "resources" must be of type object
Invalid branding configuration in /project/branding.json: "resources.favicon" references "fav.ico" but no such file was found
Invalid partners configuration in /project/partners.json: "resources" must be of type object

Implementation: new src/lib/validate-configuration-docs.js; upload-configuration-docs takes an optional validate(settings, attachments) callback (alongside the existing optional processJson), and the two actions pass their validator. Other callers of upload-configuration-docs are unaffected.

#528

Code review items

  • Readable: Concise, well named, follows the style guide, documented if necessary.
  • Documented: The accepted structure is already what cht-docs describes; happy to add a note about the new validation errors there if you'd like.
  • Tested: 17 new unit tests in test/lib/validate-configuration-docs.spec.js plus new cases in test/lib/upload-configuration-docs.spec.js (validate accepted / rejected → throws, no upload); action specs assert the validator is wired in.

Verification

  • Reproduced on main with the upload module stubbed: { "title": 42, "resources": "logo.png" } is uploaded as-is. After this change the same input throws the first error above and insertOrReplace is never called; a valid config still uploads the same doc as before.
  • npm run eslint clean.
  • npm test in a clean node:22 container with FORCE_COLOR=1 (same as CI): 899 passing, 1 pending (the pending one is pre-existing), 0 failing.

Written with assistance from Claude Code; every change and test result above was run and checked locally.

…upload

upload-branding and upload-partners accepted any JSON and uploaded it as
the branding/partners doc, even though cht-core assumes a specific
structure (a string title, a resources object mapping to attachment
names). A malformed doc breaks the admin images pages without any
indication of the cause.

Validate branding.json and partners.json with joi before uploading and
also check that every resources entry references a file that exists in
the branding/ or partners/ directory. Invalid configuration now fails
fast with a descriptive error and nothing is uploaded.
- pass processJson/validate to upload-configuration-docs as an options
  object instead of a fifth positional parameter
- name the exported function and extract assertValidConfiguration and
  uploadDoc helpers to bring cognitive complexity under the threshold
- use optional chaining in validate-configuration-docs
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.

1 participant