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

feat(application-system): Make required a dynamic field #16691

Merged
merged 3 commits into from
Nov 1, 2024

Conversation

norda-gunni
Copy link
Member

@norda-gunni norda-gunni commented Nov 1, 2024

...

Attach a link to issue if relevant

What

Specify what you're trying to achieve

Why

Specify why you need to achieve this

Screenshots / Gifs

Attach Screenshots / Gifs to help reviewers understand the scope of the pull request

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • Formatting passes locally with my changes
  • I have rebased against main before asking for a review

Summary by CodeRabbit

  • New Features

    • Introduced a new function to determine field requirements based on application context.
    • Added new types and interfaces to enhance flexibility in field definitions.
  • Improvements

    • Updated multiple form field components to utilize the new function for evaluating required fields, enhancing contextual responsiveness.
  • Bug Fixes

    • Ensured consistent handling of required properties across various input types.

Copy link
Contributor

coderabbitai bot commented Nov 1, 2024

Walkthrough

The changes in this pull request introduce a new function, buildFieldRequired, to determine if a field is required based on the application context. This function is integrated into various form field components, replacing direct assignments of the required prop with calls to buildFieldRequired. Additionally, new types and interfaces are defined in Fields.ts, enhancing the flexibility of field definitions. The modifications ensure that the required state of fields can now incorporate application-specific logic while maintaining existing functionalities across components.

Changes

File Path Change Summary
libs/application/core/src/lib/fieldBuilders.ts Added function buildFieldRequired(application: Application, maybeRequired?: MaybeWithApplication<boolean>). Updated import statement for MaybeWithApplication.
libs/application/types/src/lib/Fields.ts Introduced new type MaybeWithApplication<T>. Updated BaseField interface to include defaultValue. Added InputField interface. Modified various field interfaces to extend from InputField.
libs/application/ui-fields/src/lib/AsyncSelectFormField/AsyncSelectFormField.tsx Updated SelectController to use buildFieldRequired(application, required) instead of direct required prop.
libs/application/ui-fields/src/lib/CompanySearchFormField/CompanySearchFormField.tsx Imported buildFieldRequired and updated CompanySearchController to utilize it for the required prop.
libs/application/ui-fields/src/lib/DateFormField/DateFormField.tsx Imported buildFieldRequired and updated DatePickerController to use it for the required prop.
libs/application/ui-fields/src/lib/NationalIdWithNameFormField/NationalIdWithNameFormField.tsx Updated NationalIdWithName to use buildFieldRequired(application, field.required) for the required prop.
libs/application/ui-fields/src/lib/PhoneFormField/PhoneFormField.tsx Imported buildFieldRequired and updated PhoneInputController to use it for the required prop.
libs/application/ui-fields/src/lib/SelectFormField/SelectFormField.tsx Imported buildFieldRequired and updated SelectController to use it for the required prop.
libs/application/ui-fields/src/lib/TextFormField/TextFormField.tsx Imported buildFieldRequired and updated InputController to use it for the required prop.
libs/application/core/src/lib/fieldBuilders.spec.ts Introduced unit tests for buildFieldOptions and buildFieldRequired, validating their behavior with various inputs.

Suggested labels

automerge

Suggested reviewers

  • rafnarnason

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Nov 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 36.64%. Comparing base (63a12a7) to head (24612d0).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #16691   +/-   ##
=======================================
  Coverage   36.63%   36.64%           
=======================================
  Files        6873     6871    -2     
  Lines      143132   143051   -81     
  Branches    40805    40779   -26     
