Skip to content

WIP - make simple-schema fully Async#746

Merged
jankapunkt merged 6 commits intoMeteor-Community-Packages:feature/asyncfrom
vparpoil:feature/fullAsync
Aug 20, 2025
Merged

WIP - make simple-schema fully Async#746
jankapunkt merged 6 commits intoMeteor-Community-Packages:feature/asyncfrom
vparpoil:feature/fullAsync

Conversation

@vparpoil
Copy link
Copy Markdown

@vparpoil vparpoil commented Jan 16, 2025

Motivations

Following Meteor 3 compatibility PR, a vote has been casted and the community voted for a full async compatibility

From a quick investigation, here are the functions that could benefit of being async :

  • Functions that would enable denormalization and will make .clean() async
    • autoValue
    • label - my feeling is that using async function here is redundant with autoValue
  • Functions that would enable custom validation and allow enhancing data integrity. Thet will much likely make .validate() async
    • custom
    • required and optional
    • min, max, minCount, maxCount, exclusiveMin, exclusiveMax
    • allowedValues
    • regEx
    • skipRegExCheckForEmptyStrings

What's included

This PR introduces async support for autoValue. Here's an example use case:

authorId: {
   type: String,
    regEx: SimpleSchema.RegEx.Id
},
authorName: {
    type: String, 
    autoValue: async function(){
         const author = await Author.findOneAsync(this.field("authorId").value);
         return author.name;
    }
}

This feature is particularly useful for denormalization scenarios, such as adding fields like createdByName to your data schema.

Changes

  • Updated tests to include async autoValue. All existing tests continue to pass.
  • Full linting of the package using meteor npm run lint:fix (as outlined in CONTRIBUTING.md).
    • Note: One remaining issue with linting static properties and class fields in SimpleSchema.js still needs resolution.

⚠️ Breaking Changes

  • .clean() is now Async and must be awaited
    • this change will require an update of dependent packages, such as collection2 to add await on clean() calls and benefit of async autoValue
  • validator() is now Async and must be awaited
    • this change is breaking ValidatedMethods declarations

What's next

  • Please share your thoughts on this PR. Are you in favor of moving forward?
  • While making all those functions async could improve validation capabilities and enhance DX, my personal opinion is that making validate() async is too much of a breaking change for now. I suggest focusing on autoValue and maybe label

Thank you for you input!

@vparpoil vparpoil marked this pull request as draft January 16, 2025 14:46
@jankapunkt
Copy link
Copy Markdown
Member

This is great 👍 I think this is great step forward. Let me check the next Days how it will behave with existing apps.

vparpoil added a commit to vparpoil/validated-method that referenced this pull request Jan 16, 2025
@vparpoil
Copy link
Copy Markdown
Author

Hi @jankapunkt ,
I have tested inside a project and it seems to be working just fine!
For your tests, you will want to use the forked and updated versions of collection2 and mdg validated-methods :

vparpoil added a commit to vparpoil/meteor-collection2 that referenced this pull request Jan 16, 2025
@aashutoshshr
Copy link
Copy Markdown

Hi @jankapunkt @vparpoil ,

Just wanted to check in on the status of this PR. We are looking to upgrade our app to Meteor version 3 and this change is crucial for us. Is there any chance the PR could be reviewed and merged soon?

@jankapunkt jankapunkt changed the base branch from master to feature/async July 21, 2025 06:10
@jankapunkt jankapunkt marked this pull request as ready for review July 21, 2025 06:10
@jankapunkt jankapunkt requested a review from Copilot July 21, 2025 06:11
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces async support for autoValue functions in SimpleSchema, making the clean() method async. The changes include full linting with standard conventions (semicolon removal), and adds test globals to package.json.

  • Async support for autoValue functions enabling denormalization scenarios
  • Full linting of the codebase with standardized formatting
  • Updates to test configuration for proper linting coverage

Reviewed Changes

Copilot reviewed 76 out of 79 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
lib/clean/setAutoValues.js Converts setAutoValues function to async and updates loop structure for async autoValue execution
lib/clean/AutoValueRunner.js Makes runForPosition method async to handle async autoValue functions
lib/clean/autoValue.tests.js Updates test cases to use async/await pattern for clean() calls
lib/clean/defaultValue.tests.js Updates test cases to use async/await pattern for clean() calls
tests/package.json Adds describe and it to eslint globals
package.js Removes semicolons following linting standards
Multiple utility files Removes semicolons and adjusts formatting following linting standards
Files not reviewed (1)
  • tests/package-lock.json: Language not supported
Comments suppressed due to low confidence (1)

lib/clean/setAutoValues.js:42

  • [nitpick] The variable name 'sortedItem' is not descriptive. Consider renaming it to 'autoValueFunction' or 'avFunction' to better indicate what it represents.
  for (const sortedItem of sortedAutoValueFunctions) {

Comment on lines +42 to +43
for (const sortedItem of sortedAutoValueFunctions) {
const { func, fieldName, closestSubschemaFieldName } = sortedItem
Copy link

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

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

[nitpick] This destructuring assignment would be clearer if the variable name in the previous line was more descriptive, as it would make this line more self-documenting.

Suggested change
for (const sortedItem of sortedAutoValueFunctions) {
const { func, fieldName, closestSubschemaFieldName } = sortedItem
for (const autoValueFunction of sortedAutoValueFunctions) {
const { func, fieldName, closestSubschemaFieldName } = autoValueFunction

Copilot uses AI. Check for mistakes.
Comment on lines +49 to 71
const autoValue = await func.call({
closestSubschemaFieldName: closestSubschemaFieldName.length ? closestSubschemaFieldName : null,
field(fName) {
return getFieldInfo(mongoObject, closestSubschemaFieldName + fName);
field (fName) {
return getFieldInfo(mongoObject, closestSubschemaFieldName + fName)
},
isModifier,
isUpsert,
isSet: (value !== undefined),
key: affectedKey,
operator,
parentField() {
return parentFieldInfo;
parentField () {
return parentFieldInfo
},
siblingField(fName) {
return getFieldInfo(mongoObject, fieldParentName + fName);
siblingField (fName) {
return getFieldInfo(mongoObject, fieldParentName + fName)
},
unset() {
doUnset = true;
unset () {
doUnset = true
},
value,
...(extendedAutoValueContext || {}),
}, mongoObject.getObject());
...(extendedAutoValueContext || {})
}, mongoObject.getObject())

Copy link

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

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

The autoValue function is now awaited, but there's no error handling for async functions that might throw. Consider adding try-catch to handle potential async errors gracefully.

Copilot uses AI. Check for mistakes.
@jankapunkt jankapunkt merged commit eff523e into Meteor-Community-Packages:feature/async Aug 20, 2025
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.

4 participants