Skip to content

Add pattern alias for format validation and string regex support#5

Merged
bluzky merged 1 commit intomainfrom
feature/minor-alias-format
Sep 26, 2025
Merged

Add pattern alias for format validation and string regex support#5
bluzky merged 1 commit intomainfrom
feature/minor-alias-format

Conversation

@bluzky
Copy link
Copy Markdown
Owner

@bluzky bluzky commented Sep 26, 2025

  • Add 'pattern' as alias for 'format' validator
  • Support string regex patterns in validate_format function
  • Add comprehensive tests for pattern alias and string regex
  • Update documentation to reflect new functionality
  • Add CLAUDE.md for future development guidance

🤖 Generated with Claude Code

Summary by Sourcery

Introduce a pattern alias for the existing format validator, add support for string-based regex patterns in format validation, and update tests and documentation to cover the new functionality.

New Features:

  • Add pattern option as an alias for the format validator
  • Support passing regex patterns as strings to validate_format/2

Documentation:

  • Update inline documentation to describe the pattern option and string regex support
  • Add CLAUDE.md to provide development and contribution guidance

Tests:

  • Add tests for the pattern alias and for string-based regex pattern handling, including success, mismatch, and invalid pattern cases

- Add 'pattern' as alias for 'format' validator
- Support string regex patterns in validate_format function
- Add comprehensive tests for pattern alias and string regex
- Update documentation to reflect new functionality
- Add CLAUDE.md for future development guidance

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Sep 26, 2025

Reviewer's Guide

This PR enhances the existing format validator by introducing a new pattern alias mapped to the same implementation, extends validate_format/2 to accept and compile string-based regex patterns with proper error handling, updates documentation to reflect these changes, adds end-to-end tests covering both regex and string patterns (matching, non-matching, and invalid cases), and includes a new CLAUDE.md file for development and architecture guidance.

Flow diagram for validate_format/2 with string regex support

flowchart TD
    A["validate_format(value, check)"] --> B{Is check a string?}
    B -- Yes --> C["Regex.compile(check)"]
    C -- Success --> D["validate_format(value, regex)"]
    C -- Error --> E["{:error, 'invalid regex pattern'}"]
    B -- No --> F["Regex.match?(check, value)"]
    F -- Match --> G[":ok"]
    F -- No Match --> H["{:error, 'does not match format'}"]
Loading

File-Level Changes

Change Details Files
Introduce pattern alias for format validation and update documentation
  • Added :pattern to supported validators list
  • Mapped :pattern to validate_format/2 in get_validator/1
  • Updated module docs to list format
pattern together
Support string regex patterns in validate_format/2
  • Added function clause to compile string patterns via Regex.compile/1
  • Handle invalid regex compilation with a descriptive error
lib/valdi.ex
Add tests for new pattern alias and string regex support
  • Added tests for matching and non-matching cases using both regex and string patterns
  • Added tests for invalid pattern compilation and type error scenarios
test/valdi_test.exs
Add CLAUDE.md for future development guidance
  • Created guidance file with setup, testing, formatting, and architectural notes
CLAUDE.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bluzky bluzky merged commit 771a48f into main Sep 26, 2025
0 of 2 checks passed
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • The error message for mismatches still says "does not match format" even when using the pattern alias—consider updating it (or making it context‐aware) to reference "pattern" or a more generic phrase to avoid confusion.
  • Right now string patterns are compiled on every call—if you expect high‐volume usage you might consider caching or precompiling regexes to avoid repeated compile overhead.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The error message for mismatches still says "does not match format" even when using the `pattern` alias—consider updating it (or making it context‐aware) to reference "pattern" or a more generic phrase to avoid confusion.
- Right now string patterns are compiled on every call—if you expect high‐volume usage you might consider caching or precompiling regexes to avoid repeated compile overhead.

## Individual Comments

### Comment 1
<location> `lib/valdi.ex:458-461` </location>
<code_context>
-  @spec validate_format(String.t(), Regex.t()) ::
+  @spec validate_format(String.t(), Regex.t() | String.t()) ::
           :ok | error
+  def validate_format(value, check) when is_binary(value) and is_binary(check) do
+    case Regex.compile(check) do
+      {:ok, regex} -> validate_format(value, regex)
+      {:error, _} -> {:error, "invalid regex pattern"}
+    end
+  end
</code_context>

<issue_to_address>
**suggestion:** Consider handling Regex.compile errors with more detail.

Including the specific error from Regex.compile in the response would provide users with clearer feedback on why their pattern is invalid.

```suggestion
    case Regex.compile(check) do
      {:ok, regex} -> validate_format(value, regex)
      {:error, reason} -> {:error, "invalid regex pattern: #{inspect(reason)}"}
    end
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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