import { pipe } from 'fp-ts/lib/pipeable' ; import { left } from 'fp-ts/lib/Either' ; import { getEntryNestedMock } from './index.mock' ; import { entriesNested , EntryNested } from '.' ; import { foldLeftRight , getPaths } from '@kbn/securitysolution-io-ts-utils' ; import { getEntryMatchAnyMock } from '../entry_match_any/index.mock' ; import { getEntryExistsMock } from '../entries_exist/index.mock' ; describe ( 'entriesNested' , ( ) => { test ( 'it should validate a nested entry' , ( ) => { const payload = getEntryNestedMock ( ) ; const decoded = entriesNested . decode ( payload ) ; const message = pipe ( decoded , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( payload ) ; } ) ; test ( 'it should FAIL validation when "type" is not "nested"' , ( ) => { const payload = { ... getEntryNestedMock ( ) , type : 'match' , } ; const decoded = entriesNested . decode ( payload ) ; const message = pipe ( decoded , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'Invalid value "match" supplied to "type"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should FAIL validation when "field" is empty string' , ( ) => { const payload = { ... getEntryNestedMock ( ) , field : '' } ; const decoded = entriesNested . decode ( payload ) ; const message = pipe ( decoded , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'Invalid value "" supplied to "field"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should FAIL validation when "field" is not a string' , ( ) => { const payload = { ... getEntryNestedMock ( ) , field : 1 } ; const decoded = entriesNested . decode ( payload ) ; const message = pipe ( decoded , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'Invalid value "1" supplied to "field"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should FAIL validation when "entries" is not a an array' , ( ) => { const payload = { ... getEntryNestedMock ( ) , entries : 'im a string' } ; const decoded = entriesNested . decode ( payload ) ; const message = pipe ( decoded , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'Invalid value "im a string" supplied to "entries"' , ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should validate when "entries" contains an entry item that is type "match"' , ( ) => { const payload = { ... getEntryNestedMock ( ) , entries : [ getEntryMatchAnyMock ( ) ] } ; const decoded = entriesNested . decode ( payload ) ; const message = pipe ( decoded , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( { entries : [ { field : 'host.name' , operator : 'included' , type : 'match_any' , value : [ 'some host name' ] , } , ] , field : 'parent.field' , type : 'nested' , } ) ; } ) ; test ( 'it should validate when "entries" contains an entry item that is type "exists"' , ( ) => { const payload = { ... getEntryNestedMock ( ) , entries : [ getEntryExistsMock ( ) ] } ; const decoded = entriesNested . decode ( payload ) ; const message = pipe ( decoded , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( { entries : [ { field : 'host.name' , operator : 'included' , type : 'exists' , } , ] , field : 'parent.field' , type : 'nested' , } ) ; } ) ; test ( 'it should strip out extra keys' , ( ) => { const payload = getEntryNestedMock ( ) ; payload . extraKey = 'some extra key' ; const decoded = entriesNested . decode ( payload ) ; const message = pipe ( decoded , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( getEntryNestedMock ( ) ) ; } ) ; } ) ;