Extract validation functions in the separate file

This commit is contained in:
ArtemBaskal 2020-07-03 19:10:05 +03:00
parent c12309a1b2
commit 0c4905fa2b
13 changed files with 275 additions and 207 deletions

View File

@ -3,8 +3,8 @@ import PropTypes from 'prop-types';
import { Field, reduxForm } from 'redux-form';
import { Trans, withTranslation } from 'react-i18next';
import flow from 'lodash/flow';
import { renderInputField, required, isValidPath } from '../../helpers/form';
import { renderInputField } from '../../helpers/form';
import { validatePath, validateRequiredValue } from '../../helpers/validators';
import { FORM_NAME } from '../../helpers/constants';
const Form = (props) => {
@ -28,7 +28,7 @@ const Form = (props) => {
component={renderInputField}
className="form-control"
placeholder={t('enter_name_hint')}
validate={[required]}
validate={[validateRequiredValue]}
normalizeOnBlur={(data) => data.trim()}
/>
</div>
@ -40,7 +40,7 @@ const Form = (props) => {
component={renderInputField}
className="form-control"
placeholder={t('enter_url_or_path_hint')}
validate={[required, isValidPath]}
validate={[validateRequiredValue, validatePath]}
normalizeOnBlur={(data) => data.trim()}
/>
</div>

View File

@ -3,10 +3,8 @@ import PropTypes from 'prop-types';
import { Field, reduxForm } from 'redux-form';
import { Trans, withTranslation } from 'react-i18next';
import flow from 'lodash/flow';
import {
renderInputField, required, domain, answer,
} from '../../../helpers/form';
import { renderInputField } from '../../../helpers/form';
import { validateAnswer, validateDomain, validateRequiredValue } from '../../../helpers/validators';
import { FORM_NAME } from '../../../helpers/constants';
const Form = (props) => {
@ -34,7 +32,7 @@ const Form = (props) => {
type="text"
className="form-control"
placeholder={t('form_domain')}
validate={[required, domain]}
validate={[validateRequiredValue, validateDomain]}
/>
</div>
@ -61,7 +59,7 @@ const Form = (props) => {
type="text"
className="form-control"
placeholder={t('form_answer')}
validate={[required, answer]}
validate={[validateRequiredValue, validateAnswer]}
/>
</div>
</div>

View File

@ -13,13 +13,12 @@ import Tabs from '../../ui/Tabs';
import Examples from '../Dns/Upstream/Examples';
import { toggleAllServices } from '../../../helpers/helpers';
import {
required,
clientId,
renderInputField,
renderGroupField,
renderSelectField,
renderServiceField,
} from '../../../helpers/form';
import { validateClientId, validateRequiredValue } from '../../../helpers/validators';
import { FORM_NAME, SERVICES } from '../../../helpers/constants';
import './Service.css';
@ -48,12 +47,12 @@ const settingsCheckboxes = [
const validate = (values) => {
const errors = {};
const { name, ids } = values;
errors.name = required(name);
errors.name = validateRequiredValue(name);
if (ids && ids.length) {
const idArrayErrors = [];
ids.forEach((id, idx) => {
idArrayErrors[idx] = required(id) || clientId(id);
idArrayErrors[idx] = validateRequiredValue(id) || validateClientId(id);
});
if (idArrayErrors.length) {

View File

@ -4,11 +4,9 @@ import PropTypes from 'prop-types';
import { Field, reduxForm, formValueSelector } from 'redux-form';
import { Trans, withTranslation } from 'react-i18next';
import flow from 'lodash/flow';
import {
renderInputField, required, ipv4, isPositive, toNumber,
} from '../../../helpers/form';
import { renderInputField, toNumber } from '../../../helpers/form';
import { FORM_NAME } from '../../../helpers/constants';
import { validateIpv4, validateIsPositiveValue, validateRequiredValue } from '../../../helpers/validators';
const renderInterfaces = ((interfaces) => (
Object.keys(interfaces).map((item) => {
@ -96,7 +94,7 @@ let Form = (props) => {
name="interface_name"
component="select"
className="form-control custom-select"
validate={[required]}
validate={[validateRequiredValue]}
>
<option value="" disabled={enabled}>
{t('dhcp_interface_select')}
@ -125,7 +123,7 @@ let Form = (props) => {
type="text"
className="form-control"
placeholder={t('dhcp_form_gateway_input')}
validate={[ipv4, required]}
validate={[validateIpv4, validateRequiredValue]}
/>
</div>
<div className="form__group form__group--settings">
@ -137,7 +135,7 @@ let Form = (props) => {
type="text"
className="form-control"
placeholder={t('dhcp_form_subnet_input')}
validate={[ipv4, required]}
validate={[validateIpv4, validateRequiredValue]}
/>
</div>
</div>
@ -155,7 +153,7 @@ let Form = (props) => {
type="text"
className="form-control"
placeholder={t('dhcp_form_range_start')}
validate={[ipv4, required]}
validate={[validateIpv4, validateRequiredValue]}
/>
</div>
<div className="col">
@ -166,7 +164,7 @@ let Form = (props) => {
type="text"
className="form-control"
placeholder={t('dhcp_form_range_end')}
validate={[ipv4, required]}
validate={[validateIpv4, validateRequiredValue]}
/>
</div>
</div>
@ -179,7 +177,7 @@ let Form = (props) => {
type="number"
className="form-control"
placeholder={t('dhcp_form_lease_input')}
validate={[required, isPositive]}
validate={[validateRequiredValue, validateIsPositiveValue]}
normalize={toNumber}
/>
</div>

View File

@ -3,10 +3,8 @@ import PropTypes from 'prop-types';
import { Field, reduxForm } from 'redux-form';
import { Trans, withTranslation } from 'react-i18next';
import flow from 'lodash/flow';
import {
renderInputField, ipv4, mac, required,
} from '../../../../helpers/form';
import { renderInputField } from '../../../../helpers/form';
import { validateIpv4, validateMac, validateRequiredValue } from '../../../../helpers/validators';
import { FORM_NAME } from '../../../../helpers/constants';
const Form = (props) => {
@ -31,7 +29,7 @@ const Form = (props) => {
type="text"
className="form-control"
placeholder={t('form_enter_mac')}
validate={[required, mac]}
validate={[validateRequiredValue, validateMac]}
/>
</div>
<div className="form__group">
@ -42,7 +40,7 @@ const Form = (props) => {
type="text"
className="form-control"
placeholder={t('form_enter_ip')}
validate={[required, ipv4]}
validate={[validateRequiredValue, validateIpv4]}
/>
</div>
<div className="form__group">

View File

@ -3,31 +3,26 @@ import PropTypes from 'prop-types';
import { Field, reduxForm } from 'redux-form';
import { Trans, useTranslation } from 'react-i18next';
import { shallowEqual, useSelector } from 'react-redux';
import {
biggerOrEqualZero,
maxValue,
renderInputField,
required,
toNumber,
} from '../../../../helpers/form';
import { FORM_NAME } from '../../../../helpers/constants';
import { renderInputField, toNumber } from '../../../../helpers/form';
import { validateBiggerOrEqualZeroValue, getMaxValueValidator, validateRequiredValue } from '../../../../helpers/validators';
import { FORM_NAME, SECONDS_IN_HOUR } from '../../../../helpers/constants';
const maxValue3600 = maxValue(3600);
const validateMaxValue3600 = getMaxValueValidator(SECONDS_IN_HOUR);
const getInputFields = ({ required, maxValue3600 }) => [{
const getInputFields = ({ validateRequiredValue, validateMaxValue3600 }) => [{
name: 'cache_size',
title: 'cache_size',
description: 'cache_size_desc',
placeholder: 'enter_cache_size',
validate: required,
validate: validateRequiredValue,
},
{
name: 'cache_ttl_min',
title: 'cache_ttl_min_override',
description: 'cache_ttl_min_override_desc',
placeholder: 'enter_cache_ttl_min_override',
max: 3600,
validate: maxValue3600,
max: SECONDS_IN_HOUR,
validate: validateMaxValue3600,
},
{
name: 'cache_ttl_max',
@ -49,8 +44,8 @@ const Form = ({
const minExceedsMax = cache_ttl_min > cache_ttl_max;
const INPUTS_FIELDS = getInputFields({
required,
maxValue3600,
validateRequiredValue,
validateMaxValue3600,
});
return <form onSubmit={handleSubmit}>
@ -71,7 +66,7 @@ const Form = ({
disabled={processingSetConfig}
normalize={toNumber}
className="form-control"
validate={[biggerOrEqualZero].concat(validate || [])}
validate={[validateBiggerOrEqualZeroValue].concat(validate || [])}
min={0}
max={max}
/>

View File

@ -9,12 +9,11 @@ import {
renderInputField,
renderRadioField,
renderSelectField,
required,
ipv4,
ipv6,
biggerOrEqualZero,
toNumber,
} from '../../../../helpers/form';
import {
validateBiggerOrEqualZeroValue, validateIpv4, validateIpv6, validateRequiredValue,
} from '../../../../helpers/validators';
import { BLOCKING_MODES, FORM_NAME } from '../../../../helpers/constants';
const checkboxes = [{
@ -36,12 +35,12 @@ const checkboxes = [{
const customIps = [{
description: 'blocking_ipv4_desc',
name: 'blocking_ipv4',
validateIp: ipv4,
validateIp: validateIpv4,
},
{
description: 'blocking_ipv6_desc',
name: 'blocking_ipv6',
validateIp: ipv6,
validateIp: validateIpv6,
}];
const getFields = (processing, t) => Object.values(BLOCKING_MODES)
@ -77,7 +76,7 @@ let Form = ({
className="form-control"
placeholder={t('form_enter_rate_limit')}
normalize={toNumber}
validate={[required, biggerOrEqualZero]}
validate={[validateRequiredValue, validateBiggerOrEqualZeroValue]}
/>
</div>
</div>
@ -130,7 +129,7 @@ let Form = ({
component={renderInputField}
className="form-control"
placeholder={t('form_enter_ip')}
validate={[validateIp, required]}
validate={[validateIp, validateRequiredValue]}
/>
</div>
</div>)}

View File

@ -10,10 +10,8 @@ import {
renderSelectField,
renderRadioField,
toNumber,
port,
portTLS,
isSafePort,
} from '../../../helpers/form';
import { validateIsSafePort, validatePort, validatePortTLS } from '../../../helpers/validators';
import i18n from '../../../i18n';
import KeyStatus from './KeyStatus';
import CertificateStatus from './CertificateStatus';
@ -46,7 +44,8 @@ const clearFields = (change, setTlsConfig, t) => {
};
// eslint-disable-next-line no-alert
if (window.confirm(t('encryption_reset'))) {
Object.keys(fields).forEach((field) => change(field, fields[field]));
Object.keys(fields)
.forEach((field) => change(field, fields[field]));
setTlsConfig(fields);
}
};
@ -158,7 +157,7 @@ let Form = (props) => {
type="number"
className="form-control"
placeholder={t('encryption_https')}
validate={[port, isSafePort]}
validate={[validatePort, validateIsSafePort]}
normalize={toNumber}
onChange={handleChange}
disabled={!isEnabled}
@ -180,7 +179,7 @@ let Form = (props) => {
type="number"
className="form-control"
placeholder={t('encryption_dot')}
validate={[portTLS]}
validate={[validatePortTLS]}
normalize={toNumber}
onChange={handleChange}
disabled={!isEnabled}

View File

@ -505,3 +505,5 @@ export const FORM_NAME = {
};
export const smallScreenSize = 767;
export const SECONDS_IN_HOUR = 60 * 60;

View File

@ -1,12 +1,7 @@
import React, { Fragment } from 'react';
import { Trans } from 'react-i18next';
import PropTypes from 'prop-types';
import i18next from 'i18next';
import {
R_IPV4, R_MAC, R_HOST, R_IPV6, R_CIDR, R_CIDR_IPV6,
UNSAFE_PORTS, R_URL_REQUIRES_PROTOCOL, R_WIN_ABSOLUTE_PATH, R_UNIX_ABSOLUTE_PATH,
} from './constants';
import { createOnBlurHandler } from './helpers';
import { R_UNIX_ABSOLUTE_PATH, R_WIN_ABSOLUTE_PATH } from './constants';
export const renderField = (props, elementType) => {
const {
@ -234,138 +229,15 @@ renderServiceField.propTypes = {
}).isRequired,
};
// Validation functions
// If the value is valid, the validation function should return undefined.
// https://redux-form.com/6.6.3/examples/fieldlevelvalidation/
export const required = (value) => {
const formattedValue = typeof value === 'string' ? value.trim() : value;
if (formattedValue || formattedValue === 0 || (formattedValue && formattedValue.length !== 0)) {
return undefined;
}
return <Trans>form_error_required</Trans>;
};
export const maxValue = (maximum) => (value) => (value && value > maximum ? i18next.t('value_not_larger_than', { maximum }) : undefined);
export const ipv4 = (value) => {
if (value && !R_IPV4.test(value)) {
return <Trans>form_error_ip4_format</Trans>;
}
return undefined;
};
export const clientId = (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 <Trans>form_error_client_id_format</Trans>;
}
return undefined;
};
export const ipv6 = (value) => {
if (value && !R_IPV6.test(value)) {
return <Trans>form_error_ip6_format</Trans>;
}
return undefined;
};
export const ip = (value) => {
if (value && !R_IPV4.test(value) && !R_IPV6.test(value)) {
return <Trans>form_error_ip_format</Trans>;
}
return undefined;
};
export const mac = (value) => {
if (value && !R_MAC.test(value)) {
return <Trans>form_error_mac_format</Trans>;
}
return undefined;
};
export const isPositive = (value) => {
if ((value || value === 0) && value <= 0) {
return <Trans>form_error_positive</Trans>;
}
return undefined;
};
export const biggerOrEqualZero = (value) => {
if (value < 0) {
return <Trans>form_error_negative</Trans>;
}
return false;
};
export const port = (value) => {
if ((value || value === 0) && (value < 80 || value > 65535)) {
return <Trans>form_error_port_range</Trans>;
}
return undefined;
};
export const validInstallPort = (value) => {
if (value < 1 || value > 65535) {
return <Trans>form_error_port</Trans>;
}
return undefined;
};
export const portTLS = (value) => {
if (value === 0) {
return undefined;
}
if (value && (value < 80 || value > 65535)) {
return <Trans>form_error_port_range</Trans>;
}
return undefined;
};
export const isSafePort = (value) => {
if (UNSAFE_PORTS.includes(value)) {
return <Trans>form_error_port_unsafe</Trans>;
}
return undefined;
};
export const domain = (value) => {
if (value && !R_HOST.test(value)) {
return <Trans>form_error_domain_format</Trans>;
}
return undefined;
};
export const answer = (value) => {
if (value && (!R_IPV4.test(value) && !R_IPV6.test(value) && !R_HOST.test(value))) {
return <Trans>form_error_answer_format</Trans>;
}
return undefined;
};
export const isValidUrl = (value) => {
if (value && !R_URL_REQUIRES_PROTOCOL.test(value)) {
return <Trans>form_error_url_format</Trans>;
}
return undefined;
};
/**
* @param value {string}
* @returns {*|number}
*/
export const toNumber = (value) => value && parseInt(value, 10);
/**
* @param value {string}
* @returns {boolean}
*/
export const isValidAbsolutePath = (value) => R_WIN_ABSOLUTE_PATH.test(value)
|| R_UNIX_ABSOLUTE_PATH.test(value);
export const isValidPath = (value) => {
if (value && !isValidAbsolutePath(value) && !R_URL_REQUIRES_PROTOCOL.test(value)) {
return <Trans>form_error_url_or_path_format</Trans>;
}
return undefined;
};
export const toNumber = (value) => value && parseInt(value, 10);

View File

@ -0,0 +1,209 @@
import { Trans } from 'react-i18next';
import React from 'react';
import i18next from 'i18next';
import {
R_CIDR,
R_CIDR_IPV6,
R_HOST,
R_IPV4,
R_IPV6,
R_MAC,
R_URL_REQUIRES_PROTOCOL,
UNSAFE_PORTS,
} from './constants';
import { 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 <Trans>form_error_required</Trans>;
};
/**
* @param maximum {number}
* @returns {(value:number) => undefined|string}
*/
export const getMaxValueValidator = (maximum) => (value) => {
if (value && value > maximum) {
i18next.t('value_not_larger_than', { maximum });
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateIpv4 = (value) => {
if (value && !R_IPV4.test(value)) {
return <Trans>form_error_ip4_format</Trans>;
}
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 <Trans>form_error_client_id_format</Trans>;
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateIpv6 = (value) => {
if (value && !R_IPV6.test(value)) {
return <Trans>form_error_ip6_format</Trans>;
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateIp = (value) => {
if (value && !R_IPV4.test(value) && !R_IPV6.test(value)) {
return <Trans>form_error_ip_format</Trans>;
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateMac = (value) => {
if (value && !R_MAC.test(value)) {
return <Trans>form_error_mac_format</Trans>;
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateIsPositiveValue = (value) => {
if ((value || value === 0) && value <= 0) {
return <Trans>form_error_positive</Trans>;
}
return undefined;
};
/**
* @param value {string}
* @returns {boolean|*}
*/
export const validateBiggerOrEqualZeroValue = (value) => {
if (value < 0) {
return <Trans>form_error_negative</Trans>;
}
return false;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validatePort = (value) => {
if ((value || value === 0) && (value < 80 || value > 65535)) {
return <Trans>form_error_port_range</Trans>;
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateInstallPort = (value) => {
if (value < 1 || value > 65535) {
return <Trans>form_error_port</Trans>;
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validatePortTLS = (value) => {
if (value === 0) {
return undefined;
}
if (value && (value < 80 || value > 65535)) {
return <Trans>form_error_port_range</Trans>;
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateIsSafePort = (value) => {
if (UNSAFE_PORTS.includes(value)) {
return <Trans>form_error_port_unsafe</Trans>;
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validateDomain = (value) => {
if (value && !R_HOST.test(value)) {
return <Trans>form_error_domain_format</Trans>;
}
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 <Trans>form_error_answer_format</Trans>;
}
return undefined;
};
/**
* @param value {string}
* @returns {undefined|string}
*/
export const validatePath = (value) => {
if (value && !isValidAbsolutePath(value) && !R_URL_REQUIRES_PROTOCOL.test(value)) {
return <Trans>form_error_url_or_path_format</Trans>;
}
return undefined;
};

View File

@ -10,9 +10,8 @@ import AddressList from './AddressList';
import { getInterfaceIp } from '../../helpers/helpers';
import { ALL_INTERFACES_IP, FORM_NAME } from '../../helpers/constants';
import {
renderInputField, required, validInstallPort, toNumber,
} from '../../helpers/form';
import { renderInputField, toNumber } from '../../helpers/form';
import { validateRequiredValue, validateInstallPort } from '../../helpers/validators';
const STATIC_STATUS = {
ENABLED: 'yes',
@ -212,7 +211,7 @@ class Settings extends Component {
type="number"
className="form-control"
placeholder="80"
validate={[validInstallPort, required]}
validate={[validateInstallPort, validateRequiredValue]}
normalize={toNumber}
onChange={handleChange}
/>
@ -282,7 +281,7 @@ class Settings extends Component {
type="number"
className="form-control"
placeholder="80"
validate={[validInstallPort, required]}
validate={[validateInstallPort, validateRequiredValue]}
normalize={toNumber}
onChange={handleChange}
/>

View File

@ -3,8 +3,8 @@ import PropTypes from 'prop-types';
import { Field, reduxForm } from 'redux-form';
import { Trans, withTranslation } from 'react-i18next';
import flow from 'lodash/flow';
import { renderInputField, required } from '../../helpers/form';
import { renderInputField } from '../../helpers/form';
import { validateRequiredValue } from '../../helpers/validators';
import { FORM_NAME } from '../../helpers/constants';
const Form = (props) => {
@ -28,7 +28,7 @@ const Form = (props) => {
placeholder={t('username_placeholder')}
autoComplete="username"
disabled={processing}
validate={[required]}
validate={[validateRequiredValue]}
/>
</div>
<div className="form__group form__group--settings">
@ -44,7 +44,7 @@ const Form = (props) => {
placeholder={t('password_placeholder')}
autoComplete="current-password"
disabled={processing}
validate={[required]}
validate={[validateRequiredValue]}
/>
</div>
<div className="form-footer">