bp123
bp123

Reputation: 3417

Validation error with simple-schema

I'm trying to insert an array into an object and I'm not having any luck. I think the schema is rejecting it based on validation but I'm not sure why. If I console.log(this.state.typeOfWork) and check typeof it states its an Object which contains:

(2) ["Audit - internal", "Audit - external"]
0: "Audit - internal"
1: "Audit - external"

My collection after an update contains:

"roleAndSkills": {
    "typeOfWork": []
  }

Example: Schema

roleAndSkills: { type: Object, optional: true },
  'roleAndSkills.typeOfWork': { type: Array, optional: true },
  'roleAndSkills.typeOfWork.$': { type: String, optional: true }

Example: update

ProfileCandidate.update(this.state.profileCandidateCollectionId, {
      $set: {
        roleAndSkills: {
          typeOfWork: [this.state.typeOfWork]
        }
      }
    });

Upvotes: 0

Views: 816

Answers (3)

Michel Floyd
Michel Floyd

Reputation: 20226

You state that this.state.typeOfWork is an array (of strings) but then when you .update() your document you are enclosing it in square brackets:

ProfileCandidate.update(this.state.profileCandidateCollectionId, {
  $set: {
    roleAndSkills: {
      typeOfWork: [this.state.typeOfWork]
    }
  }
});

Simply remove the redundant square brackets:

ProfileCandidate.update(this.state.profileCandidateCollectionId, {
  $set: {
    roleAndSkills: {
      typeOfWork: this.state.typeOfWork
    }
  }
});

Also since your array is just an array of strings you can simplify your schema a bit by declaring it as such with [String] for the type:

'roleAndSkills.typeOfWork': { type: [String] }

Note furthermore that objects and arrays are by default optional so you can even omit the optional flag.

Upvotes: 0

Béranger
Béranger

Reputation: 673

typeOfWork is an Array. You should push your value in it :

$push: {
    "roleAndSkills.typeOfWork": this.state.typeOfWork
}

for multiple values :

$push: {
    "roleAndSkills.typeOfWork": { $each: [ "val1", "val2" ] }
}

mongo $push operator

mongo dot notation

Upvotes: 0

RSamurai
RSamurai

Reputation: 23

Simple schema has some problems with validation on Objects or Arrays, i had the same problem in a recent app i developed.

What can you do? well, what i did, on the Collections.js file, when you are saying:

typeOfWork:{
  type: Array
}

Try adding the property blackbox:true, like this:

typeOfWork:{
  blackbox: true,
  type: Array
}

This will tell your Schema that this field is taking an Array, but ignore further validation.

The validation i made was on main.js, just to be sure i had no empty array and the data was plain text.

As requested here is my update method, im my case i used objects not arrays but it works the same way.

 editUser: function (editedUserVars, uid) {
      console.log(uid);
      return Utilizadores.update(
        {_id: uid},
        {$set:{
          username: editedUserVars.username,
          usernim: editedUserVars.usernim,
          userrank: {short: editedUserVars.userrank.short,
          long: editedUserVars.userrank.long},
          userspec: {short: editedUserVars.userspec.short,
          long: editedUserVars.userspec.long},
          usertype: editedUserVars.usertype}},
        {upsert: true})

    },

here it the collection schema

UtilizadoresSchema = new SimpleSchema({
 username:{
    type: String
 },
 usernim:{
    type: String
 },
 userrank:{
    blackbox: true,
    type: Object
 },
 userspec:{
    blackbox: true,
    type: Object
 },
 usertype:{
    type: String
 }
});
Utilizadores.attachSchema(UtilizadoresSchema);

Hope it helps

Rob

Upvotes: 0

Related Questions