Skip to content

Commit 068acba

Browse files
committed
UI: Add validator for CIDR being passed
1 parent f2d6356 commit 068acba

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

ui/src/utils/util.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,25 @@ export function toCsv ({ keys = null, data = null, columnDelimiter = ',', lineDe
102102

103103
return result
104104
}
105+
106+
export function isValidIPv4Cidr (rule, value) {
107+
console.log('here')
108+
return new Promise((resolve, reject) => {
109+
if (!value) {
110+
reject(new Error('Required input'))
111+
return
112+
}
113+
const cidrRegex = /^(\d{1,3}\.){3}\d{1,3}\/([0-9]|[1-2][0-9]|3[0-2])$/
114+
if (!cidrRegex.test(value)) {
115+
reject(new Error('Invalid CIDR format'))
116+
return
117+
}
118+
const ip = value.split('/')[0]
119+
const octets = ip.split('.').map(Number)
120+
if (octets.some(octet => octet < 0 || octet > 255)) {
121+
reject(new Error('Invalid CIDR format'))
122+
return
123+
}
124+
resolve()
125+
})
126+
}

ui/src/views/network/CreateVpc.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
import { ref, reactive, toRaw } from 'vue'
221221
import { getAPI, postAPI } from '@/api'
222222
import { isAdmin, isAdminOrDomainAdmin } from '@/role'
223+
import { isValidIPv4Cidr } from '@/utils/util.js'
223224
import ResourceIcon from '@/components/view/ResourceIcon'
224225
import TooltipLabel from '@/components/widgets/TooltipLabel'
225226
import OwnershipSelection from '@/views/compute/wizard/OwnershipSelection.vue'
@@ -291,6 +292,7 @@ export default {
291292
this.rules = reactive({
292293
name: [{ required: true, message: this.$t('message.error.required.input') }],
293294
zoneid: [{ required: true, message: this.$t('label.required') }],
295+
cidr: [{ validator: isValidIPv4Cidr }],
294296
vpcofferingid: [{ required: true, message: this.$t('label.required') }]
295297
})
296298
},
@@ -417,7 +419,7 @@ export default {
417419
},
418420
updateCidrRule () {
419421
if (!this.selectedVpcOfferingHavingRoutedNetworkMode) {
420-
this.rules.cidr = [{ required: true, message: this.$t('message.error.required.input') }]
422+
this.rules.cidr = [{ validator: isValidIPv4Cidr }]
421423
} else {
422424
delete this.rules.cidr
423425
}

0 commit comments

Comments
 (0)