Extract validation functions in the separate file
This commit is contained in:
parent
c12309a1b2
commit
0c4905fa2b
@ -3,8 +3,8 @@ import PropTypes from 'prop-types';
|
|||||||
import { Field, reduxForm } from 'redux-form';
|
import { Field, reduxForm } from 'redux-form';
|
||||||
import { Trans, withTranslation } from 'react-i18next';
|
import { Trans, withTranslation } from 'react-i18next';
|
||||||
import flow from 'lodash/flow';
|
import flow from 'lodash/flow';
|
||||||
|
import { renderInputField } from '../../helpers/form';
|
||||||
import { renderInputField, required, isValidPath } from '../../helpers/form';
|
import { validatePath, validateRequiredValue } from '../../helpers/validators';
|
||||||
import { FORM_NAME } from '../../helpers/constants';
|
import { FORM_NAME } from '../../helpers/constants';
|
||||||
|
|
||||||
const Form = (props) => {
|
const Form = (props) => {
|
||||||
@ -28,7 +28,7 @@ const Form = (props) => {
|
|||||||
component={renderInputField}
|
component={renderInputField}
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('enter_name_hint')}
|
placeholder={t('enter_name_hint')}
|
||||||
validate={[required]}
|
validate={[validateRequiredValue]}
|
||||||
normalizeOnBlur={(data) => data.trim()}
|
normalizeOnBlur={(data) => data.trim()}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@ -40,7 +40,7 @@ const Form = (props) => {
|
|||||||
component={renderInputField}
|
component={renderInputField}
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('enter_url_or_path_hint')}
|
placeholder={t('enter_url_or_path_hint')}
|
||||||
validate={[required, isValidPath]}
|
validate={[validateRequiredValue, validatePath]}
|
||||||
normalizeOnBlur={(data) => data.trim()}
|
normalizeOnBlur={(data) => data.trim()}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
@ -3,10 +3,8 @@ import PropTypes from 'prop-types';
|
|||||||
import { Field, reduxForm } from 'redux-form';
|
import { Field, reduxForm } from 'redux-form';
|
||||||
import { Trans, withTranslation } from 'react-i18next';
|
import { Trans, withTranslation } from 'react-i18next';
|
||||||
import flow from 'lodash/flow';
|
import flow from 'lodash/flow';
|
||||||
|
import { renderInputField } from '../../../helpers/form';
|
||||||
import {
|
import { validateAnswer, validateDomain, validateRequiredValue } from '../../../helpers/validators';
|
||||||
renderInputField, required, domain, answer,
|
|
||||||
} from '../../../helpers/form';
|
|
||||||
import { FORM_NAME } from '../../../helpers/constants';
|
import { FORM_NAME } from '../../../helpers/constants';
|
||||||
|
|
||||||
const Form = (props) => {
|
const Form = (props) => {
|
||||||
@ -34,7 +32,7 @@ const Form = (props) => {
|
|||||||
type="text"
|
type="text"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('form_domain')}
|
placeholder={t('form_domain')}
|
||||||
validate={[required, domain]}
|
validate={[validateRequiredValue, validateDomain]}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -61,7 +59,7 @@ const Form = (props) => {
|
|||||||
type="text"
|
type="text"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('form_answer')}
|
placeholder={t('form_answer')}
|
||||||
validate={[required, answer]}
|
validate={[validateRequiredValue, validateAnswer]}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -13,13 +13,12 @@ import Tabs from '../../ui/Tabs';
|
|||||||
import Examples from '../Dns/Upstream/Examples';
|
import Examples from '../Dns/Upstream/Examples';
|
||||||
import { toggleAllServices } from '../../../helpers/helpers';
|
import { toggleAllServices } from '../../../helpers/helpers';
|
||||||
import {
|
import {
|
||||||
required,
|
|
||||||
clientId,
|
|
||||||
renderInputField,
|
renderInputField,
|
||||||
renderGroupField,
|
renderGroupField,
|
||||||
renderSelectField,
|
renderSelectField,
|
||||||
renderServiceField,
|
renderServiceField,
|
||||||
} from '../../../helpers/form';
|
} from '../../../helpers/form';
|
||||||
|
import { validateClientId, validateRequiredValue } from '../../../helpers/validators';
|
||||||
import { FORM_NAME, SERVICES } from '../../../helpers/constants';
|
import { FORM_NAME, SERVICES } from '../../../helpers/constants';
|
||||||
import './Service.css';
|
import './Service.css';
|
||||||
|
|
||||||
@ -48,12 +47,12 @@ const settingsCheckboxes = [
|
|||||||
const validate = (values) => {
|
const validate = (values) => {
|
||||||
const errors = {};
|
const errors = {};
|
||||||
const { name, ids } = values;
|
const { name, ids } = values;
|
||||||
errors.name = required(name);
|
errors.name = validateRequiredValue(name);
|
||||||
|
|
||||||
if (ids && ids.length) {
|
if (ids && ids.length) {
|
||||||
const idArrayErrors = [];
|
const idArrayErrors = [];
|
||||||
ids.forEach((id, idx) => {
|
ids.forEach((id, idx) => {
|
||||||
idArrayErrors[idx] = required(id) || clientId(id);
|
idArrayErrors[idx] = validateRequiredValue(id) || validateClientId(id);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (idArrayErrors.length) {
|
if (idArrayErrors.length) {
|
||||||
|
@ -4,11 +4,9 @@ import PropTypes from 'prop-types';
|
|||||||
import { Field, reduxForm, formValueSelector } from 'redux-form';
|
import { Field, reduxForm, formValueSelector } from 'redux-form';
|
||||||
import { Trans, withTranslation } from 'react-i18next';
|
import { Trans, withTranslation } from 'react-i18next';
|
||||||
import flow from 'lodash/flow';
|
import flow from 'lodash/flow';
|
||||||
|
import { renderInputField, toNumber } from '../../../helpers/form';
|
||||||
import {
|
|
||||||
renderInputField, required, ipv4, isPositive, toNumber,
|
|
||||||
} from '../../../helpers/form';
|
|
||||||
import { FORM_NAME } from '../../../helpers/constants';
|
import { FORM_NAME } from '../../../helpers/constants';
|
||||||
|
import { validateIpv4, validateIsPositiveValue, validateRequiredValue } from '../../../helpers/validators';
|
||||||
|
|
||||||
const renderInterfaces = ((interfaces) => (
|
const renderInterfaces = ((interfaces) => (
|
||||||
Object.keys(interfaces).map((item) => {
|
Object.keys(interfaces).map((item) => {
|
||||||
@ -96,7 +94,7 @@ let Form = (props) => {
|
|||||||
name="interface_name"
|
name="interface_name"
|
||||||
component="select"
|
component="select"
|
||||||
className="form-control custom-select"
|
className="form-control custom-select"
|
||||||
validate={[required]}
|
validate={[validateRequiredValue]}
|
||||||
>
|
>
|
||||||
<option value="" disabled={enabled}>
|
<option value="" disabled={enabled}>
|
||||||
{t('dhcp_interface_select')}
|
{t('dhcp_interface_select')}
|
||||||
@ -125,7 +123,7 @@ let Form = (props) => {
|
|||||||
type="text"
|
type="text"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('dhcp_form_gateway_input')}
|
placeholder={t('dhcp_form_gateway_input')}
|
||||||
validate={[ipv4, required]}
|
validate={[validateIpv4, validateRequiredValue]}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="form__group form__group--settings">
|
<div className="form__group form__group--settings">
|
||||||
@ -137,7 +135,7 @@ let Form = (props) => {
|
|||||||
type="text"
|
type="text"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('dhcp_form_subnet_input')}
|
placeholder={t('dhcp_form_subnet_input')}
|
||||||
validate={[ipv4, required]}
|
validate={[validateIpv4, validateRequiredValue]}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -155,7 +153,7 @@ let Form = (props) => {
|
|||||||
type="text"
|
type="text"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('dhcp_form_range_start')}
|
placeholder={t('dhcp_form_range_start')}
|
||||||
validate={[ipv4, required]}
|
validate={[validateIpv4, validateRequiredValue]}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="col">
|
<div className="col">
|
||||||
@ -166,7 +164,7 @@ let Form = (props) => {
|
|||||||
type="text"
|
type="text"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('dhcp_form_range_end')}
|
placeholder={t('dhcp_form_range_end')}
|
||||||
validate={[ipv4, required]}
|
validate={[validateIpv4, validateRequiredValue]}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -179,7 +177,7 @@ let Form = (props) => {
|
|||||||
type="number"
|
type="number"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('dhcp_form_lease_input')}
|
placeholder={t('dhcp_form_lease_input')}
|
||||||
validate={[required, isPositive]}
|
validate={[validateRequiredValue, validateIsPositiveValue]}
|
||||||
normalize={toNumber}
|
normalize={toNumber}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
@ -3,10 +3,8 @@ import PropTypes from 'prop-types';
|
|||||||
import { Field, reduxForm } from 'redux-form';
|
import { Field, reduxForm } from 'redux-form';
|
||||||
import { Trans, withTranslation } from 'react-i18next';
|
import { Trans, withTranslation } from 'react-i18next';
|
||||||
import flow from 'lodash/flow';
|
import flow from 'lodash/flow';
|
||||||
|
import { renderInputField } from '../../../../helpers/form';
|
||||||
import {
|
import { validateIpv4, validateMac, validateRequiredValue } from '../../../../helpers/validators';
|
||||||
renderInputField, ipv4, mac, required,
|
|
||||||
} from '../../../../helpers/form';
|
|
||||||
import { FORM_NAME } from '../../../../helpers/constants';
|
import { FORM_NAME } from '../../../../helpers/constants';
|
||||||
|
|
||||||
const Form = (props) => {
|
const Form = (props) => {
|
||||||
@ -31,7 +29,7 @@ const Form = (props) => {
|
|||||||
type="text"
|
type="text"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('form_enter_mac')}
|
placeholder={t('form_enter_mac')}
|
||||||
validate={[required, mac]}
|
validate={[validateRequiredValue, validateMac]}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="form__group">
|
<div className="form__group">
|
||||||
@ -42,7 +40,7 @@ const Form = (props) => {
|
|||||||
type="text"
|
type="text"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('form_enter_ip')}
|
placeholder={t('form_enter_ip')}
|
||||||
validate={[required, ipv4]}
|
validate={[validateRequiredValue, validateIpv4]}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="form__group">
|
<div className="form__group">
|
||||||
|
@ -3,31 +3,26 @@ import PropTypes from 'prop-types';
|
|||||||
import { Field, reduxForm } from 'redux-form';
|
import { Field, reduxForm } from 'redux-form';
|
||||||
import { Trans, useTranslation } from 'react-i18next';
|
import { Trans, useTranslation } from 'react-i18next';
|
||||||
import { shallowEqual, useSelector } from 'react-redux';
|
import { shallowEqual, useSelector } from 'react-redux';
|
||||||
import {
|
import { renderInputField, toNumber } from '../../../../helpers/form';
|
||||||
biggerOrEqualZero,
|
import { validateBiggerOrEqualZeroValue, getMaxValueValidator, validateRequiredValue } from '../../../../helpers/validators';
|
||||||
maxValue,
|
import { FORM_NAME, SECONDS_IN_HOUR } from '../../../../helpers/constants';
|
||||||
renderInputField,
|
|
||||||
required,
|
|
||||||
toNumber,
|
|
||||||
} from '../../../../helpers/form';
|
|
||||||
import { FORM_NAME } from '../../../../helpers/constants';
|
|
||||||
|
|
||||||
const maxValue3600 = maxValue(3600);
|
const validateMaxValue3600 = getMaxValueValidator(SECONDS_IN_HOUR);
|
||||||
|
|
||||||
const getInputFields = ({ required, maxValue3600 }) => [{
|
const getInputFields = ({ validateRequiredValue, validateMaxValue3600 }) => [{
|
||||||
name: 'cache_size',
|
name: 'cache_size',
|
||||||
title: 'cache_size',
|
title: 'cache_size',
|
||||||
description: 'cache_size_desc',
|
description: 'cache_size_desc',
|
||||||
placeholder: 'enter_cache_size',
|
placeholder: 'enter_cache_size',
|
||||||
validate: required,
|
validate: validateRequiredValue,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'cache_ttl_min',
|
name: 'cache_ttl_min',
|
||||||
title: 'cache_ttl_min_override',
|
title: 'cache_ttl_min_override',
|
||||||
description: 'cache_ttl_min_override_desc',
|
description: 'cache_ttl_min_override_desc',
|
||||||
placeholder: 'enter_cache_ttl_min_override',
|
placeholder: 'enter_cache_ttl_min_override',
|
||||||
max: 3600,
|
max: SECONDS_IN_HOUR,
|
||||||
validate: maxValue3600,
|
validate: validateMaxValue3600,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'cache_ttl_max',
|
name: 'cache_ttl_max',
|
||||||
@ -49,8 +44,8 @@ const Form = ({
|
|||||||
const minExceedsMax = cache_ttl_min > cache_ttl_max;
|
const minExceedsMax = cache_ttl_min > cache_ttl_max;
|
||||||
|
|
||||||
const INPUTS_FIELDS = getInputFields({
|
const INPUTS_FIELDS = getInputFields({
|
||||||
required,
|
validateRequiredValue,
|
||||||
maxValue3600,
|
validateMaxValue3600,
|
||||||
});
|
});
|
||||||
|
|
||||||
return <form onSubmit={handleSubmit}>
|
return <form onSubmit={handleSubmit}>
|
||||||
@ -71,7 +66,7 @@ const Form = ({
|
|||||||
disabled={processingSetConfig}
|
disabled={processingSetConfig}
|
||||||
normalize={toNumber}
|
normalize={toNumber}
|
||||||
className="form-control"
|
className="form-control"
|
||||||
validate={[biggerOrEqualZero].concat(validate || [])}
|
validate={[validateBiggerOrEqualZeroValue].concat(validate || [])}
|
||||||
min={0}
|
min={0}
|
||||||
max={max}
|
max={max}
|
||||||
/>
|
/>
|
||||||
|
@ -9,12 +9,11 @@ import {
|
|||||||
renderInputField,
|
renderInputField,
|
||||||
renderRadioField,
|
renderRadioField,
|
||||||
renderSelectField,
|
renderSelectField,
|
||||||
required,
|
|
||||||
ipv4,
|
|
||||||
ipv6,
|
|
||||||
biggerOrEqualZero,
|
|
||||||
toNumber,
|
toNumber,
|
||||||
} from '../../../../helpers/form';
|
} from '../../../../helpers/form';
|
||||||
|
import {
|
||||||
|
validateBiggerOrEqualZeroValue, validateIpv4, validateIpv6, validateRequiredValue,
|
||||||
|
} from '../../../../helpers/validators';
|
||||||
import { BLOCKING_MODES, FORM_NAME } from '../../../../helpers/constants';
|
import { BLOCKING_MODES, FORM_NAME } from '../../../../helpers/constants';
|
||||||
|
|
||||||
const checkboxes = [{
|
const checkboxes = [{
|
||||||
@ -36,12 +35,12 @@ const checkboxes = [{
|
|||||||
const customIps = [{
|
const customIps = [{
|
||||||
description: 'blocking_ipv4_desc',
|
description: 'blocking_ipv4_desc',
|
||||||
name: 'blocking_ipv4',
|
name: 'blocking_ipv4',
|
||||||
validateIp: ipv4,
|
validateIp: validateIpv4,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: 'blocking_ipv6_desc',
|
description: 'blocking_ipv6_desc',
|
||||||
name: 'blocking_ipv6',
|
name: 'blocking_ipv6',
|
||||||
validateIp: ipv6,
|
validateIp: validateIpv6,
|
||||||
}];
|
}];
|
||||||
|
|
||||||
const getFields = (processing, t) => Object.values(BLOCKING_MODES)
|
const getFields = (processing, t) => Object.values(BLOCKING_MODES)
|
||||||
@ -77,7 +76,7 @@ let Form = ({
|
|||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('form_enter_rate_limit')}
|
placeholder={t('form_enter_rate_limit')}
|
||||||
normalize={toNumber}
|
normalize={toNumber}
|
||||||
validate={[required, biggerOrEqualZero]}
|
validate={[validateRequiredValue, validateBiggerOrEqualZeroValue]}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -130,7 +129,7 @@ let Form = ({
|
|||||||
component={renderInputField}
|
component={renderInputField}
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('form_enter_ip')}
|
placeholder={t('form_enter_ip')}
|
||||||
validate={[validateIp, required]}
|
validate={[validateIp, validateRequiredValue]}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>)}
|
</div>)}
|
||||||
|
@ -10,10 +10,8 @@ import {
|
|||||||
renderSelectField,
|
renderSelectField,
|
||||||
renderRadioField,
|
renderRadioField,
|
||||||
toNumber,
|
toNumber,
|
||||||
port,
|
|
||||||
portTLS,
|
|
||||||
isSafePort,
|
|
||||||
} from '../../../helpers/form';
|
} from '../../../helpers/form';
|
||||||
|
import { validateIsSafePort, validatePort, validatePortTLS } from '../../../helpers/validators';
|
||||||
import i18n from '../../../i18n';
|
import i18n from '../../../i18n';
|
||||||
import KeyStatus from './KeyStatus';
|
import KeyStatus from './KeyStatus';
|
||||||
import CertificateStatus from './CertificateStatus';
|
import CertificateStatus from './CertificateStatus';
|
||||||
@ -46,7 +44,8 @@ const clearFields = (change, setTlsConfig, t) => {
|
|||||||
};
|
};
|
||||||
// eslint-disable-next-line no-alert
|
// eslint-disable-next-line no-alert
|
||||||
if (window.confirm(t('encryption_reset'))) {
|
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);
|
setTlsConfig(fields);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -158,7 +157,7 @@ let Form = (props) => {
|
|||||||
type="number"
|
type="number"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('encryption_https')}
|
placeholder={t('encryption_https')}
|
||||||
validate={[port, isSafePort]}
|
validate={[validatePort, validateIsSafePort]}
|
||||||
normalize={toNumber}
|
normalize={toNumber}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
disabled={!isEnabled}
|
disabled={!isEnabled}
|
||||||
@ -180,7 +179,7 @@ let Form = (props) => {
|
|||||||
type="number"
|
type="number"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder={t('encryption_dot')}
|
placeholder={t('encryption_dot')}
|
||||||
validate={[portTLS]}
|
validate={[validatePortTLS]}
|
||||||
normalize={toNumber}
|
normalize={toNumber}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
disabled={!isEnabled}
|
disabled={!isEnabled}
|
||||||
|
@ -505,3 +505,5 @@ export const FORM_NAME = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const smallScreenSize = 767;
|
export const smallScreenSize = 767;
|
||||||
|
|
||||||
|
export const SECONDS_IN_HOUR = 60 * 60;
|
||||||
|
@ -1,12 +1,7 @@
|
|||||||
import React, { Fragment } from 'react';
|
import React, { Fragment } from 'react';
|
||||||
import { Trans } from 'react-i18next';
|
|
||||||
import PropTypes from 'prop-types';
|
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 { createOnBlurHandler } from './helpers';
|
||||||
|
import { R_UNIX_ABSOLUTE_PATH, R_WIN_ABSOLUTE_PATH } from './constants';
|
||||||
|
|
||||||
export const renderField = (props, elementType) => {
|
export const renderField = (props, elementType) => {
|
||||||
const {
|
const {
|
||||||
@ -234,138 +229,15 @@ renderServiceField.propTypes = {
|
|||||||
}).isRequired,
|
}).isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Validation functions
|
/**
|
||||||
// If the value is valid, the validation function should return undefined.
|
* @param value {string}
|
||||||
// https://redux-form.com/6.6.3/examples/fieldlevelvalidation/
|
* @returns {*|number}
|
||||||
export const required = (value) => {
|
*/
|
||||||
const formattedValue = typeof value === 'string' ? value.trim() : value;
|
export const toNumber = (value) => value && parseInt(value, 10);
|
||||||
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 {boolean}
|
||||||
|
*/
|
||||||
export const isValidAbsolutePath = (value) => R_WIN_ABSOLUTE_PATH.test(value)
|
export const isValidAbsolutePath = (value) => R_WIN_ABSOLUTE_PATH.test(value)
|
||||||
|| R_UNIX_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);
|
|
||||||
|
209
client/src/helpers/validators.js
Normal file
209
client/src/helpers/validators.js
Normal 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;
|
||||||
|
};
|
@ -10,9 +10,8 @@ import AddressList from './AddressList';
|
|||||||
|
|
||||||
import { getInterfaceIp } from '../../helpers/helpers';
|
import { getInterfaceIp } from '../../helpers/helpers';
|
||||||
import { ALL_INTERFACES_IP, FORM_NAME } from '../../helpers/constants';
|
import { ALL_INTERFACES_IP, FORM_NAME } from '../../helpers/constants';
|
||||||
import {
|
import { renderInputField, toNumber } from '../../helpers/form';
|
||||||
renderInputField, required, validInstallPort, toNumber,
|
import { validateRequiredValue, validateInstallPort } from '../../helpers/validators';
|
||||||
} from '../../helpers/form';
|
|
||||||
|
|
||||||
const STATIC_STATUS = {
|
const STATIC_STATUS = {
|
||||||
ENABLED: 'yes',
|
ENABLED: 'yes',
|
||||||
@ -212,7 +211,7 @@ class Settings extends Component {
|
|||||||
type="number"
|
type="number"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder="80"
|
placeholder="80"
|
||||||
validate={[validInstallPort, required]}
|
validate={[validateInstallPort, validateRequiredValue]}
|
||||||
normalize={toNumber}
|
normalize={toNumber}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
/>
|
/>
|
||||||
@ -282,7 +281,7 @@ class Settings extends Component {
|
|||||||
type="number"
|
type="number"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
placeholder="80"
|
placeholder="80"
|
||||||
validate={[validInstallPort, required]}
|
validate={[validateInstallPort, validateRequiredValue]}
|
||||||
normalize={toNumber}
|
normalize={toNumber}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
/>
|
/>
|
||||||
|
@ -3,8 +3,8 @@ import PropTypes from 'prop-types';
|
|||||||
import { Field, reduxForm } from 'redux-form';
|
import { Field, reduxForm } from 'redux-form';
|
||||||
import { Trans, withTranslation } from 'react-i18next';
|
import { Trans, withTranslation } from 'react-i18next';
|
||||||
import flow from 'lodash/flow';
|
import flow from 'lodash/flow';
|
||||||
|
import { renderInputField } from '../../helpers/form';
|
||||||
import { renderInputField, required } from '../../helpers/form';
|
import { validateRequiredValue } from '../../helpers/validators';
|
||||||
import { FORM_NAME } from '../../helpers/constants';
|
import { FORM_NAME } from '../../helpers/constants';
|
||||||
|
|
||||||
const Form = (props) => {
|
const Form = (props) => {
|
||||||
@ -28,7 +28,7 @@ const Form = (props) => {
|
|||||||
placeholder={t('username_placeholder')}
|
placeholder={t('username_placeholder')}
|
||||||
autoComplete="username"
|
autoComplete="username"
|
||||||
disabled={processing}
|
disabled={processing}
|
||||||
validate={[required]}
|
validate={[validateRequiredValue]}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="form__group form__group--settings">
|
<div className="form__group form__group--settings">
|
||||||
@ -44,7 +44,7 @@ const Form = (props) => {
|
|||||||
placeholder={t('password_placeholder')}
|
placeholder={t('password_placeholder')}
|
||||||
autoComplete="current-password"
|
autoComplete="current-password"
|
||||||
disabled={processing}
|
disabled={processing}
|
||||||
validate={[required]}
|
validate={[validateRequiredValue]}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="form-footer">
|
<div className="form-footer">
|
||||||
|
Loading…
Reference in New Issue
Block a user