question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make SimpleSchema custom() function reactive

See original GitHub issue

Hi aldeed.

I am currently using the custom() function of SimpleSchema to add a custom validation for many fields of my forms. Basically, i want to add a custom verification to check if a username is available :

    username: {
        type: String,
        regEx: /^[a-z0-9A-Z_]{3,15}$/,
        label: "Username",

        // custom validation for username availability

        custom: function () {
            Meteor.call('accountsIsUsernameAvailable', this.value, function (error, result) {
                if (!result)
                    Session.set("usernameAvailability", "usernameNotAvailable");
                else
                    Session.remove("usernameAvailability");
            });
            return (Session.get("usernameAvailability"));
        }
    }

I am using a method of course because i obviously dont want to publish all the usernames of my database to the client. Because i want a result from these method, i set a callback and the method run asynchronously, and this is where is my issue.

To be sure that i will actually return a result from custom(), u can see in the code example above that i use Session as a reactive data source to make sure the custom function will return the result a soon as it arrive. In fact, there is two return for each excecution of custom(), but i don’t really care.

What i would like to propose, is a way to avoid the use of Session. I prefer in every case to limit the use of Session to do not polluate the namespace. I just wrote my code with Session to test the method inside custom(), but now i am gonna to write my own reactive data source for this purpose, according this tutorial : https://www.eventedmind.com/feed/meteor-build-a-reactive-data-source.

I am wondering if it would be possible to implement such a thing in SimpleSchema custom() function ? I don’t know how it work internally in SimpleSchema, but i guess you are invalidating a field based on the return of custom(). Would it be possible to offer a property this.result inside custom(), let the user fill it with the same way (non-string for success – string for error) and track this with a Deps.Dependency. Then on the change of this property, do the invalidation methodology inside SimpleSchema.

I am maybe wrong about this, if so, forgive me, i’m just sharing my thought because i was thinking of that when i was writting my code.

Regards,

William


<bold>EDIT:</bold> I don’t think that the context of custom() would be available in the Meteor.Method that have to set the this.result property, but maybe using _.bind would be helpfull in this case, and solve the problem of accessibility.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
aldeedcommented, Apr 28, 2014

In that case, just put my approach into your custom function:

custom: function () {
  if (this.isSet) {
    Meteor.call('accountsIsUsernameAvailable', this.value, function (error, result) {
        if (!result) {
         UserSchema.namedContext('createUserForm').addInvalidKeys([{name: 'username', type: 'notUnique'}]);
        }
    });
  }
}

The custom function doesn’t have to return anything, and when you do return a string, it is essentially identical to calling addInvalidKeys, so this approach is equivalent to returning a custom error string after a short delay.

0reactions
thearabbitcommented, Jan 24, 2017

I have problem with onSuccess Hook, it run before custom validate

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make SimpleSchema custom() function reactive #86 - GitHub
Hi aldeed. I am currently using the custom() function of SimpleSchema to add a custom validation for many fields of my forms.
Read more >
How to use node-simple-schema reactively? - Stack Overflow
So, whenever I try to validate foo , the asynchronous' custom function is called, and the proper addValidationErrors function is called, but the ......
Read more >
Publications and Data Loading - Meteor Guide
This is a reactive function that returns true when the publication is marked ready (either you call this.ready() explicitly, or the initial contents...
Read more >
How to handle translations in AutoForm-based forms [Meteor's ...
We want to use function labels from aldeed:simple-schema . In this example I will use tap:i18n but you can use any reactive translation...
Read more >
Meteor Database Collections - Khai's personal knowledge vault.
In the examples above, note that we called namedContext() with no arguments to access the SimpleSchema reactive validation methods.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found