import { all , and , equals , isArray , isObject , isString , or } from '@typed/logic' ; import { hasOwnProperty } from '@typed/objects' ; import { JsonRpcNotification } from '../../model' ; const REQUIRED_KEYS = [ 'jsonrpc' , 'method' ] ; const OPTIONAL_KEYS = [ 'params' ] ; const VALIDATE_NOTIFICATION_KEYS = { jsonrpc : and ( isString , equals ( '2.0' ) ) , method : isString , params : or ( isArray , isObject ) , } ; export function isNotification ( x ) { if ( ! isObject ( x ) ) { return false ; } const hasPropertyAndIsValid = ( key , defaultValue ) => hasOwnProperty ( key , x ) ? VALIDATE_NOTIFICATION_KEYS [ key ] ( x [ key ] ) : defaultValue ; if ( ! all ( ( key ) => hasPropertyAndIsValid ( key , false ) , REQUIRED_KEYS ) ) { return false ; } return OPTIONAL_KEYS . reduce ( ( valid , key ) => ( valid ? hasPropertyAndIsValid ( key , true ) : false ) , true as boolean ) ; }