scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
557 stars 81 forks source link

How to use 'match' properly? #42

Closed michaeljota closed 8 years ago

michaeljota commented 8 years ago

I trying to use 'match', but as #41 it throws not a function.

Here is my schema:

class User extends Document {
    constructor(){
        super();
        //Just the part where using the match. 
        this.email = {
            type: String,
            match: '/.+\@.+\..+/'
        }
}
scottwrobinson commented 8 years ago

You should specify a regex in match. So you're example would be:

class User extends Document {
    constructor(){
        super();
        //Just the part where using the match. 
        this.email = {
            type: String,
            match: /.+\@.+\..+/
        }
}

Although it probably wouldn't hurt to allow plain regex strings as well (like you're using).

Just remove the single quotes ('). Here is a quick RegExp example:

var emailRegex = /.+\@.+\..+/;
emailRegex.test('john@example.com');    // true
emailRegex.test('example.com');    // false

Unless you have any other follow-up questions, I'm going to close the issue. Thanks!

michaeljota commented 8 years ago

Thanks. :+1: