Repository navigation
fix(collections): require a rule before a smart collection previews or saves - #3710
Conversation
…r saves A smart collection with no complete rule matched every title: the editor previewed the whole catalog and let it be created or saved, and the /api/v2 create and update operations accepted it. The editor now sends no preview, asks for a rule, and keeps Create and Save off until one rule has a field, an operator and a value. The v2 admin and personal create and update operations answer 422 at body.query_definition.groups for a smart collection left without one. Updates that leave the rules alone, /api/v1 and backup restore are unchanged, so collections saved before the check keep working. Closes Silo-Server#3695
Add web cases for hasCompleteRule matching the server's, check that the editor previews only the complete rule, and cover a list toggle on a smart collection saved with no rules. Say in the API doc that only the admin update can switch a collection to smart.
Code Review Could Not Complete
|
| Options | Enabled |
|---|---|
| Bug | ✅ |
| Performance | ✅ |
| Security | ✅ |
| Business Logic | ❌ |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Review of 3a73765I found no defects in this PR, so I didn't post a line-by-line review or push any commits. One product question is left for maintainers, below. What I checked
Open for maintainers
Validation I corrected the User manual, clients, jellycompat
Overlap: #3711 changes the Checks: CI is green. The Kody check failed because Kody couldn't reach its model provider. It didn't review the code, and the failure isn't caused by this branch. Fit: 8/10 · Readiness: 9/10, conditional
🤖 Claude Code (claude-opus-5-5) |
Resolve the conflicts with main's changes since d941225: - Query normalization moved to catalog.NormalizeQueryDefinitionJSON; structural admission moves with it, so backup restores also pass it. - /api/v1 is retired (#3200): drop the v1-only item loaders and doc text and the V1Rules test field. - The web collections pages are translated (#3902): "Count unavailable" and the manual editor's add-blocked messages become catalog keys (synced locally until the silo-translations change merges). - Server smart rules name their own libraries (#3711): a server save sends query_definition only when the rules change. - Save returns to the list (#3709) and smart collections need a rule (#3710): adjust the PR's tests to observe what they still can. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Problem
Closes #3695
Related issue: #192
Validation tasks: unblocks #194 A2 (alongside #3694 and #3697). The passed #195 cases (W1, W2, W3, W6) are unaffected, and so is #195 W4: a rule set that matches nothing still saves.
A smart collection could be previewed, created and saved with no rules. Empty rule groups match every title, so the live preview filled with the whole catalog and Create collection went through, leaving a "smart" collection that holds everything. Removing the last rule from a saved smart collection could be saved too, because the save bar's
saveBlockedReasonnever looked at the rules. The API accepted the same thing from any client. This affects server smart collections (Admin > Libraries > Collections) and personal ones (Collections), which share the editor.With this change a smart collection needs at least one complete rule: a field, an operator and a value. The editor won't preview or save without one, and
/api/v2refuses it.Approach
Web editor.
hasCompleteRuleinweb/src/lib/collections/draft.tsdecides whether a rule can filter. A blank value, a missing value or a range with an empty end doesn't count, so a fresh "Genre is …" row doesn't satisfy it. Numbers (including 0) and booleans (including false) do. While no rule is complete:The rule builder's empty-state line for collections no longer says the collection holds every title.
API.
QueryDefinition.HasCompleteRuleininternal/catalogmirrors the web check.createAdminCollection,updateAdminCollection,createCollectionandupdateCollectionrefuse a smart collection without a complete rule with422 validation_failedatbody.query_definition.groups. An update is checked when it sends a smart collection'squery_definition, or when it switches a collection to smart (the stored rules become live).What I deliberately left alone:
/api/v1is frozen, so its create and update routes keep accepting empty rules. They share the service code, and the check is skipped whenV1Rulesis set.The user manual on siloserver.org already tells people to add rules and never says an empty rule set is allowed, so I haven't opened a manual issue. The API and editor docs are updated in
docs/catalog-api.md(rule section),docs/collections-api.mdanddocs/architecture/collection-editor.md.Overlap: #3694 (creating a server smart collection without picking libraries) and #3696 (where the editor goes after Save/Create) are being fixed in parallel PRs and touch the same lines in
CollectionEditor.tsx(canCreate,createHintandsaveBlockedReason). Whichever lands second will need a small merge there.Client impact:
silo-appleonly creates manual collections.silo-androidstill has aCreateCollectionSheetwith a Smart option, and its sharedCollectionRepository.createCollectionwould sendcollection_type: "smart"with noquery_definition, which now gets the 422. Nothing in the app calls either of them today, so nothing breaks for users. If that sheet comes back, it has to send a rule and show thequery_definition.groupserror against the rules. jellycompat only reads collections, so it needs no change.Validation
go test ./internal/catalog/ -run 'TestQueryDefinitionHasCompleteRule|TestQueryDefinitionValidation'passed.go test ./internal/api/handlers/(whole package) passed.go test ./internal/apiv2/ -run Collectionpassed. None of the new tests need Postgres.vitest runover the collections pages,src/lib/collections,src/components/collections,src/components/homeRowsandAddToCollectionDialog: 56 files, 534 tests passed.eslinton the changed files andtsc --noEmitwere clean.adminSmartCreate,personalSmartCreate) now carry a Genre rule, since a create with no rules is no longer possible from the editor. The editor tests add that rule through the UI.Evidence
Evidence: https://evidence.siloserver.org/r/silo-server/pr-3710/
Surface and build: web, personal smart collection editor (Collections > New collection > Smart), desktop (1440 px) and phone (390 px) width. Before is upstream
mainat4018816da, after is this branch. Both ran against a mocked/api/v2with made-up titles. Server smart collections use the same editor and gating.Risks
/api/v2now get a 422. That is the point of the change. The web editor no longer sends such a request.Checklist
AI Disclosure
hasCompleteRulecases, a stricter preview assertion, a test row for the list toggle on an old empty collection, and a doc fix. The editor behaviour for old empty collections and the rule-default limitation are described above.Note
Require a complete rule before Smart collection preview or save
QueryDefinition.HasCompleteRulecheck in query_definition.go and an equivalenthasCompleteRuledraft helper in draft.ts. A rule is complete when it has a nonblank field, operator, and filled value (zero, false, and numbers count as filled).requireSmartCollectionRuleandadminSmartRuleCheckin collections.go./api/v1requests, updates that leave existing rules unchanged, and backup restore bypass the new validation. Clients that previously saved Smart collections without rules now get a validation error atquery_definition.groups.Macroscope summarized 3a73765.