Added custom config validation errors

This commit is contained in:
VakarisZ 2020-07-27 15:31:25 +03:00
parent 026daba3e0
commit 0be9b19186
3 changed files with 27 additions and 9 deletions

View File

@ -0,0 +1,14 @@
import {IP, IP_RANGE} from "./ValidationFormats";
export default function transformErrors(errors) {
return errors.map(error => {
if (error.name === "type") {
error.message = "Field can't be empty."
} else if (error.name === "format" && error.params.format === IP_RANGE) {
error.message = "Invalid IP range, refer to description for valid examples."
} else if (error.name === "format" && error.params.format === IP) {
error.message = "Invalid IP."
}
return error;
});
}

View File

@ -2,12 +2,16 @@ const ipRegex = '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0
const cidrNotationRegex = '([0-9]|1[0-9]|2[0-9]|3[0-2])'
const hostnameRegex = '^([A-Za-z0-9]*[A-Za-z]+[A-Za-z0-9]*.?)*([A-Za-z0-9]*[A-Za-z]+[A-Za-z0-9]*)$'
export const IP_RANGE = 'ip-range';
export const IP = 'ip';
export const formValidationFormats = {
'ip-range': buildIpRangeRegex(),
'ip': buildIpRegex(),
[IP_RANGE]: buildIpRangeRegex(),
[IP]: buildIpRegex()
};
function buildIpRangeRegex(){
console.log(formValidationFormats);
return new RegExp([
'^'+ipRegex+'$|', // Single: IP
'^'+ipRegex+'-'+ipRegex+'$|', // IP range: IP-IP

View File

@ -10,6 +10,7 @@ import {faInfoCircle} from '@fortawesome/free-solid-svg-icons/faInfoCircle';
import {faCheck} from '@fortawesome/free-solid-svg-icons/faCheck';
import {faExclamationCircle} from '@fortawesome/free-solid-svg-icons/faExclamationCircle';
import {formValidationFormats} from '../configuration-components/ValidationFormats';
import transformErrors from '../configuration-components/ValidationErrorMessages';
import InternalConfig from '../configuration-components/InternalConfig';
const ATTACK_URL = '/api/attack';
@ -258,8 +259,6 @@ class ConfigurePageComponent extends AuthComponent {
this.setState({attackConfig: res.configuration});
this.setInitialAttackConfig(res.configuration);
})
};
removePBAfile(apiEndpoint, setFilenameFnc) {
@ -341,11 +340,12 @@ class ConfigurePageComponent extends AuthComponent {
setPbaFilenameLinux: this.setPbaFilenameLinux,
selectedSection: this.state.selectedSection
})
formProperties['formData'] = this.state.configuration[this.state.selectedSection]
formProperties['onChange'] = this.onChange
formProperties['customFormats'] = formValidationFormats
formProperties['className'] = 'config-form'
formProperties['liveValidate'] = true
formProperties['formData'] = this.state.configuration[this.state.selectedSection];
formProperties['onChange'] = this.onChange;
formProperties['customFormats'] = formValidationFormats;
formProperties['transformErrors'] = transformErrors;
formProperties['className'] = 'config-form';
formProperties['liveValidate'] = true;
if (this.state.selectedSection === 'internal') {
return (<InternalConfig {...formProperties}/>)