Meteor-Community-Packages / Meteor-Community-Packages/meteor-simple-schema
Using Collection Hook's direct version breaks defaultValue and autoValue on an insert
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 914
- Forks
- 162
- PR merge metrics
- No merged PRs in 30d
Description
**NOTE** - Also posted this bug [here](https://github.com/Meteor-Community-Packages/meteor-collection2/issues/452) in `aldeed:collection2`. Wasn't sure which package it's rooted in. Will close in both once it's resolved.
**Describe the bug**
Ran into an issue upgrading my project to Meteor 2.16 from 2.7.2 (on my way up to 3.0).
When using `aldeed:collection2@4.0.2` with the included `aldeed:simple-schema@1.13.1` on a Meteor 2.16 project along with `matb33:collection-hooks@1.3.1` (latest version), using the collection hook's `direct` versions that bypass a collection's hook will cause the `defaultValue` and `autoValue` set in the schema to also not run. The added logic in the defined simple schema gets bypassed and the document gets inserted/updated as if simple schema doesn't exist. I have tested `matb33:collection-hooks` and it seems the problem is rooted in `aldeed:collection2` or `aldeed:simple-schema`. If you drop back down to `aldeed:collection2@3.5.0` and use `meteor npm install simpl-schema@1.13.1` with any version of `matb33:collection-hooks` all works as expected.
**To Reproduce**
I've hotrodded the React Simple Todos demo to display the error [in a repo here](https://github.com/evolross/React-Simple-Todos-Simple-Schema-Bug/tree/main).
The repo has the following currently added:
```
aldeed:collection2@4.0.2
aldeed:simple-schema@1.13.1
matb33:collection-hooks@1.3.1
```
1. Click on the **Add New Link** button.
A link will be inserted in the list _without_ using the `direct` version in the `insert`. The `insert` is performed and the link's `description` and `createdAt` are all successfully set using either Simple Schema's `defaultValue` or `autoValue`. You'll also notice the link's `linkType` is set to "video" by the defined and working `LinksCollection.before.insert` hook.
2. Click on the **Add New Link (Direct)** button.
A link will be inserted in the list _using_ the `direct` version in the `insert`. The `insert` is performed but the link will not get any of the Simple Schema logic (e.g. `defaultValue` or `autoValue`) however it does get inserted. And the hook is not performed so its `linkType` is still "website".
**Expected behavior**
Making use of a `matb33:collection-hook` `direct` call should _still_ run the Simple Schema logic (e.g. `defaultValue` or `autoValue`) and validation. Using `aldeed:collection2@3.5.0` and `meteor npm install simpl-schema@1.12.3` all works as expected. Feel free to comment out `aldeed:collection2` and uncomment `aldeed:collection2@3.5.0` in the `packages.js` file and then do a `meteor npm install simpl-schema@1.13.1` and then update the `import` in `links.js` to see it working correctly on the prior versions.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked reproduction repository, inspect packages.js for the listed package versions, and compare the direct and non-direct inserts in links.js. Trace how direct inserts pass through collection2 and simple-schema, comparing with collection2@3.5.0; done means defaultValue, autoValue, and validation still run for direct inserts.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100