Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ const isFQDN = (input: string) => {
}

// "best effort" regex-based IP address check
// If you want a more exhaustive check, create your own custom validator, perhaps wrapping this
// implementation (the source of the ipv4 regex below): https://github.com/validatorjs/validator.js/blob/master/src/lib/isIP.js
const ipv4Regex =
/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/
const ipv6Regex = /([a-f0-9]+:+)+[a-f0-9]+/
// If you want a more exhaustive check, create your own custom validator
const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])){3}$/
const ipv6Regex = /^(([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|([0-9a-f]{1,4}:)+:([0-9a-f]{1,4}:)*[0-9a-f]{1,4}|([0-9a-f]{1,4}:)*::([0-9a-f]{1,4}:)*[0-9a-f]{0,4}|::|(([0-9a-f]{1,4}:){1,6}|:):((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9]))(%[0-9a-z.\-:]+)?$/i

const isIP = (input: string) => {
if (!input.length) return false
return ipv4Regex.test(input) || ipv6Regex.test(input)
Expand Down
4 changes: 4 additions & 0 deletions tests/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ test('host()', () => {
assertPassthrough({ FOO: 'localhost' }, spec)
assertPassthrough({ FOO: '192.168.0.1' }, spec)
assertPassthrough({ FOO: '2001:0db8:85a3:0000:0000:8a2e:0370:7334' }, spec)
assertPassthrough({ FOO: '::' }, spec) // Unspecifed IPv6 address RFC4291
assertPassthrough({ FOO: '::1' }, spec) // Loopback IPv6 address RFC4291
assertPassthrough({ FOO: 'fe80::a%en1' }, spec) // Scoped IPv6 address RFC6874
assertPassthrough({ FOO: '64:ff9b::1.1.1.1' }, spec) // NAT64 IPv4/IPv6 translation RFC6052

expect(() => cleanEnv({ FOO: '' }, spec, makeSilent)).toThrow()
expect(() => cleanEnv({ FOO: 'example.com.' }, spec, makeSilent)).toThrow()
Expand Down
Loading