Meteor check and Match not working correctly

I see that most people use simpleSchema for validation but I stumbled across meteor/check tonight and thought I’d give it a whirl.

Trouble is, it doesn’t work as expected.

Scenario 1 (works) :

check(value, Number)

Value : gfdgdfgdf

Error Result : “Match error: Expected number, got string”

Scenario 2 (not working) :

check(value, Number)

Value : 123

Error Result : “Match error: Expected number, got string”

Scenario 3 (using regexp validation) :

let regexp = '/^[a-z0-9]+$/i'; //lowercase alphanumeric no special chars
check(value, String, Match.Where(value => regexp.test(value)));

Also tried it this way around :

Match.Where(function(value){
    check(value, String);
    let regexp = '/^[a-z0-9]+$/i';
    return regexp.test(value);
});

Value : &*$%#HJHDS

Error Result : None, whatever you input as the value will pass validation, ignoring the regexp completely.

Am I just misunderstanding how check & Match work here or should I just go back to simplSchema?

Can you post exactly what you’re running?
What is value? Where does it come from?

Just to be sure I tested these and got this result:

let value;

// Scenario 1
value='gfdgdfgdf';
check('gfdgdfgdf', Number)
=> Uncaught errorClass {message: "Match error: Expected number, got string", ...}
// Working correctly

// Scenario 2
value=123;
check(value, Number)
=> undefined 
// Working correctly

// Scenario 3
value='&*$%#HJHDS'
let regexp = '/^[a-z0-9]+$/i';
check(value, String, Match.Where(value => regexp.test(value)));
=> Uncaught ReferenceError: value is not defined

// Because check only takes two arguments, re-write:
check(value, Match.Where(value => regexp.test(value)));
=> Uncaught TypeError: regexp.test is not a function

// Because regexp is a string, not a regex. rewrite:
regexp = /^[a-z0-9]+$/i; // no quotes so it's a regexp literal
check(value, Match.Where(value => regexp.test(value)));
=> Uncaught errorClass {message: "Match error: Failed Match.Where validation", ...}
// Working correctly

// Scenario 3 part 2
// With fixes from before:
value='&*$%#HJHDS'
Match.Where(function(value){
    check(value, String);
    let regexp = /^[a-z0-9]+$/i;
    return regexp.test(value);
});
=> Where {condition: ƒ}
// returns a validator function we can pass to check
check(value, Match.Where(function(value){
    check(value, String);
    let regexp = /^[a-z0-9]+$/i;
    return regexp.test(value);
}));
=> Uncaught errorClass {message: "Match error: Failed Match.Where validation", ...}
// Works correctly

I’m guessing it’s something about how you are passing value, maybe add a breakpoint and test the typeof value before continuing

2 Likes

Maybe your value is coming from an input and you didn’t realize it’s a String even if you type 123?

3 Likes

Yes that what exactly the issue. Too many late nights :confused:

1 Like

Thank you for running those tests, you are correct of course, I was assuming that I was sending a Number when it was actually casting a string.

1 Like