Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Mongoose failing to validate array properties

Mongoose failing to validate array properties

Problem

I'm having some issues with validating an array property in Mongoose.

When I use the following definition, my shouldFail method never gets called, and the record always saves.

shouldFail = (val) ->
  console.log "Fail method called with value:"
  console.log val
  return false

definitions:
  english: [
    type: String
    validate: [ shouldFail, "testing" ]
    required: true
  ]

However if I set up validation as the following, the function gets called and the record is not saved.

Sense.path('definitions.english').validate (val) ->
  console.log "Validating English"
  console.log val
  return false

I'd prefer to use the former definition style if possible. I'm just wondering if I'm doing something wrong in my definition. Is that how you define validation for arrays?

Also I'm not sure if the way I'm setting english is affecting this. I'm just doing definitions.english = [ ] and trying to save.

Problem courtesy of: Ben Humphreys

Solution

I don't think you need the [] on english:, fiddling with the code on the coffeescript compiler it renders out way wrong if you include them. That is likely why your validation isn't working in that format. Try just:

definitions:
  english:
    type: String
    validate: [ shouldFail, "testing" ]
    required: true
Solution courtesy of: Logos

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Mongoose failing to validate array properties

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×