mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-06-19 08:18:07 +00:00
refactor: Create and use validation functions for input
This includes IP address, application name and dotlocal url parsing. Change-type: patch Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
@ -19,11 +19,10 @@ import { CommandDefinition } from 'capitano';
|
|||||||
import { stripIndent } from 'common-tags';
|
import { stripIndent } from 'common-tags';
|
||||||
|
|
||||||
import { registrySecretsHelp } from '../utils/messages';
|
import { registrySecretsHelp } from '../utils/messages';
|
||||||
|
import {
|
||||||
// An regex to detect an IP address, from https://www.regular-expressions.info/ip.html
|
validateApplicationName,
|
||||||
const IP_REGEX = new RegExp(
|
validateIPAddress,
|
||||||
/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/,
|
} from '../utils/validation';
|
||||||
);
|
|
||||||
|
|
||||||
enum BuildTarget {
|
enum BuildTarget {
|
||||||
Cloud,
|
Cloud,
|
||||||
@ -32,11 +31,11 @@ enum BuildTarget {
|
|||||||
|
|
||||||
function getBuildTarget(appOrDevice: string): BuildTarget | null {
|
function getBuildTarget(appOrDevice: string): BuildTarget | null {
|
||||||
// First try the application regex from the api
|
// First try the application regex from the api
|
||||||
if (/^[a-zA-Z0-9_-]+$/.test(appOrDevice)) {
|
if (validateApplicationName(appOrDevice)) {
|
||||||
return BuildTarget.Cloud;
|
return BuildTarget.Cloud;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IP_REGEX.test(appOrDevice)) {
|
if (validateIPAddress(appOrDevice)) {
|
||||||
return BuildTarget.Device;
|
return BuildTarget.Device;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,13 @@ limitations under the License.
|
|||||||
|
|
||||||
import validEmail = require('@resin.io/valid-email');
|
import validEmail = require('@resin.io/valid-email');
|
||||||
|
|
||||||
|
const APPNAME_REGEX = new RegExp(/^[a-zA-Z0-9_-]+$/);
|
||||||
|
// An regex to detect an IP address, from https://www.regular-expressions.info/ip.html
|
||||||
|
const IP_REGEX = new RegExp(
|
||||||
|
/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/,
|
||||||
|
);
|
||||||
|
const DOTLOCAL_REGEX = new RegExp(/^[a-zA-Z0-9-]+\.local$/);
|
||||||
|
|
||||||
export function validateEmail(input: string) {
|
export function validateEmail(input: string) {
|
||||||
if (!validEmail(input)) {
|
if (!validEmail(input)) {
|
||||||
return 'Email is not valid';
|
return 'Email is not valid';
|
||||||
@ -37,5 +44,13 @@ export function validateApplicationName(input: string) {
|
|||||||
return 'The application name should be at least 4 characters';
|
return 'The application name should be at least 4 characters';
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return APPNAME_REGEX.test(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateIPAddress(input: string): boolean {
|
||||||
|
return IP_REGEX.test(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateDotLocalUrl(input: string): boolean {
|
||||||
|
return DOTLOCAL_REGEX.test(input);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user