From bc552848baa9c98f665c694a0488d0061a8e6dc2 Mon Sep 17 00:00:00 2001 From: Joe Nelson Date: Sun, 22 Nov 2015 21:51:42 -0800 Subject: [PATCH] Prevent inserting CSV with varying row length --- src/PostgREST/RequestIntent.hs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/PostgREST/RequestIntent.hs b/src/PostgREST/RequestIntent.hs index b21f45870..69d62b696 100644 --- a/src/PostgREST/RequestIntent.hs +++ b/src/PostgREST/RequestIntent.hs @@ -99,7 +99,9 @@ userIntent schema req reqBody = (JSON.eitherDecode reqBody) Right TextCSV -> either (PayloadParseError . cs) - (PayloadJSON . csvToJson) + (\val -> case ensureUniform (csvToJson val) of + Nothing -> PayloadParseError "All lines must have same number of fields" + Just json -> PayloadJSON json) (CSV.decodeByName reqBody) Left accept -> PayloadParseError $ @@ -177,11 +179,11 @@ type CsvData = V.Vector (M.HashMap T.Text BL.ByteString) The reason for its odd signature is so that it can compose directly with CSV.decodeByName -} -csvToJson :: (CSV.Header, CsvData) -> UniformObjects +csvToJson :: (CSV.Header, CsvData) -> JSON.Array csvToJson (_, vals) = - UniformObjects $ V.map rowToJsonObj vals + V.map rowToJsonObj vals where - rowToJsonObj = + rowToJsonObj = JSON.Object . M.map (\str -> if str == "NULL" then JSON.Null