import { Param as ParamNS } from './ParamSignature' ; import ParamData = ParamNS . ParamData ; import { Enum } from './ServerStateEnum' ; import AbstractEnum = Enum . AbstractEnum ; import { Command as CommandNS } from './AbstractCommand' ; export namespace Validation { /**
     *
     */ export interface IValidator { resolved : boolean ; resolve ( data , key ? ) : ParamData ; } /**
     *
     */ export abstract class AbstractValidator implements IValidator { public resolved = false ; abstract resolve ( value , key ? ) ; } /**
     *
     */ export class StringValidator extends AbstractValidator { /**
         *
         */ constructor ( private lazy = true ) { super ( ) ; } /**
         *
         */ resolve ( data ) { let textstring = '' ; function checkTextstring ( rawClipNameString ) { if ( rawClipNameString === null ) { return '' ; } rawClipNameString = rawClipNameString . trim ( ) ; if ( rawClipNameString . length === 0 ) { return '' ; } return rawClipNameString ; } if ( Array . isArray ( data ) ) { let i = 0 ; if ( this . lazy ) { do { textstring = checkTextstring ( data [ i ] ) ; i ++ ; } while ( textstring . length === 0 ) ; } else { textstring = '' ; data . forEach ( i => { let o = checkTextstring ( i ) ; textstring += ( o ) ? o + ' ' : '' ; } ) ; } } else if ( typeof data === 'object' || typeof data === 'string' ) { textstring = data . toString ( ) ; } if ( ! checkTextstring ( textstring ) ) { return false ; } return textstring ; } } /***/ export class FilterValidator extends StringValidator { /**
         *
         */ resolve ( data ) { let clipName = '' ; if ( typeof data === 'object' || typeof data === 'string' ) { clipName = data !== null ? data . toString ( ) : '' ; } let quotedClipName = `"${ clipName }"` ; return { raw : clipName , payload : quotedClipName } ; } } /***/ export class URLValidator extends StringValidator { resolve ( data ) { let url = super . resolve ( data ) . toString ( ) ; let quotedUrl = `"${ url }"` ; return { raw : url , payload : quotedUrl } ; } } /***/ export class ChannelLayoutValidator extends StringValidator { } /**
     *
     */ export class ClipNameValidator extends AbstractValidator { /**
         *
         */ resolve ( data ) { let clipName = '' ; if ( typeof data === 'object' || typeof data === 'string' ) { clipName = data !== null ? data . toString ( ) : '' ; } if ( ! this . checkClipNameString ( clipName ) ) { return false ; } let quotedClipName = `"${ clipName }"` ; return { raw : clipName , payload : quotedClipName } ; } checkClipNameString ( rawClipNameString ) { if ( rawClipNameString === null ) { return '' ; } rawClipNameString = rawClipNameString . trim ( ) ; if ( rawClipNameString . length === 0 ) { return '' ; } return rawClipNameString ; } } /**
     *
     */ export class ClipNameEmptyStringValidator extends ClipNameValidator { resolve ( data ) { let clipName = '' ; if ( typeof data === 'object' || typeof data === 'string' ) { clipName = data !== null ? data . toString ( ) : '' ; } if ( ! this . checkClipNameString ( clipName ) && clipName !== '' ) { return false ; } let quotedClipName = `"${ clipName }"` ; return { raw : clipName , payload : quotedClipName } ; } } /**
     *
     */ export class TemplateNameValidator extends ClipNameValidator { } /**
     *
     */ export class DataNameValidator extends ClipNameValidator { } /**
     *
     */ export class EnumValidator extends AbstractValidator { /**
         *
         */ constructor ( private _enumClass ) { super ( ) ; } /**
         *
         */ resolve ( data ) { if ( data instanceof this . _enumClass ) { return data . value ; } else if ( typeof data === 'string' ) { let stringCast = data ; stringCast = stringCast . toUpperCase ( ) ; stringCast = stringCast . replace ( ' ' , '_' ) ; if ( this . _enumClass . hasOwnProperty ( stringCast ) ) { return ( this . _enumClass as any ) [ stringCast ] . value ; } } return false ; } } /**
     *
     */ export class ChannelFormatValidator extends AbstractValidator { /**
         *
         */ constructor ( ) { super ( ) ; } /**
         *
         */ resolve ( data ) { if ( data instanceof Enum . ChannelFormat ) { return data . value ; } else if ( typeof data === 'string' ) { let stringCast = data . toString ( ) ; stringCast = stringCast . toUpperCase ( ) ; stringCast = stringCast . replace ( ' ' , '_' ) ; if ( Enum . ChannelFormat . hasOwnProperty ( stringCast ) ) { return ( Enum . ChannelFormat as any ) [ stringCast ] . value ; } else if ( Enum . ChannelFormat . hasOwnProperty ( 'SD_' + stringCast ) ) { return ( Enum . ChannelFormat as any ) [ 'SD_' + stringCast ] . value ; } else if ( Enum . ChannelFormat . hasOwnProperty ( 'HD_' + stringCast ) ) { return ( Enum . ChannelFormat as any ) [ 'HD_' + stringCast ] . value ; } else if ( Enum . ChannelFormat . hasOwnProperty ( 'UHD_' + stringCast ) ) { return ( Enum . ChannelFormat as any ) [ 'UHD_' + stringCast ] . value ; } } return false ; } } /**
     *
     */ export class KeywordValidator extends AbstractValidator { private _keyword : string ; private _caseSensitive : boolean ; /**
         *
         */ constructor ( keyword , caseSensitive = false ) { super ( ) ; this . _keyword = keyword ; this . _caseSensitive = caseSensitive ; } /**
         *
         */ resolve ( data ) { let keywordCopy = this . _keyword ; if ( ! this . _caseSensitive ) { keywordCopy = keywordCopy . toLowerCase ( ) ; } if ( Array . isArray ( data ) ) { if ( ! this . _caseSensitive ) { data = data . map ( value => String ( value ) . toLowerCase ( ) ) ; } if ( ( data as Array < string > ) . indexOf ( keywordCopy ) > - 1 ) { return this . _keyword ; } } else if ( typeof data === 'object' && data !== null ) { let objectCast = data ; if ( ! this . _caseSensitive ) { for ( let key in objectCast ) { ( objectCast as any ) [ key ] = String ( ( objectCast as any ) [ key ] ) . toLowerCase ( ) ; } } if ( objectCast . hasOwnProperty ( keywordCopy ) ) { return this . _keyword ; } } else if ( typeof data === 'string' ) { if ( ! this . _caseSensitive ) { data = String ( data ) . toLowerCase ( ) ; } if ( data === keywordCopy ) { return this . _keyword ; } } return false ; } } /**
     *
     */ export class FrameValidator extends AbstractValidator { private _keyword : string ; /**
         *
         */ constructor ( keyword ) { super ( ) ; this . _keyword = keyword ; } /**
         *
         */ resolve ( data ) { if ( Array . isArray ( data ) ) { data = data . map ( element => String ( element ) . toLowerCase ( ) ) ; let index = ( data as Array < string > ) . indexOf ( this . _keyword . toLowerCase ( ) ) ; if ( index > - 1 ) { data = parseInt ( data [ index + 1 ] , 10 ) ; } } else if ( typeof data === 'object' && data !== null ) { let objectCast = data ; if ( objectCast . hasOwnProperty ( this . _keyword ) ) { data = objectCast [ this . _keyword ] as number ; } } else if ( typeof data === 'string' ) { data = Number ( data ) ; } if ( typeof data === 'number' ) { let numberCast = data ; if ( numberCast >= 0 ) { return numberCast ; } } return false ; } } /**
     *
     */ export class PositiveNumberValidatorBetween extends AbstractValidator { /**
         *
         */ constructor ( private _min = 0 , private _max = Number . POSITIVE_INFINITY ) { super ( ) ; } /**
         *
         */ resolve ( data ) { if ( typeof data === 'number' ) { let numberCast = Math . max ( Math . min ( data , this . _max ) , this . _min ) ; if ( numberCast >= 0 ) { return numberCast ; } } return false ; } } /**
     *
     */ export class PositiveNumberValidator extends PositiveNumberValidatorBetween { /**
         *
         */ constructor ( ) { super ( ) ; } } /**
     *
     */ export class PositiveNumberRoundValidatorBetween extends PositiveNumberValidatorBetween { /**
         *
         */ resolve ( data ) { return Number ( super . resolve ( data ) ) . toFixed ( ) ; } } /**
     *
     */ export class NumberValidatorBetween extends AbstractValidator { /**
         *
         */ constructor ( private _min = Number . NEGATIVE_INFINITY , private _max = Number . POSITIVE_INFINITY ) { super ( ) ; } /**
         *
         */ resolve ( data ) { if ( typeof data === 'number' ) { let numberCast = Math . max ( Math . min ( data , this . _max ) , this . _min ) ; return numberCast ; } return false ; } } /**
     *
     */ export class NumberValidator extends NumberValidatorBetween { /**
         *
         */ constructor ( ) { super ( ) ; } } /***/ export class DecklinkDeviceValidator extends PositiveNumberValidator { } /**
     *
     */ export class BooleanValidatorWithDefaults extends AbstractValidator { /**
         *
         */ constructor ( private _valueOnSuccess ? , private _valueOnFail ? ) { super ( ) ; } /**
         *
         */ resolve ( data , key ) { if ( Array . isArray ( data ) ) { data = data . map ( element => String ( element ) . toLowerCase ( ) ) ; let index = ( data as Array < string > ) . indexOf ( key . toLowerCase ( ) ) ; if ( index > - 1 ) { data = data [ index + 1 ] ; if ( data === undefined ) { data = data [ index ] ; } } else { data = false ; } } let isValid = false ; if ( typeof data === 'string' ) { if ( data === 'true' ) { isValid = true ; } else if ( data === '1' ) { isValid = true ; } else if ( data === key ) { isValid = true ; } } else { isValid = ( ! ! data . valueOf ( ) ) ; } if ( isValid ) { return ( this . _valueOnSuccess !== undefined ) ? this . _valueOnSuccess : isValid ; } else { return ( this . _valueOnFail !== undefined ) ? this . _valueOnFail : isValid ; } } } /**
     *
     */ export class BooleanValidator extends BooleanValidatorWithDefaults { /**
         *
         */ constructor ( ) { super ( ) ; } } /**
     *
     */ export class TemplateDataValidator extends AbstractValidator { /**
         *
         */ resolve ( data ) { let stringCast = data . toString ( ) ; if ( typeof data === 'object' ) { stringCast = JSON . stringify ( data ) ; } else { stringCast = stringCast . replace ( /\r|\n/g , charToEscape => { return charToEscape === '\r' ? '\\r' : '\\n' ; } ) ; } stringCast = stringCast . replace ( /([\\\"])/g , '\\$1' ) ; let quotedString = `"${ stringCast }"` ; return { raw : stringCast , payload : quotedString } ; } } export class TimecodeValidator extends StringValidator { } export class RouteValidator extends AbstractValidator { regex = new RegExp ( /(route:\/\/)\d+((-)\d+)?/g ) ; regex2 = new RegExp ( /\d+((-)\d+)?/g ) ; resolve ( data ) { if ( typeof data === 'string' ) { if ( this . regex . test ( data ) ) { return { raw : data , payload : data } ; } else if ( this . regex2 . test ( data ) ) { return { raw : data , payload : 'route://' + data } ; } else { return false ; } } else if ( typeof data === 'object' ) { let routeStr = 'route://' ; if ( data . channel ) { routeStr += data . channel ; if ( data . layer ) { routeStr += '-' + data . layer ; } return { raw : data . toString ( ) , payload : routeStr } ; } else { return false ; } } else { return false ; } } } export class RouteFramesDelayValidator extends PositiveNumberValidator { } export class RouteModeValidator extends StringValidator { } export class CommandValidator extends AbstractValidator { resolve ( command ) { if ( CommandNS . isIAMCPCommand ( command ) ) { command . validateParams ( ) ; let commandString = ( command . constructor as any ) . commandString + ( command . address ? ' ' + command . address : '' ) ; for ( let i in command . payload ) { let payload = command . payload [ i ] ; commandString += ( commandString . length > 0 ? ' ' : '' ) ; commandString += ( payload . key ? payload . key + ' ' : '' ) + payload . value ; } return commandString ; } else { throw new Error ( 'Argument 0 was not an amcp command.' ) ; } } } export class StingTransitionPropertiesValidator extends AbstractValidator { regex = new RegExp ( /\(((\w+=(((?:").*(?:"))+|(\d+)))( )?)*\)/g ) ; props : { maskFile ? : string ; delay ? : number ; overlayFile ? : string ; audioFadeStart ? : number ; audioFadeDuration ? : number ; } = { } ; resolve ( data ) { if ( ! data ) return false ; if ( typeof data === 'string' && this . regex . test ( data ) ) { return { raw : data , payload : data } ; } else if ( typeof data === 'object' ) { this . props = data ; let str = '(' ; if ( this . props . maskFile ) str += `MASK="${ this . props . maskFile }" ` ; if ( this . props . overlayFile ) str += `OVERLAY="${ this . props . overlayFile }" ` ; if ( this . props . delay ) str += `TRIGGER_POINT="${ this . props . delay }" ` ; if ( this . props . audioFadeStart ) str += `AUDIO_FADE_START="${ this . props . audioFadeStart }" ` ; if ( this . props . audioFadeDuration ) str += `AUDIO_FADE_DURATION="${ this . props . audioFadeDuration }" ` ; str = str . substr ( 0 , str . length - 1 ) + ')' ; return { raw : JSON . stringify ( data ) , payload : str } ; } else { return false ; } } } }