import { exactCheck , foldLeftRight , getPaths } from '@kbn/securitysolution-io-ts-utils' ; import { pipe } from 'fp-ts/lib/pipeable' ; import { left } from 'fp-ts/lib/Either' ; import { EsDataTypeGeoPoint , EsDataTypeGeoPointRange , EsDataTypeRange , EsDataTypeRangeTerm , EsDataTypeSingle , EsDataTypeUnion , esDataTypeGeoPoint , esDataTypeGeoPointRange , esDataTypeRange , esDataTypeRangeTerm , esDataTypeSingle , esDataTypeUnion , } from './schemas' ; describe ( 'esDataTypeUnion' , ( ) => { test ( 'it will work with a regular union' , ( ) => { const payload = { boolean : 'true' } ; const decoded = esDataTypeUnion . 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 will not work with a madeup value' , ( ) => { const payload = { boolean : 'true' , madeupValue : 'madeupValue' , } ; const decoded = esDataTypeUnion . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "madeupValue"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; } ) ; describe ( 'esDataTypeRange' , ( ) => { test ( 'it will work with a given gte, lte range' , ( ) => { const payload = { gte : '127.0.0.1' , lte : '127.0.0.1' } ; const decoded = esDataTypeRange . 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 will give an error if given an extra madeup value' , ( ) => { const payload = { gte : '127.0.0.1' , lte : '127.0.0.1' , madeupvalue : 'something' , } ; const decoded = esDataTypeRange . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "madeupvalue"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; } ) ; describe ( 'esDataTypeRangeTerm' , ( ) => { test ( 'it will work with a date_range' , ( ) => { const payload = { date_range : { gte : '2015' , lte : '2017' } } ; const decoded = esDataTypeRangeTerm . 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 will give an error if given an extra madeup value for date_range' , ( ) => { const payload = { date_range : { gte : '2015' , lte : '2017' } , madeupvalue : 'something' , } ; const decoded = esDataTypeRangeTerm . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "madeupvalue"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it will work with a double_range' , ( ) => { const payload = { double_range : { gte : '2015' , lte : '2017' } } ; const decoded = esDataTypeRangeTerm . 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 will give an error if given an extra madeup value for double_range' , ( ) => { const payload = { double_range : { gte : '2015' , lte : '2017' } , madeupvalue : 'something' , } ; const decoded = esDataTypeRangeTerm . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "madeupvalue"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it will work with a float_range' , ( ) => { const payload = { float_range : { gte : '2015' , lte : '2017' } } ; const decoded = esDataTypeRangeTerm . 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 will give an error if given an extra madeup value for float_range' , ( ) => { const payload = { float_range : { gte : '2015' , lte : '2017' } , madeupvalue : 'something' , } ; const decoded = esDataTypeRangeTerm . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "madeupvalue"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it will work with a integer_range' , ( ) => { const payload = { integer_range : { gte : '2015' , lte : '2017' } } ; const decoded = esDataTypeRangeTerm . 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 will give an error if given an extra madeup value for integer_range' , ( ) => { const payload = { integer_range : { gte : '2015' , lte : '2017' } , madeupvalue : 'something' , } ; const decoded = esDataTypeRangeTerm . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "madeupvalue"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it will work with a ip_range' , ( ) => { const payload = { ip_range : { gte : '127.0.0.1' , lte : '127.0.0.2' } } ; const decoded = esDataTypeRangeTerm . 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 will work with a ip_range as a CIDR' , ( ) => { const payload = { ip_range : '127.0.0.1/16' } ; const decoded = esDataTypeRangeTerm . 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 will give an error if given an extra madeup value for ip_range' , ( ) => { const payload = { ip_range : { gte : '127.0.0.1' , lte : '127.0.0.2' } , madeupvalue : 'something' , } ; const decoded = esDataTypeRangeTerm . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "madeupvalue"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; test ( 'it will work with a long_range' , ( ) => { const payload = { long_range : { gte : '2015' , lte : '2017' } } ; const decoded = esDataTypeRangeTerm . 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 will give an error if given an extra madeup value for long_range' , ( ) => { const payload = { long_range : { gte : '2015' , lte : '2017' } , madeupvalue : 'something' , } ; const decoded = esDataTypeRangeTerm . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "madeupvalue"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; } ) ; describe ( 'esDataTypeGeoPointRange' , ( ) => { test ( 'it will work with a given lat, lon range' , ( ) => { const payload = { lat : '20' , lon : '30' } ; const decoded = esDataTypeGeoPointRange . 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 will give an error if given an extra madeup value' , ( ) => { const payload = { lat : '20' , lon : '30' , madeupvalue : 'something' , } ; const decoded = esDataTypeGeoPointRange . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "madeupvalue"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; } ) ; describe ( 'esDataTypeGeoPoint' , ( ) => { test ( 'it will work with a given lat, lon range' , ( ) => { const payload = { geo_point : { lat : '127.0.0.1' , lon : '127.0.0.1' } } ; const decoded = esDataTypeGeoPoint . 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 will work with a WKT (Well known text)' , ( ) => { const payload = { geo_point : 'POINT (30 10)' } ; const decoded = esDataTypeGeoPoint . 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 will give an error if given an extra madeup value' , ( ) => { const payload = { geo_point : 'POINT (30 10)' , madeupvalue : 'something' , } ; const decoded = esDataTypeGeoPoint . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "madeupvalue"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; } ) ; describe ( 'esDataTypeSingle' , ( ) => { test ( 'it will work with single type' , ( ) => { const payload = { boolean : 'true' } ; const decoded = esDataTypeSingle . 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 will not work with a madeup value' , ( ) => { const payload = { boolean : 'true' , madeupValue : 'madeup' , } ; const decoded = esDataTypeSingle . decode ( payload ) ; const checked = exactCheck ( payload , decoded ) ; const message = pipe ( checked , foldLeftRight ) ; expect ( getPaths ( left ( message . errors ) ) ) . toEqual ( [ 'invalid keys "madeupValue"' ] ) ; expect ( message . schema ) . toEqual ( { } ) ; } ) ; } ) ;