=======================================
- Hits        52436    52418   -18     
+ Misses      90696    90633   -63     
Flag Coverage Δ
air-discount-scheme-web 0.00% <ø> (ø)
api 3.37% <ø> (ø)
application-api-files 56.78% <ø> (ø)
application-core 71.93% <100.00%> (+0.75%) ⬆️
application-system-api 41.33% <20.00%> (-0.01%) ⬇️
application-template-api-modules 27.81% <20.00%> (-0.01%) ⬇️
application-templates-accident-notification 29.21% <20.00%> (-0.03%) ⬇️
application-templates-car-recycling 3.12% <ø> (ø)
application-templates-criminal-record 26.26% <20.00%> (-0.04%) ⬇️
application-templates-driving-license 18.40% <20.00%> (+<0.01%) ⬆️
application-templates-estate 12.19% <20.00%> (+<0.01%) ⬆️
application-templates-example-payment 25.17% <20.00%> (-0.04%) ⬇️
application-templates-financial-aid 15.56% <20.00%> (+0.01%) ⬆️
application-templates-general-petition 23.40% <20.00%> (-0.02%) ⬇️
application-templates-inheritance-report 6.52% <20.00%> (+0.02%) ⬆️
application-templates-marriage-conditions 15.20% <20.00%> (+0.02%) ⬆️
application-templates-mortgage-certificate 43.52% <20.00%> (-0.07%) ⬇️
application-templates-parental-leave 29.93% <20.00%> (-0.02%) ⬇️
application-types 6.62% <ø> (ø)
application-ui-components 1.27% <ø> (ø)
application-ui-shell 20.87% <20.00%> (-0.01%) ⬇️
clients-charge-fjs-v2 24.11% <ø> (ø)
judicial-system-api 19.57% <ø> (ø)
web 1.80% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
libs/application/core/src/lib/fieldBuilders.ts 41.14% <100.00%> (+3.71%) ⬆️
libs/application/types/src/lib/Fields.ts 100.00% <ø> (ø)
.../lib/AsyncSelectFormField/AsyncSelectFormField.tsx 3.70% <ø> (ø)
.../CompanySearchFormField/CompanySearchFormField.tsx 7.69% <ø> (ø)
.../ui-fields/src/lib/DateFormField/DateFormField.tsx 3.12% <ø> (ø)
...dWithNameFormField/NationalIdWithNameFormField.tsx 50.00% <ø> (ø)
...i-fields/src/lib/PhoneFormField/PhoneFormField.tsx 5.88% <ø> (ø)
...fields/src/lib/SelectFormField/SelectFormField.tsx 5.88% <ø> (ø)
.../ui-fields/src/lib/TextFormField/TextFormField.tsx 5.55% <ø> (ø)

... and 20 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 63a12a7...24612d0. Read the comment docs.

@datadog-island-is
Copy link

datadog-island-is bot commented Nov 1, 2024

Datadog Report

All test runs 29ca988 🔗

22 Total Test Services: 0 Failed, 21 Passed
🔻 Test Sessions change in coverage: 11 decreased, 4 increased, 78 no change

Test Services
This report shows up to 10 services
Service Name Failed Known Flaky New Flaky Passed Skipped Total Time Code Coverage Change Test Service View
air-discount-scheme-web 0 0 0 2 0 6.97s 1 no change Link
api 0 0 0 4 0 2.4s 1 no change Link
application-api-files 0 0 0 12 0 5.31s 1 no change Link
application-core 0 0 0 97 0 14.64s 1 increased (+0.58%) Link
application-system-api 0 0 0 120 2 3m 19.19s 1 no change Link
application-template-api-modules 0 0 0 123 0 2m 1.03s 1 no change Link
application-templates-accident-notification 0 0 0 148 0 15.25s 1 decreased (-0.03%) Link
application-templates-criminal-record 0 0 0 2 0 9.69s 1 decreased (-0.05%) Link
application-templates-driving-license 0 0 0 13 0 13.02s 1 no change Link
application-templates-example-payment 0 0 0 2 0 10.3s 1 decreased (-0.04%) Link

🔻 Code Coverage Decreases vs Default Branch (11)

This report shows up to 5 code coverage decreases.

  • application-templates-mortgage-certificate - jest 52.61% (-0.07%) - Details
  • application-templates-criminal-record - jest 32.85% (-0.05%) - Details
  • application-templates-example-payment - jest 31.72% (-0.04%) - Details
  • application-templates-social-insurance-administration-household-supplement - jest 33.15% (-0.04%) - Details
  • application-templates-social-insurance-administration-additional-support-for-the-elderly - jest 30.97% (-0.03%) - Details

