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

fix(j-s): Validation fix #16930

Merged
merged 23 commits into from
Nov 19, 2024
Merged

fix(j-s): Validation fix #16930

merged 23 commits into from
Nov 19, 2024

Conversation

oddsson
Copy link
Member

@oddsson oddsson commented Nov 18, 2024

Validation fix

Asana

What

In this pr a server side input validation is implemented. There was a slight mistake in the validator for string arrays that caused the e2e tests to fail. This PR fixes that.

Why

So that the e2e tests work again.

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

    • Enhanced validation for police case numbers in both case creation and update processes, ensuring each entry in the array adheres to a maximum length of 255 characters.
  • Bug Fixes

    • Corrected validation logic for police case numbers to apply constraints on individual elements rather than the entire array.

@oddsson oddsson requested review from a team as code owners November 18, 2024 21:35
@oddsson oddsson changed the title fix(j-s): Validation fox fix(j-s): Validation fix Nov 18, 2024
Copy link
Contributor

coderabbitai bot commented Nov 18, 2024

Walkthrough

The changes in this pull request involve modifications to the validation logic of the policeCaseNumbers property in both the CreateCaseDto and UpdateCaseDto classes. Specifically, the maximum length constraint for each string in the policeCaseNumbers array has been updated to enforce a limit of 255 characters per element, rather than for the array as a whole. This adjustment is implemented using the { each: true } option in the @MaxLength decorator.

Changes

File Change Summary
apps/judicial-system/backend/src/app/modules/case/dto/createCase.dto.ts Updated policeCaseNumbers to use @MaxLength(255, { each: true }) for individual string validation.
apps/judicial-system/backend/src/app/modules/case/dto/updateCase.dto.ts Updated policeCaseNumbers to use @MaxLength(255, { each: true }) for individual string validation.

Possibly related PRs

  • fix(j-s): String Length Validation #16924: This PR directly modifies the CreateCaseDto class to enforce a maximum length of 255 characters for the policeCaseNumbers property, aligning with the changes made in the main PR.

Suggested labels

automerge

Suggested reviewers

  • gudjong

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a2b25ec and 2f78c67.

📒 Files selected for processing (2)
  • apps/judicial-system/backend/src/app/modules/case/dto/createCase.dto.ts (1 hunks)
  • apps/judicial-system/backend/src/app/modules/case/dto/updateCase.dto.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
apps/judicial-system/backend/src/app/modules/case/dto/createCase.dto.ts (1)

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

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/judicial-system/backend/src/app/modules/case/dto/updateCase.dto.ts (1)

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

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
📓 Learnings (2)
apps/judicial-system/backend/src/app/modules/case/dto/createCase.dto.ts (1)
Learnt from: gudjong
PR: island-is/island.is#16924
File: apps/judicial-system/backend/src/app/modules/case/dto/updateCase.dto.ts:69-74
Timestamp: 2024-11-18T15:21:28.715Z
Learning: The `policeCaseNumbers` field in `UpdateCaseDto` in `apps/judicial-system/backend/src/app/modules/case/dto/updateCase.dto.ts` already includes the `@ArrayMinSize(1)` and `@MaxLength(255)` decorators for validation.
apps/judicial-system/backend/src/app/modules/case/dto/updateCase.dto.ts (1)
Learnt from: gudjong
PR: island-is/island.is#16924
File: apps/judicial-system/backend/src/app/modules/case/dto/updateCase.dto.ts:69-74
Timestamp: 2024-11-18T15:21:28.715Z
Learning: The `policeCaseNumbers` field in `UpdateCaseDto` in `apps/judicial-system/backend/src/app/modules/case/dto/updateCase.dto.ts` already includes the `@ArrayMinSize(1)` and `@MaxLength(255)` decorators for validation.
🔇 Additional comments (3)
apps/judicial-system/backend/src/app/modules/case/dto/createCase.dto.ts (2)

43-43: LGTM! Validation fix properly implemented.

The addition of { each: true } to @MaxLength correctly enforces the 255-character limit on each police case number individually, rather than on the entire array. This is the proper validation approach for an array of identifiers.


43-45: Verify consistency with UpdateCaseDto validation.

The validation stack for policeCaseNumbers matches the learnings from PR #16924, ensuring consistent validation behavior across both DTOs.

✅ Verification successful

Let me try a different approach to verify the validation consistency between the DTOs.


Validation for policeCaseNumbers is consistent between DTOs

The validation stack for policeCaseNumbers is identical in both DTOs:

  • CreateCaseDto (required): @MaxLength(255, { each: true }), @IsString({ each: true }), @IsArray(), @ArrayMinSize(1)
  • UpdateCaseDto (optional): @MaxLength(255, { each: true }), @IsString({ each: true }), @IsArray(), @ArrayMinSize(1)

