-
Notifications
You must be signed in to change notification settings - Fork 147
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
mongodb://.. as a validating url #93
Comments
@c-hartmann If you want to validate that the protocol is "mongodb://", you can use joi's var Joi = require('joi');
function isMongoUrl(url) {
var mongodbSchema = Joi.string().uri({
scheme: ['mongodb']
});
Joi.assert(url, mongodbSchema);
} Actually, the validator library supports custom schemes too: function isMongoUrl(url) {
if (!validator.isURL(url, {
protocols: ['mongodb']
})) {
throw new Error('must be URL like');
}
} |
I totally agree with @pdehaan suggestions. We would loose some "validation strictness" if we set Here is a full working code: const validator = require('validator');
convict.addFormat({
name: 'mongo-uri',
validate: function(val) {
if (!validator.isURL(val, {protocols: ['mongodb']})) {
throw new Error('must be a MongoDB URI');
}
}
});
var conf = convict({
uri: {
doc: 'Mongo URI',
format: 'mongo-uri',
default: ''
}
});
conf.load({uri: 'mongodb://localhost:27017/some_db_name'});
conf.validate({strict: true}); @c-hartmann could you please close this issue? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would love to see mongodb://.. URI style to validate against 'url' format, as featured in:
http://blog.nodejitsu.com/npmawesome-managing-app-configuration-with-convict/
From what i can see, this might require to use:
instead of using it without the options object.
A custom validator function works fine this way:
The text was updated successfully, but these errors were encountered: