diff --git a/server/resources/nodeResource.ts b/server/resources/nodeResource.ts index 190f820..92d9d14 100644 --- a/server/resources/nodeResource.ts +++ b/server/resources/nodeResource.ts @@ -32,7 +32,7 @@ function getNormalizedNodeData(reqData: any): CreateOrUpdateNode { _.each(nodeFields, function (field) { let value = normalizeString(reqData[field]); if (field === 'mac') { - value = normalizeMac(value); + value = normalizeMac(value as MAC); } node[field] = value; }); diff --git a/server/utils/strings.ts b/server/utils/strings.ts index 78f126a..12ba84d 100644 --- a/server/utils/strings.ts +++ b/server/utils/strings.ts @@ -1,12 +1,13 @@ import _ from "lodash" +import {MAC} from "../types"; -export function normalizeString (str: string): string { +export function normalizeString(str: string): string { return _.isString(str) ? str.trim().replace(/\s+/g, ' ') : str; } -export function normalizeMac (mac: string): string { +export function normalizeMac(mac: MAC): MAC { // parts only contains values at odd indexes - const parts = mac.toUpperCase().replace(/:/g, '').split(/([A-F0-9]{2})/); + const parts = mac.toUpperCase().replace(/[-:]/g, '').split(/([A-F0-9]{2})/); const macParts = []; @@ -14,10 +15,10 @@ export function normalizeMac (mac: string): string { macParts.push(parts[i]); } - return macParts.join(':'); + return macParts.join(':') as MAC; } -export function parseInteger (str: string): number { +export function parseInteger(str: string): number { const parsed = _.parseInt(str, 10); if (parsed.toString() === str) { return parsed; diff --git a/server/validation/constraints.ts b/server/validation/constraints.ts index 44f896a..587faf3 100644 --- a/server/validation/constraints.ts +++ b/server/validation/constraints.ts @@ -40,7 +40,7 @@ const CONSTRAINTS = { }, coords: { type: 'string', - regex: /^(-?[0-9]{1,3}(\.[0-9]{1,15})? -?[0-9]{1,3}(\.[0-9]{1,15})?)$/, + regex: /^([a-f0-9]{12}|([a-f0-9]{2}:){5}[a-f0-9]{2}|([a-f0-9]{2}-){5}[a-f0-9]{2})$/i, optional: true }, monitoring: { diff --git a/shared/validation/constraints.js b/shared/validation/constraints.js index 9c4d31f..dc6fbd7 100644 --- a/shared/validation/constraints.js +++ b/shared/validation/constraints.js @@ -38,7 +38,7 @@ }, mac: { type: 'string', - regex: /^([a-f0-9]{12}|([a-f0-9]{2}:){5}[a-f0-9]{2})$/i, + regex: /^([a-f0-9]{12}|([a-f0-9]{2}:){5}[a-f0-9]{2}|([a-f0-9]{2}-){5}[a-f0-9]{2})$/i, optional: false }, coords: {