Simple Schema Meteor - Array Validation - meteor

I am using this package to validate data before going into a database collection. One field in my collection must contain an array of strings - how can I validate this?

YourCollection.attachSchema(new SimpleSchema({
...
yourArray: {type: [String]},
});

Related

firebase - pushing an object to an array angularFire2

I have a db in which I want to store data like this:
Books:
0:{pushed Object}
1:{pushed object} ...
As if it is an arrays so I can iterate over it and do the stuff I need to do. The problem is that I am storing data as pic related shows:
pic related
It is storing the object with the push_ID firebase assigns to an object but not with the index of an array. This is how I am pushing the object to my database:
let book_data = {
id: bookId,
name: bookData.name,
author: bookData.author,
genre: bookData.genre,
publishDate: bookData.publishDate
};
this.fb.list(`my-lists/${bookList.name}/books/`).push(bookData);
What am I doing wrong?
If needed, I'm coding this for an ionic project.
If you use push() method, Firebase assign it a unique ID without you doing anything else. so if you want a custom key that you defined, you can use set() to save data to a specified reference, also it can be replacing any existing data at that path.
let book_data = {
id: bookId,
name: bookData.name,
author: bookData.author,
genre: bookData.genre,
publishDate: bookData.publishDate
};
let yourkey = 0;
this.fb.list(`my-lists/${bookList.name}/books/${yourkey}`).set(bookData);
When you set new object next time, you have to change the key again and save data

validating custom object in Meteor Simpleschema

I am trying to validate an object using SimpleSchema in Meteor, before inserting it into the database.
The object looks like this, as I print it from the Meteor method that calls the insert:
channels: { '1': [ 'rect4557-6-4-5-7-4-2', 'rect4557-6-4-97-0-7-6-3' ] } }
If I insert it in the database without attaching a schema to it, it works fine. However, when I have it run through SimpleSchema, the field value as outputted from console.log in the custom validation method is an empty object {}. Even if I don't run any validation, an empty object is stored if SimpleSchema is used.
Code to produce the simpleschema value output:
Arch.schema = new SimpleSchema({
channels: {
type: Object,
custom: function validateChannels() {
console.log("this.value:", this.value)
}
});
Architectures.attachSchema(Architectures.schema);
Really, what should I do? Is this a bug in SimpleSchema?
It looks like you just need to add blackbox: true option. SimpleSchema does not support arbitrary object keys unless you mark it as a blackbox object. See https://github.com/aldeed/meteor-simple-schema#blackbox
The filtering, which is part of the automatic cleaning, is what strips this out for you. If you want to prevent that in a specific insert call, just pass filter: false option. See https://github.com/aldeed/meteor-collection2#skip-removing-properties-that-are-not-in-the-schema

How to modify an already constructed schema

I'm using a third party package that defines a schema like this:
People.schema = new SimpleSchema({
firstName: {
type: String,
optional: false
}
//Many other fields defined...
});
I would like to modify it to have optional: true for the first name without changing the source code for the third party package.
I could use People.schema.pick to get a schema with all of the fields except the firstName, and then combine this with another schema with firstName as optional. But this method would require listing all of the fields in the schema in the pick function, which is tedious.
Is there a better way of accomplishing this?
I can edit the object simple schema creates just like any other object:
People.schema._schema.firstName.optional = true.
To override the field.

Meteor update array of objects

I'm using Schema with this Meteor project. I've got an array of objects in the Schema, which I create them this way:
'milestones.$.name'
I create some properties like this, and allow for dynamic insert.
Now, I want to grab a milestone by name from milestones array and update some of its properties.
How can I do this?
I'm trying this at the moment, but still no positive result:
Projects.update(
{_id: currentPostId, 'milestones.name':this.name},
{$set: {'milestones.$': {name: this.name, hours: this.hours, complete:true}}}
);

Meteor update a Collection object

in Meteor, I'm having a collection with a Schema, and a number of items are added dynamically.
In this case, I'm dealing with milestones object, and once the user check one off I want to update complete in this Collections item to true (default is false)
Here is my schema
milestones: {
type: Array,
optional: true
},
'milestones.$': {
type: Object
},
'milestones.$.name': {
type: String
},
'milestones.$.hours': {
type: Number
},
'milestones.$.complete': {
type: Boolean
}
How do I write a $set statement for this?
You have an array of objects so, $elemMatch do the trick here.
Projects.update({_id:this._id},{milestones:{$elemMatch:{'milestones.$‌​.name':this.name}},{$set:{'milestone.$.complete':value}}})
So thanks to Aldeed I found a solution - which needs to be called on server side, otherwise it won't let the update happen. Do:
Projects.update({_id:currentPostId, 'milestones.name':name}, {$set:{'milestones.$.complete':true}});
The function is called on the client with Meteor.call with all needed params.
According to your schema you have an object containing an array of objects. So you should write you $set like this:
{$set: {'milestone.$.complete':value}}
This will update the first array element corresponding to the query.
You can find here the official documentation if you want to know more about arrays updates in Mongo.

Resources