joi icon indicating copy to clipboard operation
joi copied to clipboard

Better way of validating a certain hostname in URI

Open Apidcloud opened this issue 2 years ago • 1 comments

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: 16.9.1
  • module version: 17.4.2
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information:

How can we help?

This is related to https://github.com/sideway/joi/issues/1965. I am wondering if there is a way of validating whether a given URI has a certain hostname. This would be useful, for instance, to validate social media URIs (e.g., instagram, facebook, twitter). In the thread https://github.com/sideway/joi/issues/1965 it is mentioned that the portion can be verified through some new option introduced in v16, but I couldn't figure out how to do it yet. For now I am relying on regex (see example below), though one could have a custom validation step (using node URL.hostname).

facebook: Joi.string()
  .uri({
    scheme: 'https',
	domain: { 
		// is there a way of checking the hostname here?
	}
   })
  .regex(/https:\/\/(www\.)?facebook\.com\/.*/)
  .optional(),

Apidcloud avatar Oct 19 '21 13:10 Apidcloud

@hueniverse I'll love to help out with this, let me know if it's something you think I can help with. Also, let me know if you've got any approach you'd like to me employ.

Thanks.

BolajiOlajide avatar Jan 07 '22 11:01 BolajiOlajide