New package: mdg:validated-method - Meteor methods with better scoping, argument checking, and good defaults

To be clear, are you saying that from hence-forth I must use simple-schema to build Meteor apps? Or are you saying this is just considered best-practices by MDG?

Thoughtful, and I like the spirit of it generally! Still, Promises are part of core now, and theyā€™re not used, thatā€™s my only critique. Can you omit the callback parameter and get a Promise ? If not, is there any guarantee of whether the callback is invoked sync or async, or multiple times, for the simulation, and the eventual real ?

2 Likes

Ah I was hoping you would comment! What do you think is the best way to integrate promise support here? I feel like itā€™s not yet a good time to switch to always returning a promise, and doing it only sometimes seems weird. Some options:

  1. Add support for mixins, have a promise mixin that wraps call
  2. Make a subclass class PromiseMethod extends Method that wraps call
  3. Add a new method callAsync or callPromise

As for the callback, it has the same semantics as Meteor.call - is there some case where that invokes the callback twice, or synchronously? This wrapper could be a good place to fix that, if so.

You donā€™t need to use simple-schema, but that will be the path we recommend. The idea is that the core Meteor framework can remain flexible (and actually become more flexible) and the guide project can be completely opinionated.

2 Likes

Iā€™m not sure if you saw this https://github.com/meteor/meteor/pull/5005 but my impression was that making methods return promises is a non-trivial exercise for some complicated reasonsā€¦

Youā€™re making a good point by mentioning Astronomy here. I think that Meteor is definitely lacking a robust and standardized way to set up model classes and hook them to the underlying collections (including validation) in a DRY way. Something that BaseModel and Astronomy are trying to achieve.

Iā€™m using neither of them at the moment (because I did not have enough time to rework my code), so I know the pain that arises from not having such a model wrapper. If I change a field in one of my model classes, I also have to update my SimpleSchema for the collection (which I am not forced to use, but helped me a lot to detect issues), and this of course is double work.

So, instead of just hooking SS into this new way of method invocation, wouldnā€™t it be better to introduce a more object-oriented modeling paradigm to Meteor, by which the method invocation itself could then decide which validation to use, based on the types of the input parameters? I am speaking of a way of introducing stronger typing to Meteor.

To keep things simple for beginners, this strong typing mechanism could be made opt-in (just like autopublish), but for a maintainable app it would be a must.

1 Like

We did actually spend some time looking around and calling for suggestions: https://github.com/meteor/guide/issues/534

Looking around (with only one more person other than you and aldeed) is one thing, bringing this up with the community, like you guys did on the Blaze topic is another thing. Besides, it was a ā€œguideā€ issue, not a ā€œcoreā€ issue that revolved around redefining how methods work.

JSON Schema seems to be the closest thing to a ā€œstandardā€ right now, but I think itā€™s a long way away from being that

So yeah, instead of embracing and contributing to the standards, letā€™s do something else (Blaze?) and then when the broader JS community finally comes up with a standard, letā€™s ditch all the hard work and the effort?

f weā€™d been quicker to embrace SS, then I would guess Astronomy ā€¦ would probably have used SS for its schemas, and weā€™d be in a better place right now

Well, itā€™s not what happened and you really canā€™t say that on behalf of Astronomyā€™s author. In fact aldeed himself said the exact opposite, that he would have not bothered creating SS had astronomy existed back then. But thatā€™s besides the point. Iā€™m not advocating astronomy here.

I donā€™t think we are ā€œshutting out alternativesā€, except if you mean choosing something to recommend in the guide and elsewhere

Again, are we talking about the guide or a package thatā€™s very likely to become core? For the latter, taking in SS is not recommending anything, it is a bold statement for SS and against anything else. Subclassing etc are then just technical politics and far cries of the remaining alternatives.

I think the consensus is that itā€™s a very good idea for us to do this.

Again, whose consensus? From the github issue youā€™ve linked, it looks like two or three people.

So my overall point is, I think this decision of yours is a statement of how MDG operates and how Meteor and the community is governed. And it just does not help heal the many similar catastrophies weā€™ve had until now.

Iā€™d say every time since its important to know how it works and what problems it solves. It can be like a pre-requisite. Maybe the package could be offered in the end as a shortcut ā€œnow you know, letā€™s save some timeā€ - but Iā€™m not comfortable with this being a core package.

1 Like

This is a big improvement of methods, and I would happily switch over to use the package. I understand @serkandurusoy though. Playing with the package is one thing, but using it in business apps is another with real money at stake.
It would help to know if it will become part of core or being maintained in the long run, so @sashko or @tmeasday maybe can tell something on their plans or long term commitment?

@aldeed after all this blaze drama, news of simpleschema v2 node package is a surprising ray of sunshine!!!

2 Likes

Yeah, now you will have to rewrite your schemas too! :smile:

3 Likes

So if check goes by the wayside in favor of Simple Schema, should be then possibly develop a pattern for using it in our publications as well?

2 Likes

An interim would be to use check(input, schema) since SS already supports use as a match pattern.

1 Like

Right, Iā€™m just wondering what happens when check is no longer maintained.

1 Like

