# Package: splodingsocks/validatable

**URL:** <https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37>\
**Category:** Show and Tell\
**Created:** [December 1, 2017, 2:52pm UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37 "2017-12-01T14:52:24Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![splodingsocks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/splodingsocks/32/26_2.png) [@splodingsocks](https://discourse.elm-lang.org/u/splodingsocks)\
**Post date:** [December 1, 2017, 2:52pm UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/1 "2017-12-01T14:52:24Z")

</div>

I found myself repeatedly making form fields on my model that needed validation before sending the content to the server. I used @rtfeldman’s elm-validate library, which I really like, bit I also found myself adding extra logic and model fields for when that data had validations that were delayed for some amount of time (turns out the user doesn’t like being told that their email isn’t valid while they’re still busy typing it in 😉 ) So I made a package based on elm-validate, and taking a similar mental approach to remote-data that represents data on the model as data that needs validation, and properly handles the different possible states, including delayed validations. In practice this has cleaned up my code quite a bit.

[http://package.elm-lang.org/packages/splodingsocks/validatable/latest](http://package.elm-lang.org/packages/splodingsocks/validatable/latest)

I feel like it could still use some real improvement. I’d love to receive feedback and ideas here, if you’d like to participate.

---

<div class="post-metadata">

**Author:** ![avh4](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/avh4/32/97_2.png) [@avh4](https://discourse.elm-lang.org/u/avh4)\
**Post date:** [December 3, 2017, 2:01am UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/2 "2017-12-03T02:01:24Z")

</div>

This seems like an interesting approach!

At work, we’ve been using a different approach, (I think @stoeffel was wanting to open-source it, but hasn’t had time yet) where we have a `Validation` module that is basically some helper functions that work with `data -> Result (List error) validatedData` (and help compose them in a way that’s similar to json-decode-pipeline. We only store the unvalidated data in the Model, and we define a separate ValidatedModel type that’s produced if validation is successful.

---

<div class="post-metadata">

**Author:** ![stoeffel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stoeffel/32/18_2.png) [@stoeffel](https://discourse.elm-lang.org/u/stoeffel)\
**Post date:** [December 3, 2017, 12:13pm UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/3 "2017-12-03T12:13:12Z")

</div>

Was actually planning to opensource it next week. hope I’ll get to it. Will let you all know. Would love to compare api and approach.

---

<div class="post-metadata">

**Author:** ![justinmimbs](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/justinmimbs/32/48_2.png) [@justinmimbs](https://discourse.elm-lang.org/u/justinmimbs)\
**Post date:** [December 3, 2017, 2:31pm UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/4 "2017-12-03T14:31:22Z")

</div>

> [@avh4](#):
>
> At work, we’ve been using a different approach, (I think @stoeffel was wanting to open-source it, but hasn’t had time yet) where we have a Validation module that is basically some helper functions that work with data -\> Result (List error) validatedData

Interesting! I’ve been using the same approach at work for validation, and it’s been really nice. I like the simplicity of only storing raw user input in the model, and validating as needed, with just a few helper functions.

For pipelining, I’ve an awkwardly named

```elm
applyOrAccumulate : Result x a -> Result (List x) (a -> b) -> Result (List x) b

```

function to use as you described.

```elm
formToData : form -> Result (List error) Data
formToData form =
    Ok Data
        |> applyOrAccumulate (form.email |> validateEmailString)
        |> applyOrAccumulate ...

```

Looking forward to seeing this kind of approach as a package.

---

<div class="post-metadata">

**Author:** ![ilias](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ilias/32/6_2.png) [@ilias](https://discourse.elm-lang.org/u/ilias)\
**Post date:** [December 3, 2017, 2:39pm UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/5 "2017-12-03T14:39:29Z")

</div>

I threw [something](https://github.com/zwilias/elm-transcoder) together after some slack conversations the other week, but hadn’t considered accumulating the errors along the way. That sounds really interesting, I love the idea. Looking forward to seeing what @stoeffel came up with! \<3

---

<div class="post-metadata">

**Author:** ![splodingsocks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/splodingsocks/32/26_2.png) [@splodingsocks](https://discourse.elm-lang.org/u/splodingsocks)\
**Post date:** [December 4, 2017, 5:41pm UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/6 "2017-12-04T17:41:04Z")

</div>

> At work, we’ve been using a different approach, (I think @stoeffel was wanting to open-source it, but hasn’t had time yet) where we have a Validation module that is basically some helper functions that work with data → Result (List error) validatedData

Yes, that sounds very interesting! Is there any provision for delayed validations?

---

<div class="post-metadata">

**Author:** ![avh4](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/avh4/32/97_2.png) [@avh4](https://discourse.elm-lang.org/u/avh4)\
**Post date:** [December 4, 2017, 7:26pm UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/7 "2017-12-04T19:26:07Z")

</div>

> [@splodingsocks](#):
>
> Yes, that sounds very interesting! Is there any provision for delayed validations?

I’m not sure if I understand the question, but all validations are delayed, and you only run them at the places that you use the validated values. (If you wanted them not delayed, you’d have to use `lazy` to cache the results of the validation.)

---

<div class="post-metadata">

**Author:** ![stoeffel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stoeffel/32/18_2.png) [@stoeffel](https://discourse.elm-lang.org/u/stoeffel)\
**Post date:** [December 7, 2017, 8:24am UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/8 "2017-12-07T08:24:37Z")

</div>

I just published `elm-verify`. I redesigned the api that we currently use at work, that’s why it took a bit longer. [http://package.elm-lang.org/packages/stoeffel/elm-verify/latest/Verify](http://package.elm-lang.org/packages/stoeffel/elm-verify/latest/Verify)  
I would love to hear your feedback.

---

<div class="post-metadata">

**Author:** ![RGBboy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rgbboy/32/214_2.png) [@RGBboy](https://discourse.elm-lang.org/u/RGBboy)\
**Post date:** [December 14, 2017, 10:48am UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/9 "2017-12-14T10:48:06Z")

</div>

One suggestion would be to allow for type transformations from input to output. I have recently been working on a project where we allow users to input monetary values via a text field. We want the users to be able to copy and past into the field with mistakes. Therefore allow any string as input. This is then converted to a number before being sent to the server. I often see that validation (a -\> Result x b) is the same as this transformation. If you were to enable these transformations the Validatable type would likely change to:

```
type Validatable error input output
    = Valid input output
    | Invalid input (List error)
    | Debouncing input { liveErrors : List error }
    | NotChecked input
```

---

<div class="post-metadata">

**Author:** ![joefiorini](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/joefiorini/32/665_2.png) [@joefiorini](https://discourse.elm-lang.org/u/joefiorini)\
**Post date:** [March 27, 2018, 1:28am UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/10 "2018-03-27T01:28:18Z")

</div>

Hey, looks cool. I recently started playing with some form ideas and used RemoteData as my inspiration. I also only fire validations on blur and submit, so will definitely check this out!

The one thing I’ve been thinking about was using Dict for form fields and a single Msg for updating fields. For larger apps/forms this seems like it could really help with productivity & less duplicate code (I’m not opposed to duplicate code in Elm typically, but if you are creating a lot of forms…)

I haven’t seen this approach taken yet in the libraries I’ve looked it. Is there a reason not to do it?

---

<div class="post-metadata">

**Author:** ![splodingsocks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/splodingsocks/32/26_2.png) [@splodingsocks](https://discourse.elm-lang.org/u/splodingsocks)\
**Post date:** [March 27, 2018, 2:28am UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/11 "2018-03-27T02:28:34Z")

</div>

Thanks for the note, @joefiorini! The dict is a fine idea if you’re going to be making lots of forms and need a very flexible pattern. [https://github.com/etaque/elm-form](https://github.com/etaque/elm-form) is a good library that takes this approach. But it just gives up some type-safety. So it’s a tradeoff. Great library, though!

---

<div class="post-metadata">

**Author:** ![brasilikum](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/brasilikum/32/524_2.png) [@brasilikum](https://discourse.elm-lang.org/u/brasilikum)\
**Post date:** [March 27, 2018, 3:40am UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/12 "2018-03-27T03:40:25Z")

</div>

@splodingsocks I really like your approach, but what would be a good way to ensure you don’t forget to add a new field to a button that needs all validadtions to pass?

---

<div class="post-metadata">

**Author:** ![splodingsocks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/splodingsocks/32/26_2.png) [@splodingsocks](https://discourse.elm-lang.org/u/splodingsocks)\
**Post date:** [March 28, 2018, 3:30pm UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/13 "2018-03-28T15:30:12Z")

</div>

That’s a great question! I haven’t been able to think of a good way. If you think of one, please let me know here!

---

<div class="post-metadata">

**Author:** ![brasilikum](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/brasilikum/32/524_2.png) [@brasilikum](https://discourse.elm-lang.org/u/brasilikum)\
**Post date:** [March 29, 2018, 5:03am UTC](https://discourse.elm-lang.org/t/package-splodingsocks-validatable/37/14 "2018-03-29T05:03:19Z")

</div>

I am thinking about it.  
Would you agree that this boils down to elm not allowing to map over dicts or compare any kind of type?

I am not saying it should necessarily, but my current understanding is that you can either put validatables in a list for reusability (loosing type safety for access) or in a type alias (not being able to map over them).

Am I right or am I missing something?
