Skip to content

Add and export JSON and YAML file loaders#55

Merged
davidjamesstone merged 12 commits intomainfrom
feature/543861-yaml
Apr 24, 2025
Merged

Add and export JSON and YAML file loaders#55
davidjamesstone merged 12 commits intomainfrom
feature/543861-yaml

Conversation

@davidjamesstone
Copy link
Copy Markdown
Contributor

@davidjamesstone davidjamesstone commented Apr 22, 2025

Proposed change

Adds some convenient classes to remove the boilerplate involved when loading files from disk.
Supports both JSON definitions and YAML.

Usage:

import { FileFormService } from '@defra/forms-engine-plugin/file-form-service.js'

// Create shared form metadata
const now = new Date()
const user = { id: 'user', displayName: 'Username' }
const author = { createdAt: now, createdBy: user, updatedAt: now, updatedBy: user }
const metadata = {
  organisation: 'Defra',
  teamName: 'Team name',
  teamEmail: 'team@defra.gov.uk',
  submissionGuidance: "Thanks for your submission, we'll be in touch",
  notificationEmail: 'email@domain.com',
  ...author,
  live: author
}

// Instantiate the file loader form service
const loader = new FileFormService()

// Add a Json form
await loader.addForm(
  'src/definitions/example-form.json', {
    ...metadata,
    id: '95e92559-968d-44ae-8666-2b1ad3dffd31',
    title: 'Example Json',
    slug: 'example-json'
  }
)

// Add a Yaml form
await loader.addForm(
  'src/definitions/example-form.yaml', {
    ...metadata,
    id: '641aeafd-13dd-40fa-9186-001703800efb',
    title: 'Example Yaml',
    slug: 'example-yaml'
  }
)

// Get the forms service
const formsService = loader.toFormsService()

Azure DevOps work item: https://dev.azure.com/defragovuk/DEFRA-CDP/_workitems/edit/543861

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Misc. (documentation, build updates, etc)

Checklist

  • You have executed this code locally and it performs as expected.
  • You have added tests to verify your code works.
  • You have added code comments and JSDoc, where appropriate.
  • There is no commented-out code.
  • You have added developer docs in README.md and docs/* (where appropriate, e.g. new features).
  • The tests are passing (npm run test).
  • The linting checks are passing (npm run lint).
  • The code has been formatted (npm run format).

@davidjamesstone davidjamesstone marked this pull request as ready for review April 24, 2025 12:18

// Add a Yaml form
await loader.addForm(
'src/definitions/example-form.json', {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this normally have a .yaml extension?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jbarnsley10

Copy link
Copy Markdown
Contributor

@jbarnsley10 jbarnsley10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice. Just that typo in the README to do with yaml extension

@sonarqubecloud
Copy link
Copy Markdown

@davidjamesstone davidjamesstone merged commit 246cd47 into main Apr 24, 2025
9 checks passed
@davidjamesstone davidjamesstone deleted the feature/543861-yaml branch April 24, 2025 13:00
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.

3 participants