My users schema (currently) looks like this:
UserSchema = new SimpleSchema(
{
username: { type: String, required: true },
services: Object,
'services.password': Object,
'services.password.bcrypt': String,
'services.resume': { type: Object, blackbox: true },
emails: { type: Array, label: "Email Addresses" },
'emails.$': Object,
'emails.$.address': String,
'emails.$.verified': Boolean,
createdAt: Date,
profile: { type: Object, blackbox: true },
...
},
{ tracker: Tracker, requiredByDefault: false }
);
In order to use Uniforms to create an automatic form field for a password, I need to do this:
<TextField
name="services.password.bcrypt"
label={false}
placeholder="Password"
showInlineError
/>
Using the actual name seems to be unsafe? It exposes a direct structure for users’ password hashes? Is there a way to create a name alias to be used in form elements?
More on that, how do I create a single text input for the “emails” field? Is “emails.$.address” correct?
My approach is to use one schema for the input form and another for the MongoDB collection. For example, here’s the schema for an input form:
const makeSchema = (allInterests, allParticipants) => new SimpleSchema({
name: String,
description: String,
homepage: String,
picture: String,
interests: { type: Array, label: 'Interests', optional: false },
'interests.$': { type: String, allowedValues: allInterests },
participants: { type: Array, label: 'Participants', optional: true },
'participants.$': { type: String, allowedValues: allParticipants },
});
And here’s the schema for a MongoDB collection:
this.schema = new SimpleSchema({
name: { type: String, index: true, unique: true },
homepage: { type: String, optional: true },
description: { type: String, optional: true },
picture: { type: String, optional: true },
}, { tracker: Tracker });
I find this separation of concerns makes life much easier.
Philip
1 Like