Match.Optional for null

Securing my Meteor app, I came across a strange quirk of Match.Optional of the "check" package.
Example (in CoffeeScript)

  test: (x) -> check x, Match.Optional String

I expected the match to be successful when calling the method like this: '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 null (only undefined or 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?

Leave a Reply

Your email address will not be published. Required fields are marked *