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

Adding minlength and maxlength to Schema with String as type #1457

Closed
justin-john opened this issue Apr 24, 2013 · 13 comments
Closed

Adding minlength and maxlength to Schema with String as type #1457

justin-john opened this issue Apr 24, 2013 · 13 comments

Comments

@justin-john
Copy link

Add a new feature for minimum length and maximum length to Schema with String as type. This will validate minimum and maximum length to corresponding string in schema.

For example,

var s = new Schema({ name: { type: String, minlength: 5, maxlength: 15 }});

OR

var s = new Schema({ name: { type: String, min: 5, max: 15 }});
@aheckmann
Copy link
Collaborator

This belongs as a plugin.

@justin-john
Copy link
Author

Can you please be more specific?

I can't find a plugin regarding this feature.

@aheckmann
Copy link
Collaborator

You could extend the string schematype similar to how match works.

The plugin would just require mongoose and add the function to mongoose.Schema.Types.String.prototype.

@justin-john
Copy link
Author

I think, SchemaString.prototype.minlength and SchemaString.prototype.maxlength can be easily added similar to min and max methods implemented for number schema with slight change. IMO, I don't think it needs to be a plugin as it's similar feature like min and max methods of number schema. So it needs to come as feature provided by mongoose, not by additional plugin.

@aheckmann
Copy link
Collaborator

Not gonna happen. Mongoose is bloated enough and exposes the apis necessary to extend it.

@sandesh27
Copy link

Hi,

This can be handled using path function like this:

var Person = new Schema({
title : { type: String, required: true }
, age : { type: Number, min: 5, max: 20 }
, meta : {
likes : [String]
, birth : { type: Date, default: Date.now }
}
});

Person.path('title').validate(function (v) {
return v.length > 50;
});

@vedi
Copy link

vedi commented Feb 12, 2015

looks like it's on its way to the next release: b96d724

@vedi
Copy link

vedi commented Feb 12, 2015

@sandesh27, thank you for workaround

@vkarpov15
Copy link
Collaborator

Its in 4.0.0-rc2 :)

@jeremyml
Copy link

jeremyml commented Aug 4, 2015

Here is the documentation for the new minlength validator http://mongoosejs.com/docs/api.html#schema_string_SchemaString-minlength

@tejasmanohar
Copy link

Just stumbled upon this. I personally think the ORM/ODM should include as few validations and whatnot built-in, as these are really application concerns, and there are plenty of libraries for this. Keep it lean :)

@gayan27
Copy link

gayan27 commented Mar 7, 2017

how to present date type in schema

@Automattic Automattic locked as resolved and limited conversation to collaborators Dec 14, 2018
@vkarpov15
Copy link
Collaborator

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants