validating custom object in Meteor Simpleschema - meteor

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

Related

Separate form validation with Meteor

I'm using collection2 and I'm trying to get it to handle validation is a specific way. I have a profile schema which looks kind of like this:
Schema.UserProfile = new SimpleSchema({
name: {
type: String,
optional: false
}
location: {
type: String,
optional: true
}
gender: {
type: String,
optional: false
}
});
Schema.User = new SimpleSchema({
username: {
type: String,
optional: true
},
emails: {
type: Array,
optional: true
},
"emails.$": {
type: Object
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
type: Boolean
},
createdAt: {
type: Date
},
profile: {
type: Schema.UserProfile,
optional: true
},
services: {
type: Object,
optional: true,
blackbox: true
},
roles: {
type: [String],
optional: true
},
heartbeat: {
type: Date,
optional: true
}
});
Meteor.users.attachSchema(Schema.User);
Now, on my registration form I'm requiring the user to select their gender and then later once they log in, users are presented with a separate form asking for their name and location. Here's the problem:
The registration form works and everything goes through with saving. When they try to save the internal form with location and name though I get an error:
Error invoking Method 'updateProfile': Gender is required [400]
I know it's happening because it's required in the schema but I've already obtained this information. How do I not require that? Or do I set up validation per form?
You have to add validation through jquery or you can use toaster for display the error on client side.
Read this also : link
I assume you use aldeed:autoform for your forms. When you use normal type in the form, all fields, even those already filled marked as mandatory have to be submitted. Two ways to fix this:
Dirty way: set hidden field with the prefilled value.
You can also set your form type as update as seen in the doc. This way, simple-schema will validate your newDoc already filled with your previous entries without screaming.
The solution number two is the one I use in most cases. This plus autoform's hooks give you enough flexibility to adapt to most use-cases you might encounter.
I don't know whether or not it is a more elegant solution, but we've stopped attaching simpleSchemas to documents in our current project.
We instead have different schemas in each collection's namespace, one for checking user input on insert, one on update, and and one to be used to fill defaultValue when inserting a new doc (which can be done either by the client or the server, in which case we don't check for input). We call .validate() or .clean() depending on what we want to do.
With clever use of the possibility to build schemas from array of schemas, we're not writing bigger schemas in the end (there's more of them though), but we have total control on when we check, and which fields are checked.
From the SimpleSchema docs:
Say you have a required key "friends.address.city" but
"friends.address" is optional. If "friends.address" is set in the
object you're validating, but "friends.address.city" is not, there is
a validation error. However, if "friends.address" is not set, then
there is no validation error for "friends.address.city" because the
object it belongs to is not present.
So the error happens because you're including profile on both forms and gender is not optional within profile. I can think of two ways to solve this:
Have additional objects under profile that are both optional and contain required fields for name/location on one (though it seems like location might be optional in both scenarios based on your code) and a required field for gender on the other. I don't particularly like this solution but it prevents needing form validation.
Use jQuery form validation (I use the package themeteorchef:jquery-validation) and make all your fields in profile optional.
It also looks like SimpleSchema accepts a function for the optional property, so you could use some custom logic there - maybe you get arguments or a context in that function that will allow you to do what you want?
Hope that helps!

Exception in defer callback: Error: When the modifier option is true, validation object must have at least one operator

I'm trying to add the roles package and then set a custom user role like guest or member so I can use it with paid plans. I'm getting the following error
Exception in defer callback: Error: When the modifier option is true, validation object must have at least one operator
at checkModifier (packages/aldeed:simple-schema/simple-schema-validation.js:271:1)
at doValidation1 (packages/aldeed:simple-schema/simple-schema-validation.js:321:1)
When I run the following function
Meteor.methods({
setUserRole: function(userId, roleToSet){
// check(Meteor.userId(), String);
check(userId, String );
check(roleToSet, String);
var user = Meteor.users.findOne(userId);
if (_.isEmpty(user.roles)) {
Roles.addUsersToRoles(userId, roleToSet);
}
}
});
This often means you're trying to $set a field that hasn't been added to the schema.
If you're using Telescope, make sure you call Users.addField() for whatever fields are needed by the Roles package.
This happens when you apply schema to Users collection.
There are two types of roles you can apply:
roles: {
type: Object,
optional: true,
blackbox: true
},
or
roles: {
type: [String],
optional: true
}
You can't use both at the same time. In your case, as you do not use the groups in Roles.addUsersToRoles(userId, roleToSet); you need the second example of roles schema definition.
Just be aware that you will not be able to use groups without changing the scheme.
This error is thrown by simple-schema, it means that an update method is used using a modifier that doesn't have an operator ($set, $unset, .. etc). The latest version of the roles package seems to avoid this in the code related to Roles.addUsersToRoles, but if the error goes away when you comment the line where you use addUsersToRoles method, then maybe you need to
Make sure you are using latest version of roles package or use :
meteor update alanning:roles
Check the code that calls this method and make sure arguments are correct and in correct order
Make sure you are not mixing grouped with non-grouped model (when using the roles package you should choose whether to always use groups or never use them) .. for example :
Roles.addUsersToRoles(userId, roles, Roles.GLOBAL_GROUP)

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.

