import { AbstractControl , ValidatorFn } from '@angular/forms' ; export class DatasetValidator { private static readonly REQUIRED_FIELD : string = 'This field is required' ; private static readonly REQUIRED_FIELD_LABEL : string = 'This field is required at least 2 labels' ; private static readonly REQUIRED_FIELD_ENTITY : string = 'This field is required at least 1 entity' ; private static readonly FILE_FORMAT_NOT_SUPPORT : string = 'Selected file format is not supported' ; private static readonly FILE_SIZE_EXCEED_LIMIT : string = 'Selected file size exceeds the limit 500MB' ; private static readonly CREATE_FILE_SIZE_EXCEED_LIMIT : string = 'Selected file size exceeds the limit 100MB. Please use the My Datasets tab for larger datasets. Once completed, data will be available in this menu' ; private static readonly IMAGE_FILE_SIZE_EXCEED_LIMIT : string = 'Selected file size exceeds the limit 1MB' ; private static readonly FILE_DUPLICATED : string = 'Selected file has already exist in database' ; static modelName ( ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } if ( DatasetValidator . isEmpty ( control . value ) ) { return { msg : { value : DatasetValidator . REQUIRED_FIELD } } ; } const pattern = `^[a-zA-Z0-9 _\\-\\.]+$` ; const argRegEx = new RegExp ( pattern , 'g' ) ; if ( ! control . value . match ( argRegEx ) ) { return { msg : { value : 'Wrong format! Model name only allow letters, digits, dots, underscores and hyphen' , } , } ; } return null ; } ; } static datasetName ( ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } if ( DatasetValidator . isEmpty ( control . value ) ) { return { msg : { value : DatasetValidator . REQUIRED_FIELD } } ; } const pattern = `^[a-zA-Z0-9._-]+$` ; const argRegEx = new RegExp ( pattern , 'g' ) ; if ( ! control . value . match ( argRegEx ) ) { return { msg : { value : 'Wrong format! Dataset name only allow digits, letters, dots, underscore, and hyphen' , } , } ; } return null ; } ; } static validEmail ( ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } if ( DatasetValidator . isEmpty ( control . value ) ) { return { msg : { value : DatasetValidator . REQUIRED_FIELD } } ; } const pattern = `@vmware.com\\s*$` ; const argRegEx = new RegExp ( pattern , 'g' ) ; if ( ! control . value . match ( argRegEx ) ) { return { msg : { value : 'Wrong format! Email only accept vmware emailbox' } } ; } return null ; } ; } static validNormalEmail ( ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } if ( DatasetValidator . isEmpty ( control . value ) ) { return { msg : { value : DatasetValidator . REQUIRED_FIELD } } ; } const pattern = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/ ; if ( pattern . test ( control . value ) ) { return null ; } else { return { msg : { value : 'Wrong format! Only accept email address' } } ; } } ; } static validPassword ( ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } if ( DatasetValidator . isEmpty ( control . value ) ) { return { msg : { value : DatasetValidator . REQUIRED_FIELD } } ; } return null ; } ; } static validRow ( ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } if ( DatasetValidator . isEmpty ( control . value ) ) { return { msg : { value : 'Please set data source!' } } ; } if ( control . value == 0 ) { return { msg : { value : 'Please set data source!' } } ; } return null ; } ; } static localFile ( projectType , enableAWSS3 , duplicate ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } const inputFile = control . parent . get ( 'localFile' ) . value ; if ( inputFile ) { if ( ! DatasetValidator . validateFileExt ( inputFile , projectType ) ) { return { msg : { value : DatasetValidator . FILE_FORMAT_NOT_SUPPORT } } ; } if ( ! DatasetValidator . validateFileSize ( inputFile ) ) { return { msg : { value : DatasetValidator . FILE_SIZE_EXCEED_LIMIT } } ; } if ( ! enableAWSS3 ) { return duplicate ? { msg : { value : DatasetValidator . FILE_DUPLICATED } } : null ; } } return DatasetValidator . isEmpty ( control . value ) ? { msg : { value : DatasetValidator . REQUIRED_FIELD } } : null ; } ; } static imageFile ( ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } const inputFile = control . parent . get ( 'localFile' ) . value ; if ( inputFile ) { if ( ! DatasetValidator . validateImageFile ( inputFile ) ) { return { msg : { value : DatasetValidator . FILE_FORMAT_NOT_SUPPORT } } ; } if ( ! DatasetValidator . validateImageFileSize ( inputFile ) ) { return { msg : { value : DatasetValidator . IMAGE_FILE_SIZE_EXCEED_LIMIT } } ; } } return DatasetValidator . isEmpty ( control . value ) ? { msg : { value : DatasetValidator . REQUIRED_FIELD } } : null ; } ; } static required ( ) { return ( control ) : { [ key ] : any ; } => { if ( DatasetValidator . isEmpty ( control . value ) ) { return { msg : { value : DatasetValidator . REQUIRED_FIELD } } ; } return null ; } ; } static requiredTwo ( type ) { return ( control ) : { [ key ] : any ; } => { if ( type == 'ner' || type == 'image' || type == 'log' ) { if ( control . value . length < 1 ) { return { msg : { value : DatasetValidator . REQUIRED_FIELD_ENTITY } } ; } } else { if ( control . value . length < 2 ) { return { msg : { value : DatasetValidator . REQUIRED_FIELD_LABEL } } ; } } return null ; } ; } static sizeLimit ( ) { return ( control ) : { [ key ] : any ; } => { return DatasetValidator . isEmpty ( control . value ) ? { msg : { value : DatasetValidator . REQUIRED_FIELD } } : null ; } ; } static numberOnly ( ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } if ( DatasetValidator . isEmpty ( control . value ) ) { return { msg : { value : DatasetValidator . REQUIRED_FIELD } } ; } const pattern = `^[0-9]+$` ; const argRegEx = new RegExp ( pattern , 'g' ) ; if ( ! String ( control . value ) . match ( argRegEx ) ) { return { msg : { value : 'Wrong format! number only!' } } ; } return null ; } ; } static maxAnnotation ( ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } const pattern = `^[0-9]+$` ; const argRegEx = new RegExp ( pattern , 'g' ) ; if ( ! String ( control . value ) . match ( argRegEx ) || control . value <= 0 || control . value == null || control . value == undefined ) { return { msg : { value : 'Please enter a integer number greater than 0.' } } ; } return null ; } ; } static threshold ( ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } if ( DatasetValidator . isEmpty ( control . value ) ) { return { msg : { value : DatasetValidator . REQUIRED_FIELD } } ; } const pattern = `^[0-9]+$` ; const argRegEx = new RegExp ( pattern , 'g' ) ; if ( ! String ( control . value ) . match ( argRegEx ) ) { return { msg : { value : 'Wrong format! number only!' } } ; } if ( control . value < 50 ) { return { msg : { value : 'Should not less than the minimum threshold 50 !' } } ; } return null ; } ; } static minNumber ( ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } if ( control . value == null || control . value == undefined ) { return { msg : { value : DatasetValidator . REQUIRED_FIELD } } ; } const pattern = `^(\\-|\\+)?\\d+(\\.\\d+)?$` ; const argRegEx = new RegExp ( pattern , 'g' ) ; if ( ! String ( control . value ) . match ( argRegEx ) ) { return { msg : { value : 'Wrong format! number only' } } ; } return null ; } ; } static textOnly ( ) { return ( control ) : { [ key ] : any ; } => { if ( ! control . parent ) { return null ; } if ( DatasetValidator . isEmpty ( control . value ) ) { return { msg : { value : DatasetValidator . REQUIRED_FIELD } } ; } const pattern = `^[a-zA-Z]+$` ; const argRegEx = new RegExp ( pattern , 'g' ) ; if ( ! control . value . match ( argRegEx ) ) { return { msg : { value : 'Wrong format! text only' } } ; } return null ; } ; } private static validateFileExt ( inputFile , projectType ) { const ext = inputFile . name . split ( '.' ) . pop ( ) . toLowerCase ( ) ; if ( projectType || projectType == '' ) { if ( projectType == 'image' ) { if ( ext == 'zip' ) { return true ; } else { return false ; } } if ( projectType == 'ner' || projectType == 'text' || projectType == 'csv' || projectType == 'tabular' || projectType == '' ) { if ( ext == 'csv' ) { return true ; } else { return false ; } } if ( projectType == 'txt' || projectType == 'log' ) { if ( ext == 'zip' || ext == 'tgz' ) { return true ; } else { return false ; } } } else { return false ; } } private static validateImageFile ( inputFile ) { const ext = inputFile . name . split ( '.' ) . pop ( ) ; if ( ext == 'jpg' || ext == 'jpeg' || ext == 'png' || ext == 'gif' ) { return true ; } else { return false ; } } private static validateImageFileSize ( inputFile ) { const size = inputFile . size ; if ( inputFile . size < 100000000 ) { return true ; } else { return false ; } } private static validateFileSize ( inputFile ) { const size = inputFile . size ; return true ; } private static isEmpty ( str ) { if ( ! str ) { return true ; } else if ( str instanceof String && str . trim ( ) . length === 0 ) { return true ; } else if ( str instanceof Array && str . length === 0 ) { return true ; } else if ( str instanceof Object ) { return this . isEmptyObject ( str ) ; } return false ; } private static isEmptyObject ( obj ) { for ( const key in obj ) { if ( obj [ key ] !== null && obj [ key ] !== '' ) { return false ; } } return true ; } static isInvalidNumber ( input ) { return ( input === null || input === '' ) ; } static isNotIntegerNum ( input ) { return Math . round ( input ) !== input ; } static isRepeatArr ( arr ) { let hasArr = { } ; for ( var i in arr ) { if ( arr [ i ] && hasArr [ arr [ i ] ] ) { return true ; } hasArr [ arr [ i ] ] = true ; } return false ; } }