badguardhome/client/src/helpers/validators.js
Artem Baskal 2a5b0b8da1 - client: Allow change minimum TTL override in UI
Close #2091 #2094 #2056

Squashed commit of the following:

commit a84384bb409bfe60c4bd6477b2249c4431aa3b63
Merge: cdc5f27f a22db5f3
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Mon Sep 14 19:59:47 2020 +0300

    Merge branch 'master' into fix/2091

commit cdc5f27f279f33c7d988f2927adc172e77e0a6af
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Mon Sep 14 15:41:00 2020 +0300

    Fix uint32 fields validation

commit 0c6fcb90f9741ae8a33bf6c4d53bd954f2033a88
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Mon Sep 14 14:43:20 2020 +0300

    Validate DNS cache configuration DNS values unit32 range

commit 1f90a1fcbc04f6c7ffb75b453e5c67e117d1c5bf
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Mon Sep 14 12:11:39 2020 +0300

    Remove the limit on cache-min-ttl

commit 72e961034cc5752a50a4afc57c7be6a93d652f7d
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Fri Sep 11 16:50:19 2020 +0300

    Fix translation

commit 6aebf4b87bb806ac0729b40418ba6056b3f71afa
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Fri Sep 11 12:53:01 2020 +0300

    - client: Allow change minimum TTL override in UI
2020-09-14 20:16:46 +03:00

209 lines
4.6 KiB
JavaScript

import {
MAX_PORT,
R_CIDR,
R_CIDR_IPV6,
R_HOST,
R_IPV4,
R_IPV6,
R_MAC,
R_URL_REQUIRES_PROTOCOL,
STANDARD_WEB_PORT,
UNSAFE_PORTS,
} from './constants';
import { getLastIpv4Octet, isValidAbsolutePath } from './form';
// Validation functions
// https://redux-form.com/8.3.0/examples/fieldlevelvalidation/
// If the value is valid, the validation function should return undefined.
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateRequiredValue = (value) => {
const formattedValue = typeof value === 'string' ? value.trim() : value;
if (formattedValue || formattedValue === 0 || (formattedValue && formattedValue.length !== 0)) {
return undefined;
}
return 'form_error_required';
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateIpv4RangeEnd = (_, allValues) => {
if (!allValues || !allValues.v4 || !allValues.v4.range_end || !allValues.v4.range_start) {
return undefined;
}
const { range_end, range_start } = allValues.v4;
if (getLastIpv4Octet(range_end) <= getLastIpv4Octet(range_start)) {
return 'range_end_error';
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateIpv4 = (value) => {
if (value && !R_IPV4.test(value)) {
return 'form_error_ip4_format';
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateClientId = (value) => {
if (!value) {
return undefined;
}
const formattedValue = value ? value.trim() : value;
if (formattedValue && !(
R_IPV4.test(formattedValue)
|| R_IPV6.test(formattedValue)
|| R_MAC.test(formattedValue)
|| R_CIDR.test(formattedValue)
|| R_CIDR_IPV6.test(formattedValue)
)) {
return 'form_error_client_id_format';
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateIpv6 = (value) => {
if (value && !R_IPV6.test(value)) {
return 'form_error_ip6_format';
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateIp = (value) => {
if (value && !R_IPV4.test(value) && !R_IPV6.test(value)) {
return 'form_error_ip_format';
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateMac = (value) => {
if (value && !R_MAC.test(value)) {
return 'form_error_mac_format';
}
return undefined;
};
/**
* @param value {number}
* @returns {boolean|*}
*/
export const validateBiggerOrEqualZeroValue = (value) => {
if (value < 0) {
return 'form_error_negative';
}
return false;
};
/**
* @param value {number}
* @returns {undefined|string}
*/
export const validatePort = (value) => {
if ((value || value === 0) && (value < STANDARD_WEB_PORT || value > MAX_PORT)) {
return 'form_error_port_range';
}
return undefined;
};
/**
* @param value {number}
* @returns {undefined|string}
*/
export const validateInstallPort = (value) => {
if (value < 1 || value > MAX_PORT) {
return 'form_error_port';
}
return undefined;
};
/**
* @param value {number}
* @returns {undefined|string}
*/
export const validatePortTLS = (value) => {
if (value === 0) {
return undefined;
}
if (value && (value < STANDARD_WEB_PORT || value > MAX_PORT)) {
return 'form_error_port_range';
}
return undefined;
};
/**
* @param value {number}
* @returns {undefined|string}
*/
export const validatePortQuic = validatePortTLS;
/**
* @param value {number}
* @returns {undefined|string}
*/
export const validateIsSafePort = (value) => {
if (UNSAFE_PORTS.includes(value)) {
return 'form_error_port_unsafe';
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateDomain = (value) => {
if (value && !R_HOST.test(value)) {
return 'form_error_domain_format';
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateAnswer = (value) => {
if (value && (!R_IPV4.test(value) && !R_IPV6.test(value) && !R_HOST.test(value))) {
return 'form_error_answer_format';
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validatePath = (value) => {
if (value && !isValidAbsolutePath(value) && !R_URL_REQUIRES_PROTOCOL.test(value)) {
return 'form_error_url_or_path_format';
}
return undefined;
};