Petrov
Petrov

Reputation: 4250

Meteor.js : How to run check() when arguments are Mongodb ObjectId's?

In some of my Meteor methods, I'm sending Mongodb ObjectId's from the client as arguments. I'd like to run these through Meteor's check() system but I can't seem to find anything that matches successfully with them.

I've tried

var someObjectId = Meteor.Collection.ObjectId();

check(someObjectId, Meteor.Collection.ObjectId()) // fails
check(someObjectId, { _str : String }) //fails
check(someObjectId, String) //fails

any help much appreciated !

Upvotes: 14

Views: 8608

Answers (5)

magicgregz
magicgregz

Reputation: 7669

Complete answer to the original question:

First, define a matcher for a single object in your array of arguments:

Match._id = Match.Where(function (id) {
  check(id, String);
  return /[a-zA-Z0-9]{17,17}/.test(id);
});

Then you can call:

check(MyArrayOfArguments, [Match._id])

Upvotes: 0

Michel Floyd
Michel Floyd

Reputation: 20226

Normally when using check() you're not in a position to generate a new Meteor _id. Here's an alternative using Match.check()

First extend the Match object with:

Match._id = Match.Where(function (id) {
  check(id, String);
  return /[a-zA-Z0-9]{17,17}/.test(id);
});

This is useful because you're likely to be checking _ids in many of your methods.

Now simply:

check(_id,Match._id);

more on this pattern

Upvotes: 1

Brad M
Brad M

Reputation: 7898

As an alternative solution, you could simply pass the hexadecimal string as an argument instead of the ObjectID.

var idValidator = Match.Where(function (id) {
    check(id, String);
    return /[0-9a-fA-F]{24}/.test(id);
});

check(new Meteor.Collection.ObjectID()._str, idValidator);
// success

check('', idValidator);
// Error: Match error: Failed Match.Where validation

check({}, idValidator);
// Error: Match error: Expected string, got object

check([], idValidator);
// Error: Match error: Expected string, got object <--- bug? I expect array

Note, this regular expression is pulled from here.

https://github.com/mongodb/js-bson/blob/master/lib/bson/objectid.js

Upvotes: 2

sbking
sbking

Reputation: 7680

Instead of:

check(someObjectId, Meteor.Collection.ObjectID());

Try without the parentheses:

check(someObjectId, Meteor.Collection.ObjectID);

Edit-

Note that the error message for this check isn't ideal.

check({}, Meteor.Collection.ObjectID);
// Error: Match error: Expected

You could assume the message should be something like

// Error: Match error: Expected ObjectId, got object

You can see why this happens in this snippet from the check package.

https://github.com/meteor/meteor/blob/devel/packages/check/match.js

if (pattern instanceof Function) {
    if (value instanceof pattern)
        return;
    // XXX what if .name isn't defined
    throw new Match.Error("Expected " + pattern.name);
}

Meteor.Collection.ObjectID does not have name property.

Upvotes: 11

waitingkuo
waitingkuo

Reputation: 93794

You should use following to generate a random ObjectID:

var someObjectId = new Meteor.Collection.ObjectID();

As Cuberto said, you can then check it by Meteor.Collection.ObjectID:

check(someObjectId, Meteor.Collection.ObjectID)

Upvotes: 1

Related Questions