Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error or warn on unknown fields #37

Closed
soenkehahn opened this issue Apr 25, 2023 · 5 comments
Closed

Error or warn on unknown fields #37

soenkehahn opened this issue Apr 25, 2023 · 5 comments

Comments

@soenkehahn
Copy link

Is it possible to use autodocodec to parse json or yaml while either failing or warning on unknown fields? aeson provides rejectUnknownFields, but I couldn't find a way to plug in Options into autodocodec.

@NorfairKing
Copy link
Owner

@soenkehahn You'd have to write your own decoder, but I think it's possible because we know all the fields statically.

@soenkehahn
Copy link
Author

Thanks for the reply! Are you saying it's possible for me to implement this without modifying autodocodec? If yes, I'm not sure what you mean by "write your own decoder". Codecs seem to encompass both encoders and decoders, and I'm not sure how I would go about writing my own decoder while using a provided decoder, if that makes sense. Any pointers appreciated!

@NorfairKing
Copy link
Owner

NorfairKing commented Apr 28, 2023

@soenkehahn Sorry I've not been very clear. It's easy for me to forget context that I only have because I wrote this code.

A codec is just a value that represents an idea.
This function does the decoding:

-- | Parse via a general codec.
--
-- You probably won't need this. See 'eitherDecodeViaCodec', 'parseJSONViaCodec' and 'parseJSONVia' instead.
parseJSONContextVia :: Codec context void a -> context -> JSON.Parser a
parseJSONContextVia codec_ context_ =
modifyFailure (\s -> if '\n' `elem` s then "\n" ++ s else s) $
go context_ codec_

You could write your own version to warn or reject on unknown fields.

@NorfairKing NorfairKing closed this as not planned Won't fix, can't repro, duplicate, stale May 5, 2023
@Icelandjack
Copy link

Is it possible to add support for this (decoder?) to the library so that users can configure this behaviour. I know you have already closed this but would you accept a pull request?

@NorfairKing
Copy link
Owner

@Icelandjack I think my "closed" communication was not clear. I just meant that I haven't planned to add it myself. A PR would be welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants