import { textService } from '../text' ; import { Repository , RepositoryType , FSRepository , ReadonlyRepository , S3Repository , GCSRepository , HDFSRepository , EmptyRepository , } from '../../../../common/types' ; import { REPOSITORY_TYPES } from '../../../../common/constants' ; export interface RepositoryValidation { isValid : boolean ; errors : { name ? : string [ ] ; type ? : string [ ] ; settings ? : RepositorySettingsValidation ; } ; } export interface RepositorySettingsValidation { [ key ] : string [ ] ; } export const INVALID_NAME_CHARS = [ '"' , '*' , '\\' , '<' , '|' , ',' , '>' , '/' , '?' , '#' ] ; const isStringEmpty = ( str ) : boolean => { return str ? ! Boolean ( str . trim ( ) ) : true ; } ; const doesStringContainChar = ( string , char ) => { const chars = Array . isArray ( char ) ? char : [ char ] ; const total = chars . length ; let containsChar = false ; let charFound = null ; for ( let i = 0 ; i < total ; i ++ ) { if ( string . includes ( chars [ i ] ) ) { containsChar = true ; charFound = chars [ i ] ; break ; } } return { containsChar , charFound } ; } ; export const validateRepository = ( repository , validateSettings = true ) : RepositoryValidation => { const { name , type , settings } = repository ; const { i18n } = textService ; const validation = { isValid : true , errors : { } , } ; if ( validateSettings ) { const settingsValidation = validateRepositorySettings ( type , settings ) ; if ( Object . keys ( settingsValidation ) . length ) { validation . errors . settings = settingsValidation ; } } if ( isStringEmpty ( name ) ) { validation . errors . name = [ i18n . translate ( 'xpack.snapshotRestore.repositoryValidation.nameRequired' , { defaultMessage : 'Repository name is required.' , } ) , ] ; } if ( name . includes ( ' ' ) ) { validation . errors . name = [ i18n . translate ( 'xpack.snapshotRestore.repositoryValidation.nameValidation.errorSpace' , { defaultMessage : 'Spaces are not allowed in the name.' , } ) , ] ; } const nameCharValidation = doesStringContainChar ( name , INVALID_NAME_CHARS ) ; if ( nameCharValidation . containsChar ) { validation . errors . name = [ i18n . translate ( 'xpack.snapshotRestore.repositoryValidation.nameValidation.invalidCharacter' , { defaultMessage : 'Character "{char}" is not allowed in the name.' , values : { char : nameCharValidation . charFound } , } ) , ] ; } if ( isStringEmpty ( type ) || ( type === REPOSITORY_TYPES . source && isStringEmpty ( settings . delegateType ) ) ) { validation . errors . type = [ i18n . translate ( 'xpack.snapshotRestore.repositoryValidation.delegateTypeRequired' , { defaultMessage : 'Type is required.' , } ) , ] ; } if ( Object . keys ( validation . errors ) . length ) { validation . isValid = false ; } return validation ; } ; const validateRepositorySettings = ( type , settings ) : RepositorySettingsValidation => { switch ( type ) { case REPOSITORY_TYPES . fs : return validateFSRepositorySettings ( settings ) ; case REPOSITORY_TYPES . url : return validateReadonlyRepositorySettings ( settings ) ; case REPOSITORY_TYPES . source : return validateRepositorySettings ( settings . delegateType , settings ) ; case REPOSITORY_TYPES . s3 : return validateS3RepositorySettings ( settings ) ; case REPOSITORY_TYPES . gcs : return validateGCSRepositorySettings ( settings ) ; case REPOSITORY_TYPES . hdfs : return validateHDFSRepositorySettings ( settings ) ; default : return { } ; } } ; const validateFSRepositorySettings = ( settings ) : RepositorySettingsValidation => { const i18n = textService . i18n ; const validation = { } ; const { location } = settings ; if ( isStringEmpty ( location ) ) { validation . location = [ i18n . translate ( 'xpack.snapshotRestore.repositoryValidation.locationRequired' , { defaultMessage : 'Location is required.' , } ) , ] ; } return validation ; } ; const validateReadonlyRepositorySettings = ( settings ) : RepositorySettingsValidation => { const i18n = textService . i18n ; const validation = { } ; const { url } = settings ; if ( isStringEmpty ( url ) ) { validation . url = [ i18n . translate ( 'xpack.snapshotRestore.repositoryValidation.urlRequired' , { defaultMessage : 'URL is required.' , } ) , ] ; } return validation ; } ; const validateS3RepositorySettings = ( settings ) : RepositorySettingsValidation => { const i18n = textService . i18n ; const validation = { } ; const { bucket } = settings ; if ( isStringEmpty ( bucket ) ) { validation . bucket = [ i18n . translate ( 'xpack.snapshotRestore.repositoryValidation.bucketRequired' , { defaultMessage : 'Bucket is required.' , } ) , ] ; } return validation ; } ; const validateGCSRepositorySettings = ( settings ) : RepositorySettingsValidation => { const i18n = textService . i18n ; const validation = { } ; const { bucket } = settings ; if ( isStringEmpty ( bucket ) ) { validation . bucket = [ i18n . translate ( 'xpack.snapshotRestore.repositoryValidation.bucketRequired' , { defaultMessage : 'Bucket is required.' , } ) , ] ; } return validation ; } ; const validateHDFSRepositorySettings = ( settings ) : RepositorySettingsValidation => { const i18n = textService . i18n ; const validation = { } ; const { uri , path } = settings ; if ( isStringEmpty ( uri ) ) { validation . uri = [ i18n . translate ( 'xpack.snapshotRestore.repositoryValidation.uriRequired' , { defaultMessage : 'URI is required.' , } ) , ] ; } if ( isStringEmpty ( path ) ) { validation . path = [ i18n . translate ( 'xpack.snapshotRestore.repositoryValidation.pathRequired' , { defaultMessage : 'Path is required.' , } ) , ] ; } return validation ; } ;