1

I'm trying to use validate.js to validate input from the front end before entering it in the database with node but I'm getting an error that I can't figure out. I've gone over the docs and believe I setup the constraints correctly. The exact error is:

message:"Unknown validator pattern"

my validator is setup like this:

                    let alphanumeric = /^[a-zA-Z0-9]*$/;
                    let constraints = {
                        clientUsername:{
                            presence: true,
                            length: {min:8, max:15},
                            pattern:alphanumeric,
                            message: 'make sure client username is between 8-15 characters, is only numbers and letters'
                        },
                        tileCategory:{
                            presence:true,
                            length:{min:1, max:1},
                            numericality:{
                                onlyInteger:true,
                                lessThanOrEqualTo:tileCategoryNumber,
                            },
                            message:'enter a number, 1 char in length, less than or equal to 3' //the current number of tiles
                        }
                    };

                    validate({clientUsername: input.clientUsername},constraints);

At first I thought it was the regex pattern but tried commenting that out and then it said

message:"Unknown validator messsage"

so I'm guessing there is something wrong with my validator in general.

at the very top I of course included const validate = require('validate.js');

2 Answers2

2

Something similar to this just burned me, have a look at the documentation again.

pattern is sort of a sub-validator of format and should look like:

{
    format: {
        pattern: "[A-Za-z0-9]+"
    }
}

You're trying to use pattern at the "top level". I don't see anything in the documentation that implies helper patterns like alphanumeric exist. (I think the language the tool would use is to say "pattern is an option of the format validator" but I'm not sure.)

Your stated error message also implies a misspelling: it tells you it doesn't recognize messsage, which has 3 of the letter 's' but should have 2.

OwenP
  • 24,950
  • 13
  • 65
  • 102
1

There's (2) things I could see being the issue. Firstly, you're using JS based regexes with the preceding and following /. Try removing these.

Beyond that, I'd recommend trying to remove the alphanumeric parameter & input the regex directly... it may be a type issue as well.

  pattern:"^[a-zA-Z0-9]*$",

Hope this helps! :)

mochsner
  • 307
  • 2
  • 10
  • 1
    given the error that was the first thing I tried. It still doesn't work though. Thanks for trying to help. –  May 30 '18 at 20:15