@norda-gunni norda-gunni marked this pull request as ready for review November 1, 2024 11:09
@norda-gunni norda-gunni requested a review from a team as a code owner November 1, 2024 11:09
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (3)
libs/application/ui-fields/src/lib/AsyncSelectFormField/AsyncSelectFormField.tsx (1)

68-68: Consider adding type annotation for the application parameter

The implementation correctly uses buildFieldRequired to make the required state dynamic. However, to improve type safety and maintainability, consider adding a type annotation for the application parameter.

-  required={buildFieldRequired(application, required)}
+  required={buildFieldRequired(application as Application, required)}

Don't forget to import the Application type:

import { Application } from '@island.is/application/types'
libs/application/core/src/lib/fieldBuilders.ts (1)

540-548: Add JSDoc documentation for consistency.

The function implementation looks good, but it's missing documentation unlike other functions in the file. Consider adding JSDoc to explain the parameters and return value.

Add documentation like this:

+/**
+ * Determines if a field is required based on the application context.
+ * 
+ * @param {Application} application - The current application state
+ * @param {MaybeWithApplication<boolean>} [maybeRequired] - Static boolean or function that determines if field is required
+ * @returns {boolean | undefined} - Whether the field is required
+ */
export const buildFieldRequired = (
  application: Application,
  maybeRequired?: MaybeWithApplication<boolean>,
) => {
libs/application/types/src/lib/Fields.ts (1)

200-201: Address the TODO for non-schema validation

The TODO comment suggests implementing a validation function for non-schema validation. Addressing this would enhance validation capabilities beyond the schema.

Would you like assistance in implementing this validation function or opening a GitHub issue to track this task?

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d0c9471 and 93acf5b.

📒 Files selected for processing (9)
  • libs/application/core/src/lib/fieldBuilders.ts (2 hunks)
  • libs/application/types/src/lib/Fields.ts (8 hunks)
  • libs/application/ui-fields/src/lib/AsyncSelectFormField/AsyncSelectFormField.tsx (2 hunks)
  • libs/application/ui-fields/src/lib/CompanySearchFormField/CompanySearchFormField.tsx (2 hunks)
  • libs/application/ui-fields/src/lib/DateFormField/DateFormField.tsx (2 hunks)
  • libs/application/ui-fields/src/lib/NationalIdWithNameFormField/NationalIdWithNameFormField.tsx (2 hunks)
  • libs/application/ui-fields/src/lib/PhoneFormField/PhoneFormField.tsx (2 hunks)
  • libs/application/ui-fields/src/lib/SelectFormField/SelectFormField.tsx (2 hunks)
  • libs/application/ui-fields/src/lib/TextFormField/TextFormField.tsx (2 hunks)
🧰 Additional context used
📓 Path-based instructions (9)
libs/application/core/src/lib/fieldBuilders.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/application/types/src/lib/Fields.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/application/ui-fields/src/lib/AsyncSelectFormField/AsyncSelectFormField.tsx (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/application/ui-fields/src/lib/CompanySearchFormField/CompanySearchFormField.tsx (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/application/ui-fields/src/lib/DateFormField/DateFormField.tsx (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/application/ui-fields/src/lib/NationalIdWithNameFormField/NationalIdWithNameFormField.tsx (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/application/ui-fields/src/lib/PhoneFormField/PhoneFormField.tsx (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/application/ui-fields/src/lib/SelectFormField/SelectFormField.tsx (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/application/ui-fields/src/lib/TextFormField/TextFormField.tsx (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
🔇 Additional comments (26)
libs/application/ui-fields/src/lib/NationalIdWithNameFormField/NationalIdWithNameFormField.tsx (2)

1-1: LGTM! Clean import addition.

The import is well-structured and follows good practices for tree-shaking optimization.


21-21: Verify the buildFieldRequired implementation.

The change aligns with making the required field dynamic and maintains type safety. However, let's verify the implementation of buildFieldRequired to ensure it handles all edge cases.

✅ Verification successful

Let me gather more information about the types and usage patterns to ensure complete verification.


buildFieldRequired implementation is correct and consistently used

The implementation in libs/application/core/src/lib/fieldBuilders.ts properly handles both static boolean values and dynamic functions through the MaybeWithApplication<boolean> type. The function is consistently used across multiple form field components (Phone, Text, Select, NationalId, Date, Company, AsyncSelect) with the same pattern of passing the application and required field property.

The type definition in libs/application/types/src/lib/Fields.ts confirms proper type safety:

type MaybeWithApplication<T> = T | ((application: Application) => T)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify buildFieldRequired implementation and its usage across the codebase

# Test 1: Check buildFieldRequired implementation
echo "Checking buildFieldRequired implementation..."
ast-grep --pattern 'export const buildFieldRequired = $_'

# Test 2: Verify consistent usage across other form fields
echo "Verifying consistent usage across form fields..."
rg "buildFieldRequired\(application, .*\.required\)" --type ts

Length of output: 1183


Script:

#!/bin/bash
# Check the type definition of MaybeWithApplication
echo "Checking MaybeWithApplication type definition..."
rg "type MaybeWithApplication" -A 3 --type ts

# Check other usages of buildFieldRequired to ensure consistent patterns
echo "Checking all buildFieldRequired usages..."
rg "buildFieldRequired\(" -B 1 -A 1 --type ts

# Look for any tests related to buildFieldRequired
echo "Checking for related tests..."
rg "buildFieldRequired" --type test

Length of output: 3422

libs/application/ui-fields/src/lib/CompanySearchFormField/CompanySearchFormField.tsx (1)

6-10: LGTM! Clean import organization.

The imports are well-organized and follow good practices:

  • Grouped with related core utilities
  • Named imports support tree-shaking
  • Shared from core package enabling reuse across apps
libs/application/ui-fields/src/lib/PhoneFormField/PhoneFormField.tsx (2)

3-3: LGTM! Clean import addition.

The import is well-organized and tree-shakeable, bundling related utilities from the core module.


78-78: LGTM! Dynamic required validation implemented correctly.

The change enhances field validation by making it context-aware while maintaining type safety and component reusability.

Let's verify that this pattern is consistently applied across other form fields:

✅ Verification successful

Dynamic required validation is consistently implemented across form fields

The verification confirms that buildFieldRequired(application, required) is consistently used across all form field components in the codebase:

  • AsyncSelectFormField
  • CompanySearchFormField
  • DateFormField
  • PhoneFormField
  • SelectFormField
  • TextFormField
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify consistent usage of buildFieldRequired across form fields
# Expected: All form field components should use buildFieldRequired similarly

# Search for buildFieldRequired usage in form field components
rg -A 2 "buildFieldRequired\(application, required\)" "libs/application/ui-fields/src/lib/"

Length of output: 2273

libs/application/ui-fields/src/lib/SelectFormField/SelectFormField.tsx (2)

7-7: LGTM: Import follows best practices

The import is properly organized with related utility functions and maintains tree-shakeability through named imports.


57-57: 🛠️ Refactor suggestion

Verify default value logic with dynamic required state

While the required prop now uses buildFieldRequired for dynamic validation, the default value logic still uses the static required boolean. This might lead to inconsistent behavior.

Let's check if other form fields have updated their default value logic:

Consider updating the default value logic to use buildFieldRequired:

          required={buildFieldRequired(application, required)}
          defaultValue={
            (getValueViaPath(application.answers, id) ??
              getDefaultValue(field, application)) ||
-            (required ? '' : undefined)
+            (buildFieldRequired(application, required) ? '' : undefined)
          }

Also applies to: 60-64

libs/application/ui-fields/src/lib/TextFormField/TextFormField.tsx (2)

4-4: LGTM! Import structure maintains tree-shaking efficiency

The import is properly structured to allow effective tree-shaking, and the placement in the application/core package is appropriate for shared functionality.


93-93: Verify form validation behavior with dynamic required state

The change to use buildFieldRequired enhances flexibility but warrants verification of form validation behavior.

Let's verify other form field components for consistency:

Consider adding a comment explaining the dynamic required behavior for future maintainers:

          backgroundColor={backgroundColor}
          rows={rows}
+         // Required state is determined dynamically based on application context
          required={buildFieldRequired(application, required)}
          rightAlign={rightAlign}
✅ Verification successful

Form validation behavior with dynamic required state is correctly implemented

The verification shows that:

  • buildFieldRequired is consistently used across all form field components in the ui-fields library
  • The function properly handles both static boolean and dynamic function-based required states
  • The implementation is type-safe and follows the same pattern in all form components

The suggested documentation comment is not necessary as this is a standard pattern used consistently throughout the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other form field components to verify consistent usage
rg -l "buildFieldRequired" "libs/application/ui-fields/src"

Length of output: 592


Script:

#!/bin/bash
# Check the implementation of buildFieldRequired function and its usage across components
rg "buildFieldRequired" "libs/application/ui-fields/src" -A 2 -B 2

Length of output: 7944


Script:

#!/bin/bash
# Check the implementation of buildFieldRequired function in the core package
rg "buildFieldRequired" "libs/application/core/src" -A 5 -B 2

Length of output: 641

libs/application/ui-fields/src/lib/AsyncSelectFormField/AsyncSelectFormField.tsx (2)

3-3: LGTM: Import is correctly structured for tree-shaking

The import is properly organized, combining related functions from the same module.


Line range hint 1-99: Component meets reusability requirements

The component is well-structured for reuse across NextJS apps:

  • Uses shared UI components and form fields
  • Properly typed with TypeScript interfaces
  • Clear separation of concerns
  • No direct NextJS dependencies
libs/application/ui-fields/src/lib/DateFormField/DateFormField.tsx (2)

3-7: LGTM! Import changes follow best practices

The addition of buildFieldRequired follows proper tree-shaking practices by using named imports and is appropriately grouped with related core utilities.


131-131: Verify consistent implementation across form fields

The change to use buildFieldRequired aligns with making the required state dynamic. However, let's verify this pattern is consistently implemented across other form fields.

✅ Verification successful

Implementation of buildFieldRequired is consistent across form fields

The verification shows that buildFieldRequired is consistently implemented across relevant form field components that require validation:

  • AsyncSelectFormField
  • CompanySearchFormField
  • DateFormField
  • NationalIdWithNameFormField
  • PhoneFormField
  • SelectFormField
  • TextFormField

All these components follow the same pattern of using buildFieldRequired(application, required) for their required prop, maintaining consistency with the dynamic required state implementation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify consistent implementation of buildFieldRequired across form fields
# Expected: All form field components should use buildFieldRequired for required prop

# Search for DatePickerController required prop usage
echo "Checking DatePickerController required prop implementations:"
ast-grep --pattern 'DatePickerController {
  $$$
  required={$_}
  $$$
}'

# Search for other potential form field components that might need this change
echo -e "\nChecking other form components for required prop implementations:"
fd -e tsx -e ts . libs/application/ui-fields/src/lib/ --exec rg -l 'required.*=.*{(?!buildFieldRequired).*}'

Length of output: 5632


Script:

#!/bin/bash
# Let's try a different approach to find form field components and their required prop usage

# First, find all form field components
echo "Form field components in the library:"
fd -e tsx -e ts . libs/application/ui-fields/src/lib/

# Then search for required prop patterns in these files
echo -e "\nChecking required prop implementations:"
rg "required\s*=\s*{" libs/application/ui-fields/src/lib/ -A 1

# Also search for buildFieldRequired usage
echo -e "\nChecking buildFieldRequired usage:"
rg "buildFieldRequired" libs/application/ui-fields/src/lib/

Length of output: 5428

libs/application/core/src/lib/fieldBuilders.ts (1)

47-47: LGTM! Import follows established patterns.

The addition of MaybeWithApplication type import is well-placed with related type imports.

libs/application/types/src/lib/Fields.ts (12)

26-27: Introduction of MaybeWithApplication types enhances field flexibility

Adding MaybeWithApplication<T> and MaybeWithApplicationAndField<T> types allows field properties to be defined as static values or functions dependent on the application context, increasing the dynamic capability of field definitions.


205-207: Refactoring fields to extend InputField promotes consistency

Introducing InputField with the required property and having other fields extend it standardizes the handling of required fields across the application, improving code maintainability.


280-289: CheckboxField now extends InputField

By extending InputField, CheckboxField now uniformly handles the required property, enhancing consistency among input fields.


Line range hint 291-304: DateField now extends InputField

The change allows DateField to utilize the inherited required property, promoting consistent behavior.


315-325: RadioField now extends InputField

Extending InputField ensures RadioField consistently incorporates the required property.


327-334: SelectField now extends InputField

This update standardizes the handling of the required property within SelectField.


337-344: CompanySearchField now extends InputField

By inheriting from InputField, CompanySearchField includes the required property, enhancing consistency.


Line range hint 346-356: AsyncSelectField now extends InputField

This change ensures that AsyncSelectField consistently handles the required property.


Line range hint 358-376: TextField now extends InputField

Extending InputField allows TextField to uniformly utilize the required property.


Line range hint 378-387: PhoneField now extends InputField

The change standardizes the required property handling in PhoneField.


Line range hint 560-571: NationalIdWithNameField now extends InputField

By extending InputField, NationalIdWithNameField inherits the required property, promoting consistency.


Line range hint 636-648: FindVehicleField now extends InputField

This update incorporates the required property into FindVehicleField by extending InputField.

@norda-gunni norda-gunni requested a review from Toti91 November 1, 2024 11:20
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
libs/application/core/src/lib/fieldBuilders.spec.ts (2)

1-6: Consider consolidating imports from the same module.

The three separate imports from '@island.is/application/types' can be combined into a single import statement for better maintainability.

-import { Field } from '@island.is/application/types'
-import { FieldComponents, FieldTypes } from '@island.is/application/types'
-import { Application } from '@island.is/application/types'
+import { Application, Field, FieldComponents, FieldTypes } from '@island.is/application/types'

8-46: Consider adding edge case tests for buildFieldOptions.

While the current tests cover the main scenarios well, consider adding tests for:

  • Undefined/null options
  • Empty array options
  • Invalid option shapes

Example additional test:

it('should handle undefined options gracefully', () => {
  const result = buildFieldOptions(undefined, mockApplication, mockField)
  expect(result).toEqual([]) // or whatever the expected behavior is
})
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 93acf5b and da64938.

📒 Files selected for processing (2)
  • libs/application/core/src/lib/fieldBuilders.spec.ts (1 hunks)
  • libs/application/core/src/lib/fieldBuilders.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • libs/application/core/src/lib/fieldBuilders.ts
🧰 Additional context used
📓 Path-based instructions (1)
libs/application/core/src/lib/fieldBuilders.spec.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
🔇 Additional comments (2)
libs/application/core/src/lib/fieldBuilders.spec.ts (2)

48-72: Well-structured and comprehensive tests for buildFieldRequired!

The test suite effectively covers:

  • Static boolean values
  • Undefined input
  • Dynamic function-based required field validation
  • Proper validation of function calls with correct arguments

The implementation follows TypeScript best practices and provides good test coverage.


1-72: Overall excellent test implementation!

The test file demonstrates:

  • Proper TypeScript usage with clear type definitions
  • Comprehensive test coverage for utility functions
  • Good separation of concerns and test organization
  • Reusable test utilities and mock objects

This implementation aligns well with the coding guidelines for library code.

Copy link
Member

@Toti91 Toti91 left a comment

Choose a reason for hiding this comment

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

nice

@norda-gunni norda-gunni added automerge Merge this PR as soon as all checks pass and removed automerge Merge this PR as soon as all checks pass labels Nov 1, 2024
@kodiakhq kodiakhq bot merged commit dc89848 into main Nov 1, 2024
88 checks passed
@kodiakhq kodiakhq bot deleted the as-dynamic-required branch November 1, 2024 13:20
@coderabbitai coderabbitai bot mentioned this pull request Nov 26, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Merge this PR as soon as all checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants