Text validation


Is there a way to check the format of a text field on a record? I am trying to set up an example that configures Java microservices, and would like to sanity check some of the values (i.e. that a bit of text matches a URL pattern and seems to generally match the conventions in place; that sort of thing).

The manual says that “The only text manipulation allowed is concatenation. Other than that, Text values are opaque and there is no way to introspect them. For example, you cannot compare Text values for equality, nor can you compute their length.” Is there another approach that would work well?


There is not currently a way to do this. Text values are a black box as far as the language is concerned, so you cannot parse them or match them against, say, a regex

Generally, the way I’d recommend approaching this is to actually start from a more strongly-typed representation and then “render” it to the final Text representation. For example, suppose that you have a Text field named example that can only be one of three values: foo, bar or baz. Then you could model things like this:

let StrongConfig = { example : < foo | bar | baz > }

let WeakConfig = { example : Text }

let render
    : StrongConfig → WeakConfig
    = λ(config : StrongConfig) →
        { example =
            merge { foo = "foo", bar = "bar", baz = "baz" } config.example

in  { StrongConfig, WeakConfig, render }


That makes sense and for most of the Dhall program, it was an easy shift to make. The one place it hurts a bit is trying to load union values from env (which looks like is a feature request on Github). But the end result is definitely better, thanks!


Yeah, we’ll do something to handle the union values from env case soon-ish, because it comes up as a request so frequently