Commit 2d8de8ea authored by peterhegman's avatar peterhegman

Move ip address integer check to main validator

It was confirmed that we also want this check for Geo settings
parent a93172ee
......@@ -2,9 +2,7 @@ import validateIpAddress from 'ee/validators/ip_address';
import { __, sprintf } from '~/locale';
export default address => {
// Reject IP addresses that are only integers to match Ruby IPAddr
// https://github.com/whitequark/ipaddr.js/issues/7#issuecomment-158545695
if (/^\d+$/.exec(address) || !validateIpAddress(address)) {
if (!validateIpAddress(address)) {
return sprintf(__('%{address} is an invalid IP address range'), { address }, false);
}
......
import ipaddr from 'ipaddr.js';
export default address => {
// Reject IP addresses that are only integers to match Ruby IPAddr
// https://github.com/whitequark/ipaddr.js/issues/7#issuecomment-158545695
if (/^\d+$/.exec(address)) {
return false;
}
try {
// Checks if Valid IPv4/IPv6 (CIDR) - Throws if not
return Boolean(ipaddr.parseCIDR(address));
......
......@@ -2,19 +2,6 @@ import * as validateIpAddress from 'ee/validators/ip_address';
import validateRestrictedIpAddress from 'ee/groups/settings/access_restriction_field/validate_ip_address';
describe('validateRestrictedIpAddress', () => {
describe('when IP address is only integers', () => {
it.each`
address
${1}
${19}
${192}
`('$address - returns an error message', ({ address }) => {
expect(validateRestrictedIpAddress(address)).toBe(
`${address} is an invalid IP address range`,
);
});
});
describe('when `validateIpAddress` returns false', () => {
it('returns an error message', () => {
validateIpAddress.default = jest.fn(() => false);
......
......@@ -2,6 +2,17 @@ import ipaddr from 'ipaddr.js';
import validateIpAddress from 'ee/validators/ip_address';
describe('validateIpAddress', () => {
describe('when IP address is only integers', () => {
it.each`
address
${1}
${19}
${192}
`('$address - returns false', ({ address }) => {
expect(validateIpAddress(address)).toBe(false);
});
});
describe('when IP address is in valid CIDR format', () => {
it('returns true', () => {
ipaddr.parseCIDR = jest.fn(() => [
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment