diff --git a/src/validators.ts b/src/validators.ts index 10f0e95..5daf714 100644 --- a/src/validators.ts +++ b/src/validators.ts @@ -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) diff --git a/tests/validators.test.ts b/tests/validators.test.ts index 6a48373..60ead85 100644 --- a/tests/validators.test.ts +++ b/tests/validators.test.ts @@ -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()