Unsuccessful Publishing/Subscribing of Record with _id parameter

I'm using Meteor 0.6.5 and I'm trying to publish/subscribe to data that has a specific _id, but it doesn't seem to be successful. The autopublish package has been removed. Below is my code:
Meteor.publish("currentBook", function(bookId){
return Books.find({_id: bookId});
});
Meteor.subscribe("pages", {_id: Session.get("currentBook").id});
Here is the log output from the Chrome console and it does not contain the object I'm looking for:
LocalCollection.Cursor
_transform: null
collection: LocalCollection
cursor_pos: 0
db_objects: null
limit: undefined
reactive: true
selector_f: function (doc) { // 562
selector_id: undefined
skip: undefined
sort_f: null
__proto__: Object
Please let me know how I could resolve this. Thank you
Code:
Meteor.publish("currentBook", function(bookId){
return Books.find(bookId);
});
Deps.autorun(function(){
Meteor.subscribe("currentBook", Session.get("currentBook").id);
});
Publish and subscribe channels must have the same name.
You've wrapped _id in an object in the subscribe channel, but didn't extract it in publish. This wrapping is not necessary.
You should wrap subscribe call in Deps.autorun if you use parameters that can change, like a session variable.
The other thing in addition to Hubert's answer is that using .find() doesn't return the object straight, it returns a lazy cursor which is what you're seeing.
To get the data boxed up into arrays use .find().fetch() when peering in with the console.

Adding a model to Collection in Backbone [duplicate]

I'm running into an odd issue with a Backbone.js Model where an array member is being shown as blank. It looks something like this:
var Session = Backbone.Model.extend({
defaults: {
// ...
widgets: []
},
addWidget: function (widget) {
var widgets = this.get("widgets");
widgets.push(widget);
this.trigger("change:widgets", this, widgets);
},
// ...
// I have a method on the model to grabbing a member of the array
getWidget: function (id) {
console.log(this.attributes);
console.log(this.attributes.widgets);
// ...
}
});
I then add a widget via addWidget. When trying getWidget the result I get (in Chrome) is this:
Object
widgets: Array[1]
0: child
length: 1
__proto__: Array[0]
__proto__: Object
[]
It's showing that widgets is not empty when logging this.attributes but it's shown as empty when logging this.attributes.widgets. Does anyone know what would cause this?
EDIT
I've changed the model to instantiate the widgets array in the initialization method to avoid references across multiple instances, and I started using backbone-nested with no luck.
Be careful about trusting the console, there is often asynchronous behavior that can trip you up.
You're expecting console.log(x) to behave like this:
You call console.log(x).
x is dumped to the console.
Execution continues on with the statement immediately following your console.log(x) call.
But that's not what happens, the reality is more like this:
You call console.log(x).
The browser grabs a reference to x, and queues up the "real" console.log call for later.
Various other bits of JavaScript run (or not).
Later, the console.log call from (2) gets around to dumping the current state of x into the console but this x won't necessarily match the x as it was in (2).
In your case, you're doing this:
console.log(this.attributes);
console.log(this.attributes.widgets);
So you have something like this at (2):
attributes.widgets
^ ^
| |
console.log -+ |
console.log -----------+
and then something is happening in (3) which effectively does this.attributes.widgets = [...] (i.e. changes the attributes.widget reference) and so, when (4) comes around, you have this:
attributes.widgets // the new one from (3)
^
|
console.log -+
console.log -----------> widgets // the original from (1)
This leaves you seeing two different versions of widgets: the new one which received something in (3) and the original which is empty.
When you do this:
console.log(_(this.attributes).clone());
console.log(_(this.attributes.widgets).clone());
you're grabbing copies of this.attributes and this.attributes.widgets that are attached to the console.log calls so (3) won't interfere with your references and you see sensible results in the console.
That's the answer to this:
It's showing that widgets is not empty when logging this.attributes but it's shown as empty when logging this.attributes.widgets. Does anyone know what would cause this?
As far as the underlying problem goes, you probably have a fetch call somewhere and you're not taking its asynchronous behavior into account. The solution is probably to bind to an "add" or "reset" event.
Remember that [] in JS is just an alias to new Array(), and since objects are passed by reference, every instance of your Session model will share the same array object. This leads to all kinds of problems, including arrays appearing to be empty.
To make this work the way you want, you need to initialize your widgets array in the constructor. This will create a unique widget array for each Session object, and should alleviate your problem:
var Session = Backbone.Model.extend({
defaults: {
// ...
widgets: false
},
initialize: function(){
this.set('widgets',[]);
},
addWidget: function (widget) {
var widgets = this.get("widgets");
widgets.push(widget);
this.trigger("change:widgets", this, widgets);
},
// ...
// I have a method on the model to grabbing a member of the array
getWidget: function (id) {
console.log(this.attributes);
console.log(this.attributes.widgets);
// ...
}
});
Tested in a fiddle with Chrome and Firefox: http://jsfiddle.net/imsky/XBKYZ/
var s = new Session;
s.addWidget({"name":"test"});
s.getWidget()
Console output:
Object
widgets: Array[1]
__proto__: Object
[
Object
name: "test"
__proto__: Object
]

Resources