Check is a fairly straightforward package which has not seen too many major updates since its inception. I guess one could continue using it with some piece of mind. That being said, Iā€™ll no longer be surprized if/when MDG decides pulling things from under me.

1 Like

Ah, thanks! I was confusing the platform with the Guide :slight_smile: Thanks again for pushing out a best practices document for us all!

Really like your point here of Meteor being flexible but the Guide being opinionated. As noob to development, I always feel like Iā€™m doing something wrong and missing out on important features. It can be discouraging sometimes, so thatā€™s where I think the Guide comes to the rescue. My humble suggestion for the Guide (and Meteor docs perhaps):

Iā€™m a huge fan o the way Stripe structured their documentation: https://stripe.com/docs/api#intro , with the 3 panels split screen showing the menu | explanations | code syntax + examples. Itā€™s really easy to navigate and grasp whatā€™s going on. The examples are few but encompassing, giving you full acknowledgment of the features available. The overall formatting is outstanding.

To start out, I want to say that if building/releasing this kind of package is not the best way forward, then we wonā€™t do it. This is definitely a request for feedback rather than a set in stone thing.

So hereā€™s how this package came about:

  1. We needed to write an article for the guide about forms and methods.
  2. We started thinking about the best ways to call methods, in line with the patterns @tmeasday had been using at Percolate.
  3. In the initial exploration of the article, the pattern for defining and calling methods became quite verbose - that is from an earlier exploration of best practices for methods.
  4. Even that verbose pattern didnā€™t handle returnStubValue, and didnā€™t let you actually display helpful errors on the client when a method invocation failed to validate.

We figured that recommending 15+ lines of boilerplate for every single method call was a bit much, but anything less was not using the Method system to its full potential. Our analysis was that any serious project would need to build its own wrapper class for methods.

Analysis of this thread

Please let me know if Iā€™ve mis-represented your position.

For:

  1. @efrancis says ā€œitā€™s really greatā€
  2. @avital says ā€œI love the idea of eliminating magic strings, but I donā€™t want to be forced to use simple-schemaā€
  3. @waldgeist says ā€œGood idea to make method invocation easier, this has some built in features that are very hard to find otherwise, but we shouldnā€™t tie it to simple-schemaā€
  4. @Steve says ā€œmeteor-call-sync has been doing this for a while, having returnStubValue and throwStubExceptions on by default is a good idea.ā€
  5. @jhartma says ā€œThis is a big improvement of methods, and I would happily switch over to use the package. However, I wouldnā€™t want to use it in a real business app without a guarantee of support.ā€

Against:

  1. @msavin says ā€œThe guide should explain the underlying issues and teach people how to make a method wrapper for themselves. We should make sure this package is not presented as a replacement for methods.ā€
  2. @serkandurusoy says "I really donā€™t want MDG to start building things around simple-schema, since itā€™s not a standard and there are some alternatives that might be better. The proposed API seems much better than the previous method API, but Iā€™m uncomfortable for new packages to be introduced in this way.

Neither:

  1. @SkinnyGeek1010 says ā€œI also wrote a wrapper for methods, which looks different and enforces some modularity in a different way. I think the most important part is understanding what methods exist, and making them easily testable.ā€
  2. @deanius says ā€œA good method API would include support for promises.ā€

Conclusions

Itā€™s clear from the conversation that the current method API isnā€™t ideal, and there is some boilerplate involved to write good, maintainable methods. A wrapper package isnā€™t necessarily the best way to deal with the situation, but if we had to reproduce the boilerplate in every place in the guide where methods are involved, it would be very difficult to read.

Decouple simple-schema: The built-in simple-schema integration set off some alarms for people. aldeed proposed a simpler API that allows any type of schema to be used, as long as you can write a function that throws validation errors. I think going with that is a good idea. While we are definitely going to be writing code samples for the Meteor guide using simple-schema for a lot of stuff, thereā€™s no reason for it to be hardcoded into anything if it doesnā€™t have to be.

Guarantee of support: If we release this as a package that we intend people to use, it needs to be maintained. Methods are a crucial part of the Meteor app writing experience, and you wouldnā€™t want to have bugs in that system introduced by some wrapper code.

Another option - DIY method wrappers?

Thereā€™s one other option other than introducing a package - teach people to write their own method wrapper that encodes some of the same practices. The main downside here is that encouraging people to write custom wrappers for methods will significantly cut down how easy it is for someone to understand a Meteor app they havenā€™t worked with before. First youā€™ll have to read and understand that particular projectā€™s method wrapper code. So there are benefits to a standardized wrapper package.

15 Likes

Wow, I like this analysis. Would love to see such things in another topics.

Great job, @sashko!

3 Likes

@sashko although the for/against/neither breakdown is somewhat convoluted in its own regard, I think this is a great breakdown and it fills me with joy to see how well you/mdg can assess a given situtation with all of its complexity. Kudos.

I sincerely believe that all arguments pro/con given for the blaze announcement applies here as well, in fact to everything Meteor and I guess the overall concerns are about the API stability and long term support. For this thread specifically, I wish the proposed method api did take cue from the openness of the view layer such that things like schemas/models could be planned out to be more interchangeable.

Apart from that, I find the direction quite pleasing and in line with the overall feel of Meteor.

1 Like