import shelljs from "shelljs" ; import { Config } from "../config" ; import { logger } from "../logger" ; import { build as buildError , getErrorMessage } from "./errorBuilder" ; import { errorStatusCode } from "./errorStatusCode" ; /**
 * Values to be validated
 */ export interface ValidationValue { value : undefined | null | string ; error : string ; } param val export const hasValue = ( val ) : val is string => { if ( typeof val !== "string" ) { return false ; } return val . trim ( ) !== "" ; } ; param val export const isIntegerString = ( val ) : val is string => { if ( ! hasValue ( val ) ) { return false ; } return /^[1-9]\d*$/ . test ( val ) ; } ; param val export const isPortNumberString = ( val ) : val is string => { if ( ! isIntegerString ( val ) ) { return false ; } const port = parseInt ( val , 10 ) ; return port > 0 && port <= 65535 ; } ; param val param err export const validateForNonEmptyValue = ( validValue ) : string => { return hasValue ( validValue . value ) ? "" : validValue . error ; } ; param executables export const validatePrereqs = ( executables , globalInit ) : boolean => { const config = Config ( ) ; config . infra = config . infra || { } ; config . infra . checks = config . infra . checks || { } ; for ( const i of executables ) { if ( ! shelljs . which ( i ) ) { config . infra . checks [ i ] = false ; if ( globalInit === true ) { logger . warn ( i + " not installed." ) ; } else { logger . error ( ":no_entry_sign: '" + i + "'" + " not installed" ) ; return false ; } } else { config . infra . checks [ i ] = true ; } } return true ; } ; param value export const validateOrgName = ( value ) : string | boolean => { if ( ! hasValue ( ( value || "" ) . trim ( ) ) ) { return getErrorMessage ( "validation-err-org-name-missing" ) ; } const pass = value . match ( /^[0-9a-zA-Z][^\s]*[0-9a-zA-Z]$/ ) ; if ( pass ) { return true ; } return getErrorMessage ( "validation-err-org-name" ) ; } ; export const validateOrgNameThrowable = ( value ) : void => { const err = validateOrgName ( value ) ; if ( typeof err == "string" ) { throw buildError ( errorStatusCode . VALIDATION_ERR , err ) ; } } ; export const isDashHex = ( value ) : boolean => { return ! ! value . match ( /^[a-f0-9-]+$/ ) ; } ; export const isAlphaNumeric = ( value ) : boolean => { return ! ! value . match ( /^[a-zA-Z0-9]+$/ ) ; } ; export const isDashAlphaNumeric = ( value ) : boolean => { return ! ! value . match ( /^[a-zA-Z0-9-]+$/ ) ; } ; param value export const validatePassword = ( value ) : string | boolean => { if ( ! hasValue ( value ) ) { return getErrorMessage ( "validation-err-password-missing" ) ; } if ( value . length < 8 ) { return getErrorMessage ( "validation-err-password-too-short" ) ; } return true ; } ; param value export const validateProjectName = ( value ) : string | boolean => { if ( ! hasValue ( value ) ) { return getErrorMessage ( "validation-err-project-name-missing" ) ; } if ( value . length > 64 ) { return getErrorMessage ( "validation-err-project-name-too-long" ) ; } if ( value . startsWith ( "_" ) ) { return getErrorMessage ( "validation-err-project-name-begin-underscore" ) ; } if ( value . startsWith ( "." ) || value . endsWith ( "." ) ) { return getErrorMessage ( "validation-err-project-name-period" ) ; } const invalidChars = [ "/" , ":" , "\\" , "~" , "&" , "%" , ";" , "@" , "'" , '"' , "?" , "<" , ">" , "|" , "#" , "$" , "*" , "}" , "{" , "," , "+" , "=" , "[" , "]" , ] ; if ( invalidChars . some ( ( x ) => value . indexOf ( x ) !== - 1 ) ) { return getErrorMessage ( "validation-err-project-name-special-char" ) ; } return true ; } ; export const validateProjectNameThrowable = ( value ) : void => { const err = validateProjectName ( value ) ; if ( typeof err == "string" ) { throw buildError ( errorStatusCode . VALIDATION_ERR , err ) ; } } ; param value export const validateAccessToken = ( value ) : string | boolean => { if ( ! hasValue ( value ) ) { return getErrorMessage ( "validation-err-personal-access-token-missing" ) ; } return true ; } ; export const validateAccessTokenThrowable = ( value ) : void => { const err = validateAccessToken ( value ) ; if ( typeof err == "string" ) { throw buildError ( errorStatusCode . VALIDATION_ERR , err ) ; } } ; export const validateServicePrincipal = ( value , missing , invalid ) : string | boolean => { if ( ! hasValue ( value ) ) { return getErrorMessage ( missing ) ; } if ( ! isDashHex ( value ) ) { return getErrorMessage ( invalid ) ; } return true ; } ; param value export const validateServicePrincipalId = ( value ) : string | boolean => { return validateServicePrincipal ( value , "validation-err-service-principal-id-missing" , "validation-err-service-principal-id-invalid" ) ; } ; param value export const validateServicePrincipalIdThrowable = ( value ) : void => { const msg = validateServicePrincipalId ( value ) ; if ( typeof msg === "string" ) { throw buildError ( errorStatusCode . VALIDATION_ERR , msg ) ; } } ; param value export const validateServicePrincipalPassword = ( value ) : string | boolean => { return validateServicePrincipal ( value , "validation-err-service-principal-pwd-missing" , "validation-err-service-principal-pwd-invalid" ) ; } ; param value export const validateServicePrincipalPasswordThrowable = ( value ) : void => { const msg = validateServicePrincipalPassword ( value ) ; if ( typeof msg === "string" ) { throw buildError ( errorStatusCode . VALIDATION_ERR , msg ) ; } } ; param value export const validateServicePrincipalTenantId = ( value ) : string | boolean => { return validateServicePrincipal ( value , "validation-err-service-principal-tenant-id-missing" , "validation-err-service-principal-tenant-id-invalid" ) ; } ; param value export const validateServicePrincipalTenantIdThrowable = ( value ) : void => { const msg = validateServicePrincipalTenantId ( value ) ; if ( typeof msg === "string" ) { throw buildError ( errorStatusCode . VALIDATION_ERR , msg ) ; } } ; param value export const validateSubscriptionId = ( value ) : string | boolean => { if ( ! hasValue ( value ) ) { return getErrorMessage ( "validation-err-subscription-id-missing" ) ; } if ( ! isDashHex ( value ) ) { return getErrorMessage ( "validation-err-subscription-id-invalid" ) ; } return true ; } ; param value export const validateSubscriptionIdThrowable = ( value ) : void => { const msg = validateSubscriptionId ( value ) ; if ( typeof msg === "string" ) { throw buildError ( errorStatusCode . VALIDATION_ERR , msg ) ; } } ; param value export const validateStorageAccountName = ( value ) : string | boolean => { if ( ! hasValue ( value ) ) { return getErrorMessage ( "validation-err-storage-account-name-missing" ) ; } if ( ! value . match ( /^[a-z0-9]+$/ ) ) { return getErrorMessage ( "validation-err-storage-account-name-invalid" ) ; } if ( value . length < 3 || value . length > 24 ) { return getErrorMessage ( "validation-err-storage-account-name-length" ) ; } return true ; } ; param value export const validateStorageAccountNameThrowable = ( value ) : void => { const msg = validateStorageAccountName ( value ) ; if ( typeof msg === "string" ) { throw buildError ( errorStatusCode . VALIDATION_ERR , msg ) ; } } ; param value export const validateStorageTableName = ( value ) : string | boolean => { if ( ! hasValue ( value ) ) { return getErrorMessage ( "validation-err-storage-table-name-missing" ) ; } if ( ! value . match ( /^[A-Za-z][A-Za-z0-9]*$/ ) ) { return getErrorMessage ( "validation-err-storage-table-name-invalid" ) ; } if ( value . length < 3 || value . length > 63 ) { return getErrorMessage ( "validation-err-storage-table-name-length" ) ; } return true ; } ; param value export const validateStorageTableNameThrowable = ( value ) : void => { const msg = validateStorageTableName ( value ) ; if ( typeof msg === "string" ) { throw buildError ( errorStatusCode . VALIDATION_ERR , msg ) ; } } ; param value export const validateStoragePartitionKey = ( value ) : string | boolean => { if ( ! hasValue ( value ) ) { return getErrorMessage ( "validation-err-storage-partition-key-missing" ) ; } if ( value . match ( /[/\\#?]/ ) ) { return getErrorMessage ( "validation-err-storage-partition-key-invalid" ) ; } return true ; } ; param value export const validateACRName = ( value ) : string | boolean => { if ( ! hasValue ( value ) ) { return getErrorMessage ( "validation-err-acr-missing" ) ; } if ( ! isAlphaNumeric ( value ) ) { return getErrorMessage ( "validation-err-acr-invalid" ) ; } if ( value . length < 5 || value . length > 50 ) { return getErrorMessage ( "validation-err-acr-length" ) ; } return true ; } ; export const validateStorageKeyVaultName = ( value ) : string | boolean => { if ( ! hasValue ( value ) ) { return true ; } if ( ! isDashAlphaNumeric ( value ) ) { return getErrorMessage ( "validation-err-storage-key-vault-invalid" ) ; } if ( ! value . match ( /^[a-zA-Z]/ ) ) { return getErrorMessage ( "validation-err-storage-key-vault-start-letter" ) ; } if ( ! value . match ( /[a-zA-Z0-9]$/ ) ) { return getErrorMessage ( "validation-err-storage-key-vault-end-char" ) ; } if ( value . indexOf ( "--" ) !== - 1 ) { return getErrorMessage ( "validation-err-storage-key-vault-hyphen" ) ; } if ( value . length < 3 || value . length > 24 ) { return getErrorMessage ( "validation-err-storage-key-vault-length" ) ; } return true ; } ; export const validateStorageAccessKey = ( value ) : string | boolean => { if ( ! hasValue ( value ) ) { return getErrorMessage ( "validation-err-storage-access-key-missing" ) ; } return true ; } ;