The only difference is that the field is required in CreateCaseDto and optional in UpdateCaseDto, which is the expected behavior for update operations.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that both DTOs have identical validation for policeCaseNumbers

# Search for policeCaseNumbers validation in both DTOs
echo "Comparing validation in CreateCaseDto and UpdateCaseDto:"
ast-grep --pattern 'readonly policeCaseNumbers!: string[]' -A 5

Length of output: 188


Script:

#!/bin/bash
# Search for files containing 'CaseDto' in their names
echo "Finding relevant DTO files:"
fd "Case.*dto" --type f

# Display the content of both DTOs focusing on validation decorators
echo -e "\nChecking validation decorators in both DTOs:"
rg "@(MaxLength|IsString|ApiProperty)" -A 2 -B 2 $(fd "Case.*dto" --type f)

Length of output: 60409

apps/judicial-system/backend/src/app/modules/case/dto/updateCase.dto.ts (1)

73-73: LGTM! Validation fix correctly implemented.

The change to add { each: true } to @MaxLength properly fixes the validation by applying the 255-character limit to each police case number individually, rather than to the entire array.

Let's verify similar validation patterns in related DTOs:

Consider adding test cases to verify:

  1. An array with multiple strings each under 255 chars passes validation
  2. An array with a string exceeding 255 chars fails validation
  3. Edge cases like empty strings and strings exactly 255 chars long

Would you like me to help create these test cases?

✅ Verification successful

Let me check the content of these DTOs to see if they have similar array validation patterns that might need the same fix.


Let me check the createCase.dto.ts file to verify if the same validation pattern is used consistently.


Validation pattern is correctly implemented and consistent

The validation for policeCaseNumbers is correctly implemented in both createCase.dto.ts and updateCase.dto.ts with the same pattern:

@IsArray()
@ArrayMinSize(1)
@MaxLength(255, { each: true })
@IsString({ each: true })

This ensures each police case number in the array:

  • Is a string
  • Has a maximum length of 255 characters
  • The array has at least one element
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find similar validation patterns in DTOs that might need the same fix
# Look for MaxLength decorators on array properties that might be missing the each:true option

# Search for array properties with MaxLength decorator
rg -U "@IsArray\(\)[^}]*@MaxLength\([^{]*\)" --type ts

# Search for potential DTO files that might need similar validation
fd -e ts -E "*.spec.ts" -E "*.test.ts" --full-path "dto" | xargs rg "@MaxLength" -l

Length of output: 1160


Script:

#!/bin/bash
# Check for @IsArray followed by @MaxLength without {each: true} in DTO files
for file in $(fd -e ts -E "*.spec.ts" -E "*.test.ts" --full-path "dto"); do
  echo "=== $file ==="
  rg -U "@IsArray\(\)[^}]*@MaxLength\([^{]" "$file" -A 1
done

Length of output: 316920


Script:

#!/bin/bash
# Check the validation pattern in createCase.dto.ts
cat apps/judicial-system/backend/src/app/modules/case/dto/createCase.dto.ts

Length of output: 1970


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.

@oddsson oddsson removed request for a team and RunarVestmann November 18, 2024 21:38
Copy link

codecov bot commented Nov 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 36.46%. Comparing base (a2b25ec) to head (2f78c67).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #16930   +/-   ##
=======================================
  Coverage   36.46%   36.46%           
=======================================
  Files        6903     6903           
  Lines      144583   144583           
  Branches    41283    41283           
=======================================
  Hits        52718    52718           
  Misses      91865    91865           
Flag Coverage Δ
judicial-system-backend 55.53% <ø> (ø)

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

Files with missing lines Coverage Δ
...backend/src/app/modules/case/dto/createCase.dto.ts 100.00% <ø> (ø)
...backend/src/app/modules/case/dto/updateCase.dto.ts 86.72% <ø> (ø)

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 a2b25ec...2f78c67. Read the comment docs.

---- 🚨 Try these New Features:

@datadog-island-is
Copy link

Datadog Report

Branch report: j-s/fix-e2e
Commit report: 8d36761
Test service: judicial-system-backend

✅ 0 Failed, 21314 Passed, 0 Skipped, 21m 4.52s Total Time
➡️ Test Sessions change in coverage: 1 no change

@unakb unakb added the automerge Merge this PR as soon as all checks pass label Nov 19, 2024
@kodiakhq kodiakhq bot merged commit b3e05fd into main Nov 19, 2024
33 checks passed
@kodiakhq kodiakhq bot deleted the j-s/fix-e2e branch November 19, 2024 09:04
jonnigs pushed a commit that referenced this pull request Nov 26, 2024
* Checkpoint

* Refactor AlertMessage

* Format date

* Cleanup

* Cleanup

* Merge

* Add key

* Refactor

* Remove console.log

* Merge

* Merge

* Fix e2e

* Resolve merge conflict

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Co-authored-by: Guðjón Guðjónsson <[email protected]>
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.

3 participants