Keep in Trim

Reviewing a candidate take-home coding assignment solution is usually not the most thrilling task.
But when you get to learn a new trick, it gets much more interesting.

Today I learned about the trim flag for mongoose String schema type.

Say you define a new schema with a field of type String.
Adding the trim: true option will always call JavaScript .trim() method on the string value when set.

I found it especially useful for ‘email’ fields.

Example

const userSchema = new Schema({ email: { type: String, trim: true }})
const userModel = db.model('User', userSchema)

const email = '   til@danielkorn.io   '
console.log(email.length) // 23

const user = new userModel({ email: email })
console.log(user.email.length) // 17

Happy Trimming! Trimming gif

comments powered by Disqus