Add pattern alias for format validation and string regex support#5
Merged
Add pattern alias for format validation and string regex support#5
Conversation
- 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>
Reviewer's GuideThis PR enhances the existing format validator by introducing a new Flow diagram for validate_format/2 with string regex supportflowchart 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'}"]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
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
patternalias—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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 Generated with Claude Code
Summary by Sourcery
Introduce a
patternalias for the existingformatvalidator, add support for string-based regex patterns in format validation, and update tests and documentation to cover the new functionality.New Features:
patternoption as an alias for theformatvalidatorvalidate_format/2Documentation:
patternoption and string regex supportCLAUDE.mdto provide development and contribution guidanceTests:
patternalias and for string-based regex pattern handling, including success, mismatch, and invalid pattern cases