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

unjust function? #12

Open
jamesdbrock opened this issue May 28, 2022 · 2 comments
Open

unjust function? #12

jamesdbrock opened this issue May 28, 2022 · 2 comments

Comments

@jamesdbrock
Copy link

This package is really nice and it composes well with https://github.com/xc-jp/purescript-protobuf , because in protobuf messages are mostly records of Maybes, so this package could be useful for message construction.

I can also imagine a function unjustified which could be useful for message validation, and it would work by turning Justs into non-Maybes, or failing with a message about which fields were missing.

validated :: { name :: String, id :: Int }
validated = unjustified { name: Just "Mark", id: 10 }
-- Right { name: "Mark", id: 10 }
validated :: { name :: String, id :: Int }
validated = unjustified { id: 10 }
-- Left "Missing record field 'name'"

Anyway thanks for this package, I linked to it from the protobuf README https://github.com/xc-jp/purescript-protobuf#other-references and I'm thinking that maybe I should feature this package even more prominently in the protobuf docs.

@i-am-the-slime
Copy link
Owner

Thank you very much!
I've been thinking about the function you propose.
I'm curious as to what the use case for this would be, however.

It would mean you have a runtime type that you know is of the shape including the Maybes, but you don't know how many of the values are actual Justs. Don't you start out with a Foreign value anyway? Then you could just use something like yoga-json and parse as the strict type. That'd give you a NonEmptyList of errors, wouldn't it?

@jamesdbrock
Copy link
Author

jamesdbrock commented May 28, 2022

So here's my use case:

In this Writing programs with the generated code, we have received a Rectangle message with type

newtype Rectangle = Rectangle { width :: Maybe Number, height :: Maybe Number }

This is protobuf so all fields are optional, but the Rectangle message is only valid if it has values for width and height.

For this validation step, pattern matching on the Rectangle message type works well, so we could validate this way:

        case rectangle of
            Rectangle { width: Just width, height: Just height } ->
                pure $ Tuple width height
            _ -> fail "Missing required width or height"

Or we might want to use parseMaybe, one of the convenience parsing functions exported by Protobuf.Library, for more fine-grained validation:

        width <- parseMaybe "Missing required width" (unwrap rectangle).width
        height <- parseMaybe "Missing required height" (unwrap rectangle).height
        pure $ Tuple width height

If I had an unjustified function, then in the docs I could recommend something like this:

    {width, height} :: {width :: Number, height :: Number} <- parseEither $ unjustified (unwrap rectangle)

(The parseEither function comes from here.)

I guess if we did this the right way then it would produce one error for each Nothing, yeah. So maybe a NonEmptyList of errors.

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

2 participants