Securing my Meteor app, I came across a strange quirk of
Match.Optional of the “check” package.
Example (in CoffeeScript)
Meteor.methods test: (x) -> check x, Match.Optional String
I expected the match to be successful when calling the method like this:
Meteor.call 'test', undefined
But instead, I got a “Match failed” exception. After debugging for a bit, it turned out that the
undefined becomes a
null when it arrives on the server.
Which is odd, of course—but also annoying as the
check x, Match.Optional String fails for
String are acceptable).
To overcome this, here is a new Match.Optional that does the trick:
Match.OptionalOrNull = (pattern) -> Match.OneOf undefined, null, pattern
Also, I keep wondering: what’s the purpose of making
Match.Optional fail on
null in the first place?