# Pattern for literals that need validation

**URL:** <https://discourse.elm-lang.org/t/pattern-for-literals-that-need-validation/6681>\
**Category:** Learn\
**Created:** [December 30, 2020, 4:58am UTC](https://discourse.elm-lang.org/t/pattern-for-literals-that-need-validation/6681 "2020-12-30T04:58:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_Hunt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jeremy_hunt/32/3636_2.png) [@Jeremy\_Hunt](https://discourse.elm-lang.org/u/Jeremy_Hunt)\
**Post date:** [December 30, 2020, 4:58am UTC](https://discourse.elm-lang.org/t/pattern-for-literals-that-need-validation/6681/1 "2020-12-30T04:58:35Z")

</div>

I have an opaque type `Nineagram.NineagramPuzzle`, and to get one of these values I have to call `Nineagram.fromString`, and the string has some conditions, so it returns a `Result Nineagram.CreationProblem NineagramPuzzle`.

In my Main.elm view if the user hasn’t entered a puzzle to solve yet (`model.puzzle == Nothing`), I want to just render the usual puzzle view with some default puzzle serving as a placeholder, and so I really just wanted a `NineagramPuzzle` literal in `Main.elm` for that purpose, but of course I can’t construct that value from opaque type.

The solution I came up with was a pattern where I construct the literals on init and then return a model representing a failure if the literals are not valid, otherwise it returns a model that has the desired inital state plus the literals. Then I updated view to either show an error on the failure state, or call a separate view which accepts the valid literals as an additional argument: `viewState : Constants -> State -> Html Msg`

Here’s the diff for my app: [https://github.com/bukkfrig/nineagram-solver/commit/eae9d2da58630991b1248549363acd367b54c699](https://github.com/bukkfrig/nineagram-solver/commit/eae9d2da58630991b1248549363acd367b54c699)

If I had tests set up, I could just add a test that `init ()` produces the good model and not the bad one to validate my literals.

This is probably overengineering for this simple program, but is this approach sensible for larger programs that need literals that can’t be simply type checked? Have you used a similar approach before?

---

<div class="post-metadata">

**Author:** ![jfmengels](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jfmengels/32/3124_2.png) [@jfmengels](https://discourse.elm-lang.org/u/jfmengels)\
**Post date:** [December 30, 2020, 7:38am UTC](https://discourse.elm-lang.org/t/pattern-for-literals-that-need-validation/6681/2 "2020-12-30T07:38:28Z")

</div>

I think the approach is valid and makes sense 👍

When dealing with literals, I found (what I call) the [Safe Unsafe Pattern](https://jfmengels.net/safe-unsafe-operations-in-elm/) with `elm-review` to be quite effective. It’s very likely overkill for your situation because you only have a single location where this happens. And especially so since you already have a test giving you all the guarantees you’re looking for.

For this situation, I think your current solution is 👌 though.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Hunt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jeremy_hunt/32/3636_2.png) [@Jeremy\_Hunt](https://discourse.elm-lang.org/u/Jeremy_Hunt)\
**Post date:** [December 30, 2020, 8:40am UTC](https://discourse.elm-lang.org/t/pattern-for-literals-that-need-validation/6681/3 "2020-12-30T08:40:08Z")

</div>

Oh, I saw from your link, you can just define it recursively…

```auto
defaultPuzzle : () -> NineagramPuzzle
defaultPuzzle _ = 
    case Nineagram.fromString "GRNAMNIEA" of
        Ok puzzle ->
            puzzle
        
        Err _ ->
            defaultPuzzle ()

```

As long as I have a test that evaluates it, and as long as I’m OK with the testrunner crashing when I break it, perhaps that could be the simplest way?

---

<div class="post-metadata">

**Author:** ![jfmengels](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jfmengels/32/3124_2.png) [@jfmengels](https://discourse.elm-lang.org/u/jfmengels)\
**Post date:** [December 30, 2020, 9:37am UTC](https://discourse.elm-lang.org/t/pattern-for-literals-that-need-validation/6681/4 "2020-12-30T09:37:08Z")

</div>

That is not the solution I meant, but yeah that also works 😄  
Without a test that seems very dangerous, but with one I guess it’s okay, as long as you (and your teammates) are well aware how it may break one day.

---

<div class="post-metadata">

**Author:** ![lydell](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lydell/32/178_2.png) [@lydell](https://discourse.elm-lang.org/u/lydell)\
**Post date:** [December 30, 2020, 1:24pm UTC](https://discourse.elm-lang.org/t/pattern-for-literals-that-need-validation/6681/5 "2020-12-30T13:24:29Z")

</div>

I think the approach you had before with `Nineagram.defaultPuzzle` was fine! Simple and safe. (Maybe it should be called `Nineagram.placeholderPuzzle`, though.)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [January 9, 2021, 1:24pm UTC](https://discourse.elm-lang.org/t/pattern-for-literals-that-need-validation/6681/6 "2021-01-09T13:24:30Z")

</div>

Closed on January 9, 2021, 1:24pm UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
