Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema Type for Email #9

Open
natac13 opened this issue Mar 10, 2016 · 2 comments
Open

Schema Type for Email #9

natac13 opened this issue Mar 10, 2016 · 2 comments
Milestone

Comments

@natac13
Copy link

natac13 commented Mar 10, 2016

Proposal

Create a custom Schema.Type using Mongoose to check that the value is a valid email.

// function that calls a SchemaType with the given key and any options
function Email(key, options) {
  mongoose.SchemaTypes.String.call(this, key, options, 'Email');
}

// link the prototype chains together
Email.prototype = Object.create(mongoose.Schema.prototype);

// shadow the cast method from the linkage
Email.prototype.cast = (value) => {
  const re = /^([A-Z|a-z|0-9](\.|_){0,1})+[A-Z|a-z|0-9]\@([A-Z|a-z|0-9])+((\.){0,1}[A-Z|a-z|0-9]){2}\.[a-z]{2,3}$/;
  if (! re.test(value)) {
    return mongoose.Schema.CastError('Email', 'A non-valid email was provided');
  }
  return value;
}

// add to the Type registry
mongoose.Schema.Types.Email = Email;

// Then use like normal

const User = Schema({
  email: Email,
  name: String
});

export default mongoose.model('User', User);

Alternative

Using a custom validate function which is found on the SchemaType prototype chain.

Usage

function someFunction (value) {
  const re = ... same as above;
  return re.test(value);
}
const custom = [somefunction, 'Custom error message for file {PATH}, due to a passed in value of {VALUE}']; // could be used as the value of the validate key

const User = Schema({
  name: { type: String, validate: somefunction} // will return a SchemaType
})

The someFunction could be an array of arguments to supply the validate function itself.
The error messages support basic templating. By passing in the array it looks to be unpacked and supllied to the function using validate.apply(null, custom) where custom is from the above snippet

Or instead of it directly in the Schema definition itself

User.path('email').validate(someFunction, 'Custom Error message again')

Note for Validate

  • there is no validation call when using update() or findOneAndUpdate()
@natac13 natac13 mentioned this issue Mar 10, 2016
@jakeklassen
Copy link
Contributor

I'm thinking a custom type here could be overkill given String types support match for regular expression validation. If anything we could abstract the location of regular expressions to a module to be reused anywhere else we'd need it, if you are leaning towards re-usability with the custom type.

@natac13
Copy link
Author

natac13 commented Mar 10, 2016

Sounds good, Ill change the User model issue I started

@natac13 natac13 added this to the 1.0.0 milestone Mar 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants