import { left } from 'fp-ts/lib/Either' ; import { pipe } from 'fp-ts/lib/pipeable' ; import { exactCheck , foldLeftRight , getPaths } from '@kbn/securitysolution-io-ts-utils' ; import { getPathListSchemaMock } from './index.mock' ; import { PatchListSchema , patchListSchema } from '.' ; describe ( 'patch_list_schema' , ( ) => { test ( 'it should validate a typical list item request' , ( ) => { const payload = getPathListSchemaMock ( ) ; const decoded = patchListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( payload ) ; } ) ; test ( 'it should NOT accept an undefined for "id"' , ( ) => { const payload = getPathListSchemaMock ( ) ; delete payload . id ; const decoded = patchListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'Invalid value "undefined" supplied to "id"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it should accept an undefined for "meta"' , ( ) => { const payload = getPathListSchemaMock ( ) ; delete payload . meta ; const decoded = patchListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( payload ) ; } ) ; test ( 'it should accept an undefined for "name"' , ( ) => { const payload = getPathListSchemaMock ( ) ; delete payload . name ; const decoded = patchListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( payload ) ; } ) ; test ( 'it should accept an undefined for "description"' , ( ) => { const payload = getPathListSchemaMock ( ) ; delete payload . description ; const decoded = patchListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( payload ) ; } ) ; test ( 'it should accept an undefined for "description", "meta", "name' , ( ) => { const payload = getPathListSchemaMock ( ) ; delete payload . description ; delete payload . name ; delete payload . meta ; const decoded = patchListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( payload ) ; } ) ; test ( 'it should accept an undefined for "description", "meta"' , ( ) => { const payload = getPathListSchemaMock ( ) ; delete payload . description ; delete payload . meta ; const decoded = patchListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( payload ) ; } ) ; test ( 'it should accept an undefined for "description", "name"' , ( ) => { const payload = getPathListSchemaMock ( ) ; delete payload . description ; delete payload . name ; const decoded = patchListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( payload ) ; } ) ; test ( 'it should accept an undefined for "meta", "name"' , ( ) => { const payload = getPathListSchemaMock ( ) ; delete payload . meta ; delete payload . name ; const decoded = patchListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ ] ) ; expect ( message . schema ) . toEqual ( payload ) ; } ) ; test ( 'it should not allow an extra key to be sent in' , ( ) => { const payload = getPathListSchemaMock ( ) ; payload . extraKey = 'some new value' ; const decoded = patchListSchema . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "extraKey"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; } ) ;