I am using validate, and need to validate a host, and would like to have it fail, if http://, https:// or it is ending with / as in https://google.com/. It should fail if it is anything but of the form google.com.
Does anyone know how to do that?
const Schema = require('validate');
const p_schema = new Schema({
Host: {
type: String,
match: /???/,
required: true
},
});
let p = {
Host: 'https://google.com/'
}
const errors = p_schema.validate(p)
>Solution :
You want to check if your string contains a valid hostname, and you want your validation to fail if your string contains extraneous junk.
For that, use the npm is-valid-hostname package, and avoid creating your own regex for the purpose. There are lots of weird edge cases in hostnames, so you may as well use fully-debugged code rather than taking on the testing burden of writing your own regex.
const isValidHostname = require('is-valid-hostname')
isValidHostname('https://google.com/') // false
isValidHostname('google.com:443') // false
isValidHostname('google.com') // true