BrianHicks / elm-csv

Decode CSV in the most boring way possible.

Home Page:https://package.elm-lang.org/packages/BrianHicks/elm-csv/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Function to parse only at runtime specified fields

ad-si opened this issue · comments

I'm looking for a easy way to parse just the at runtime provided fields. So something like:

parseFields :
    { fieldSeparator : Char }
    -> List String
    -> String
    -> Result Problem (List (List String))
    
parseFields ',' ["id", "name", "color"] "id,name,color\n1,bike shed,green\n"

If the specified fields are not available it should it result in an error (maybe there should also be a flag wether to allow additional fields in the CSV).

Maybe this is already possible with decodeCsv, but if so I'm not sure how, since into needs a function with a pre defined arity.

Can you say more about what you're trying to do? https://package.elm-lang.org/packages/BrianHicks/elm-csv/latest/Csv-Parser#parse may suit you but I'd like to know more anyway so we can keep you in type-safety land.

I'm building a website where a user can specify which fields a CSV should have, and then validate CSVs against that schema. I.e. the available fields must be specified at runtime.

Ah, I see! When you say a schema, are you talking about the types of data in the columns or only their names? I can't think of a really easy way for you to get this data out of the decoder package, but you could parse it and then look at the first row to get the names.

but you could parse it and then look at the first row to get the names

That's what I'm doing now, but it's a shame that I can't use any of elm-csv's helper functions then. I only get a List (List String) and have to do the rest myself. 😅 A parseFields as described here, would make things quite easier.

I mean, I've already written in now: https://csvcheck.surge.sh
…but maybe for the next poor soul ;-)