Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embedded doc - no validation #722

Closed
mmukhin opened this issue Feb 8, 2012 · 5 comments
Closed

embedded doc - no validation #722

mmukhin opened this issue Feb 8, 2012 · 5 comments

Comments

@mmukhin
Copy link

mmukhin commented Feb 8, 2012

I'm assuming this was not intended and that I dont have errors :)

var Person = new Schema({
    first_name      : { type: String, required: true, trim: true }
  , last_name       : { type: String, required: true, trim: true }
  , username        : { type: String, required: true, trim: true, unique: true }
  , hobbies         : [Hobby]
  , shoe_size       : Number
  , eye_color       : String
});

function sampleval(v){
    return v.length < 3;
}
var Hobby = new Schema({
    name            : { type: String, required: true, trim: true, unique: true, validate: [sampleval] }
});

None of the Hobby validations work (unique, required (if blank string after trimming), sampleval)...

    Person.findOne({ username: req.params.username }, function(error, person){
        if(error){
            res.json(error);
        }
        else if(person == null){
            res.json('no such user!')
        }
        else{
            person.hobbies.push({ name: req.params.hobby });
            person.save( function(error, data){
                if(error){
                    res.json(error);
                }
                else{
                    res.json(data);
                }
            });
        }
    });
@aheckmann
Copy link
Collaborator

your schemas are out of order. declare the embedded doc schema before passing it into the parent schema.

@mmukhin
Copy link
Author

mmukhin commented Feb 8, 2012

Does it work for updating schemas? I put in a validator for "min characters = 2" for the Hobby.name and it works, but "unique still doesn't"

@aheckmann
Copy link
Collaborator

unique is not a validator. it is a unique index. the index creation is probably failing. listen to errors on your connection to see if thats whats up.

mongoose.connect(..);
mongoose.connection.on('error', console.error);

or

var db = mongoose.createConnection(..);
db.on('errro', console.error);

@mmukhin
Copy link
Author

mmukhin commented Feb 8, 2012

Oh thats probably it then.. thanks! Is there any built-in validation for uniqueness of key:val (e.g. a unique username) for a given collection?

@aheckmann
Copy link
Collaborator

no

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants