Skip to content

Flatten validation #6

Merged
bluzky merged 5 commits intomainfrom
feature/support-more-alias
Sep 26, 2025
Merged

Flatten validation #6
bluzky merged 5 commits intomainfrom
feature/support-more-alias

Conversation

@bluzky
Copy link
Copy Markdown
Owner

@bluzky bluzky commented Sep 26, 2025

Summary by Sourcery

Flatten validation syntax by introducing top-level min/max, greater_than/less_than, length and items aliases, unify number and Decimal validation under validate_number/2, add ignore_unknown option, deprecate validate_decimal/2, refactor dispatch logic, and update docs and tests.

New Features:

  • Add flattened validation options (min, max, greater_than, less_than, min_length, max_length, min_items, max_items) alongside nested syntax
  • Introduce ignore_unknown option to skip unsupported validators when validating
  • Support Decimal types in validate_number/2 and deprecate validate_decimal/2

Enhancements:

  • Refactor internal do_validate/4 to pattern-match individual validators and simplify dispatch logic
  • Remove legacy supported_validations list and consolidate validator lookup into function clauses
  • Update documentation to describe new validators and deprecation of validate_decimal/2

Tests:

  • Add tests for enum alias, flattened validators, mixed syntax, and skip-type scenarios

bluzky and others added 4 commits September 26, 2025 17:22
- Add :enum alias for :in validation
- Add flattened validators: min, max, greater_than, less_than
- Add length validators: min_length, max_length, min_items, max_items
- Add ignore_unknown option to skip unknown validators
- Refactor validation system using pattern matching instead of get_validator
- Unify number and decimal validation with backward compatibility
- Mark validate_decimal as deprecated in favor of validate_number
- Remove @supported_validations list and manual validator filtering
- Add comprehensive tests and documentation examples

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

Co-Authored-By: Claude <noreply@anthropic.com>
- Skip type validation when no type is explicitly specified
- Only validate type when type: key is provided in validators
- Improves performance for constraint-only validations
- Add tests and documentation for type-less validation behavior

Examples:
- Valdi.validate("hello", min_length: 3) # No type check, just length
- Valdi.validate(15, min: 10, max: 20)   # No type check, just bounds

🤖 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 refactors the Valdi module to support flattened validation syntax, unify number and Decimal validations, simplify dispatch logic with direct pattern matching, introduce an ignore_unknown option, and update documentation and tests accordingly.

Sequence diagram for validation with ignore_unknown option

sequenceDiagram
    participant Caller
    participant Valdi
    Caller->>Valdi: validate(value, [type: :string, unknown: :foo], ignore_unknown: true)
    Valdi->>Valdi: prepare_validator(validators)
    Valdi->>Valdi: do_validate(value, validators, :ok, opts)
    Valdi->>Valdi: do_validate(value, {:type, :string}, opts)
    Valdi->>Valdi: do_validate(value, {:unknown, :foo}, opts)
    Valdi-->>Caller: :ok
Loading

File-Level Changes

Change Details Files
Flattened validation syntax
  • Added direct support for min, max, greater_than, less_than, min_length, max_length, min_items, and max_items as top-level validators
  • Updated do_validate to pattern match on these new flattened validator keys and alias them to existing number/length checks
  • Enhanced prepare_validator to merge both nested and flattened validator options into a single pipeline
lib/valdi.ex
test/valdi_test.exs
Unified number and Decimal validations
  • Removed separate validate_decimal flow and deprecated the decimal validator in favor of validate_number
  • Extended validate_number to detect and compare Decimal.t() values alongside regular numbers
  • Redirected all decimal validation calls to the updated validate_number implementation
lib/valdi.ex
test/valdi_test.exs
Refactored validation dispatch and added ignore_unknown option
  • Removed supported_validations list and dynamic get_validator lookup
  • Replaced with explicit function clauses for each validator in do_validate
  • Introduced a catch-all clause to error or skip unknown validators based on the ignore_unknown flag
lib/valdi.ex
API signature changes to support opts
  • Extended validate, validate_list, validate_map, and validate_each_item to accept an optional opts argument
  • Propagated opts (including ignore_unknown) through do_validate to each validation clause
lib/valdi.ex
Documentation and test coverage updates
  • Updated module docs to list all new validators, deprecations, and ignore_unknown behavior with examples
  • Added tests for enum alias, flattened syntax, mixed nested/flattened options, and skip-type scenarios
lib/valdi.ex
test/valdi_test.exs

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

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 and they look great!


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.

@bluzky bluzky merged commit 51927d7 into main Sep 26, 2025
1 of 2 checks passed
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