# How Can I Validate ConfigOption Values?

**URL:** <https://community.replicated.com/t/how-can-i-validate-configoption-values/569>\
**Category:** How do I?\
**Created:** [September 2, 2021, 3:30pm UTC](https://community.replicated.com/t/how-can-i-validate-configoption-values/569 "2021-09-02T15:30:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fernando\_Cremer](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.replicated.com/fernando_cremer/32/436_2.png) [@Fernando\_Cremer](https://community.replicated.com/u/Fernando_Cremer)\
**Post date:** [September 2, 2021, 3:30pm UTC](https://community.replicated.com/t/how-can-i-validate-configoption-values/569/1 "2021-09-02T15:30:34Z")

</div>

A common request from KOTS users is the ability to validate data entered in a [Config](https://kots.io/reference/v1beta1/config/) field. Depending on your use case, there are a couple of options:

**Option 1. Use Labels to Warn the User**

Let’s say you want the user to provide an email address and would like to validate that the email is well formed. You could add a [Label](https://kots.io/reference/v1beta1/config/#label) field that is only displayed if the email does not look right.

In the example below, a Label is displayed when the email address looks correct and another one when it does not (note that this includes the field being empty)

```nohighlight
- name: email_addr
        title: Enter Email Address
        type: text
      - name: email_validate
        title: the email looks good
        type: label 
        when: '{{repl regexMatch "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$" (ConfigOption "email_addr") }}'
      - name: email_not_validate
        title: ' ***Please enter a valid email address!***'
        type: label 
        when: '{{repl not (regexMatch "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$" (ConfigOption "email_addr")) }}'

```

In the above example we are using the [regexMatch sprig](http://masterminds.github.io/sprig/strings.html) function to compare the value of the field against the regular expression.

To test the inverse, we used the [not](https://pkg.go.dev/text/template#hdr-Functions) Go Template Function.

Here is a screenshot of the Config field containing a valid email address:

 ![Screen Shot 2022-03-04 at 3.40.04 PM](https://canada1.discourse-cdn.com/flex030/uploads/replicated/original/1X/6f079b2802eec33fa340840f4bf0498f0ae20220.jpeg)

The label then changes as soon as the email address is changed and no longer valid:

 ![Screen Shot 2022-03-04 at 3.41.06 PM](https://canada1.discourse-cdn.com/flex030/uploads/replicated/original/1X/b632a645ea657c7f89cde2f229ff5b28de0dda9a.jpeg)

**Option 2. Use a PreFlight Check**

Let’s say that during installation you want the user to provide some http address. Could be a database your app needs to connect to or some other service. Before installing your app, you want to make sure you can at least ping that address.

In this case since the validation requires an action, using labels will not work. But what we can do is create a [PreFlight](https://troubleshoot.sh/docs/preflight/introduction/) check to ping the address and determine if we were able to connect.

This preflight would first need a couple of [Collector](https://troubleshoot.sh/docs/collect/collectors/) to get the address and then run `ping` against it. We’ll then need an [Analyzer](https://troubleshoot.sh/docs/analyze/) to see the results and display a pass/fail message to the user.

If we named our field “service\_addr” the preflight would look like this:

```nohighlight
apiVersion: troubleshoot.replicated.com/v1beta1
kind: Preflight
metadata:
  name: example-preflight-checks
spec:
  collectors:
    - run:
        collectorName: "run-ping"
        image: busybox:1
        name: ping.txt
        namespace: default
        command: ["ping"]
        args: ["-w", "10", "-c", "10", "-i", "0.3", '{{repl ConfigOption "service_addr"}}']
        imagePullPolicy: IfNotPresent
  analyzers:
    - textAnalyze:
        checkName: "Validate Address"
        fileName: ping.txt/run-ping.log
        regexGroups: '(?P<Transmitted>\d+) packets? transmitted, (?P<Received>\d+) packets? received, (?P<Loss>\d+)(\.\d+)?% packet loss'
        outcomes:
          - pass:
              when: "Loss < 5"
              message: Solid connection to {{repl ConfigOption "service_addr"}}
          - fail:
              message: Unable to establish a solid connection to {{repl ConfigOption "service_addr"}}, please ensure this is valid

```

There is a similar [example](https://troubleshoot.sh/docs/analyze/regex/#example-analyzer-definition-for-regexgroups) in the docs.

**Data Redaction**

One note to make is that when you are using Collectors, some values will automatically be [redacted](https://troubleshoot.sh/docs/redact/). So the above example may not work if you want to validate an IP address, as that is one of the values that gets redacted

**Debugging**

One of the challenges debugging preflight checks is that collector results are not easily available. However, they are available if you add the collector to a [SupportBundle](https://troubleshoot.sh/docs/support-bundle/introduction/).
