Separate form validation with Meteor - 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!

Related

Simple Schema and multiple forms

I have a user profile schema that looks sort of like this:
Schema.UserProfile = new SimpleSchema({
name: {
type: String,
optional: false
},
gender: {
type: String,
optional: false,
allowedValues: ['Male', 'Female']
},
location: {
type: String,
optional: false
},
age: {
type: Number,
optional: false
}
}
My registration form requires the user to select their gender on it. Once registered, the user is presented with another form to fill out the rest of the data: name, location, and age.
Here's the problem: When the user tries to register, I get the error: Name is required. The user isn't suppose to enter their name until they register.
What is the proper approach to only validating data that is actually being saved on a specific form?
EDIT
Here is my registration script:
var data = {};
var profile = {};
data.email = $('#email').val();
data.password = $('#password').val();
profile.gender = $('#gender').val();
data.profile = profile;
Accounts.createUser(data, function (error) {
// do
// ERROR: Name is required
});
When the user actually registers, they're presented with a form to fill the other data in. The form submit calls a method that the tries to update the profile object.
By using the option optional: false you are making it mandatory whenever creating the object.
One solution could be to use myCollection.insert(); with the flag validate: false instead of createUser().
e.g.
myCollection.insert(doc, {validate: false});
This will not trigger the error, since is skipping the validation.
I would personally not use this way, but it might be a solution.
You can use SimpleSchema's pick command to select certain elements from a schema. That way you can have multiple schemas derived from the same original schema definition.
Schemas.UserProfileRegistration1 = Schemas.UserProfile.pick(['gender']);
Schemas.UserProfileRegistration2 = Schemas.UserProfile.pick(['name', 'location', 'age']);
https://github.com/aldeed/meteor-simple-schema#extracting-simpleschemas

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.

Best way to exclude a Meteor value from the database?

What is the best way to exclude a field value from the data in Meteor when using Autoform, SimpleSchema, Collection2, etc? Say I have:
MySchema = new SimpleSchema({
password: {
type: String,
label: "Enter a password",
min: 8
},
confirmPassword: {
type: String,
label: "Enter the password again",
min: 8,
custom: function () {
if (this.value !== this.field('password').value) {
return "passwordMismatch";
}
}
}
});
... and I do not want to confirmPassword field persisted to the database, what is the best way to handle that? I assume using hooks, but if so where and how? Hopefully there is a way to just exclude one (or more) values without having to redefine the whole whole schema to say which to include and which to exclude. If I have 100 fields and want to exlcude 1, hopefullly the hook or whatever does not need the other 99 defiled too.
TIA
With autoform, you must use a method on the server side. Simply delete the field in the method code when you receive it at the server before inserting the document.

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.

Resources