Skip to content

Add default_filters option - #726

Merged
woylie merged 8 commits into
woylie:mainfrom
timothyvanderaerden:default-filter-option
Nov 26, 2026
Merged

woylie merged 8 commits into
woylie:mainfrom
timothyvanderaerden:default-filter-option

Conversation

@timothyvanderaerden

@timothyvanderaerden timothyvanderaerden commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Adds a :default_filters option that can be set on a schema or passed to a
query function. It takes a list of Flop.Filter structs that Flop.validate/2
applies when the parameters contain no filter with a value for the respective
field.

Behavior

  • Client filters take precedence per field: if the parameters contain a filter
    with a value for a field, the defaults for that field are not applied,
    regardless of the operator used by the client filter. Filters for other
    fields are combined with the defaults as usual.
  • Defaults are appended after the client filters, and only if the parameters
    are valid.
  • Filter values are cast to the field type like filter parameters, so a default
    value of "8" for an integer field results in a value of 8.
  • Schema default filters are validated and cast once when the schema is
    compiled; an invalid default raises Flop.InvalidConfigError. Defaults on
    :binary_id fields and on fields that take their type from another schema
    module are cast at runtime, since their type depends on the repo or on
    another module. Default filters passed at the call site are cast at runtime.
    Invalid defaults cast at runtime raise an ArgumentError.
  • Flop.validate/2 raises if a default filter refers to a field that is not
    filterable, e.g. because the filterable fields were narrowed with the
    :filterable option.
  • Defaults are applied after invalid client filters have been removed (with
    replace_invalid_params: true), so a removed filter does not suppress the
    default for its field.
  • Default filters do not count towards the max_filters limit. Filters that
    are identical to a default filter (e.g. resubmitted from a validated Flop
    struct) don't count either. The comparison is made per filter, so multiple
    defaults on the same field, e.g. a range, keep working when the client
    changes one bound.
  • The option can be disabled for a single query by passing
    default_filters: false at the call site.
  • Like :default_order, the option names fields, so it cannot be set in the
    application environment or on a backend module.

Flop.Filter.new/1 can be used to build the list from a map of field names to
values, applying the default :== operator:

@flop_options [
  filterable: [:name, :age, :deleted],
  sortable: [:name, :age],
  default_filters: Flop.Filter.new(%{deleted: false})
]

Notes

  • With filtering: false, filter parameters are ignored, but default filters
    are still applied, mirroring how the default order is applied with
    ordering: false.
  • Since the applied filters are part of the validated Flop struct, they also
    show up in Flop.Meta.flop.
  • To cast the schema's own fields at compile time, Flop.Schema reads the
    field types from Ecto's @ecto_fields attribute, since __schema__/2 is not
    available yet while the module is compiled.

@shunjilin shunjilin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for this PR, the implementation seems sound and I have a similar use case for setting defaults on the backend as well. 🙏

Comment thread lib/flop/validation.ex Resolved
Comment thread lib/flop.ex Resolved
Comment thread lib/flop/validation.ex Resolved
@woylie

woylie commented Oct 12, 2026

Copy link
Copy Markdown
Owner

@timothyvanderaerden Thanks for opening the PR! There are two issues:

  1. If the client submits an empty value for the filter field, the default is not applied.
  2. The default filter ends up in the Flop struct, which results in it being submitted by the client the next time. The second time it counts against max_filters, which means a first request can succeed and the second one can fail. The default filter also ends up in the URLs unless it is explicitly filtered.

All other defaults end up in the Flop struct, but here I'm not sure. I've added this to 0.30.0, will get back to it when I start with that version.

@timothyvanderaerden

Copy link
Copy Markdown
Contributor Author

@timothyvanderaerden Thanks for opening the PR! There are two issues:

1. If the client submits an empty value for the filter field, the default is not applied.

2. The default filter ends up in the Flop struct, which results in it being submitted by the client the next time. The second time it counts against `max_filters`, which means a first request can succeed and the second one can fail. The default filter also ends up in the URLs unless it is explicitly filtered.

All other defaults end up in the Flop struct, but here I'm not sure. I've added this to 0.30.0, will get back to it when I start with that version.

Both issues are addressed in e028596:

  1. Empty values: An empty filter value is cast to nil and skipped when
    the query is built anyway, so it is now treated like a missing filter and
    the default is applied. Covered for nil, "" and " ".

  2. Round-trip / max_filters: Client filters that are identical to a
    configured default filter (compared after casting, so a default value of
    "8" matches a resubmitted 8) are removed before max_filters
    validation and applied as default filters again.

Let me know if this is the outcome you had in mind, or if you'd prefer a different approach.

@woylie

woylie commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

Yes, thank you. I'll leave this PR open for a bit, I'm planning to release one patch version before the next minor.

@codecov

codecov Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.33962% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.37%. Comparing base (a90f601) to head (87ede72).
⚠️ Report is 87 commits behind head on main.

Files with missing lines Patch % Lines
lib/flop/validation.ex 94.11% 4 Missing ⚠️
lib/flop/nimble_schemas.ex 80.00% 1 Missing ⚠️
lib/flop/schema.ex 96.55% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #726      +/-   ##
==========================================
+ Coverage   89.25%   89.37%   +0.12%     
==========================================
  Files          16       16              
  Lines        1163     1459     +296     
==========================================
+ Hits         1038     1304     +266     
- Misses        125      155      +30     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@woylie woylie added this to the 0.30.0 milestone Oct 16, 2026
Comment thread lib/flop/validation.ex Resolved
Comment thread lib/flop/validation.ex Resolved
Comment thread lib/flop/nimble_schemas.ex Resolved
Comment thread lib/flop/schema.ex Resolved
Comment thread lib/flop/schema.ex Resolved
Comment thread test/base/flop_test.exs Resolved
Comment thread test/base/flop/option_levels_test.exs Resolved
Comment thread test/base/flop_test.exs Resolved
Comment thread test/base/flop_test.exs Resolved
Comment thread test/base/flop_test.exs Resolved
Comment thread lib/flop/schema.ex Resolved
Comment thread test/base/flop_test.exs Resolved
@timothyvanderaerden timothyvanderaerden changed the title Add default_filter option Add default_filters option Nov 25, 2026
@woylie
woylie merged commit ab1646f into woylie:main Nov 26, 2026
21 checks passed
@woylie

woylie commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you!

@timothyvanderaerden
timothyvanderaerden deleted the default-filter-option branch November 26, 2026 07:41
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.

3 participants