envvars icon indicating copy to clipboard operation
envvars copied to clipboard

Envvar 'Allowed Values' Enum?

Open andyt10 opened this issue 3 years ago • 3 comments

I don't know if this is trying to solve a problem in the wrong place, but I have reached a conclusion it might be useful to specify a list of allowed values for env vars when using this tool. A validator of sorts:

Example:

envvars:
  - name: ENV
    desc: Application stage (dev, qa, preprod, prod)
    tags:
      - deploy
    optional: true
    example: dev
    values:
        - dev
        - qa
        - preprod
        - prod

I'd be happy to flex my go muscles and contribute this, just want to get some input before I do :)

andyt10 avatar Nov 12 '21 02:11 andyt10

Hi @andyt10 ! Thanks for creating a new issue. I did think about it before and I am not sure what is the best solution. We can start with string values but I think eventually, one will want types and different validations. A bit like AWS parameters. Is it where envvars should be headed?

I also wouldn't mind having value validation based on regex. Introducing this feature would probably be simple as well as providing more options to the user.

Lately, I have been playing with CUE. You can see my spike in _cuelang.

flemay avatar Nov 12 '21 10:11 flemay

Hey @flemay I think regex validation could also be a useful feature, for similar reasons I'd like to specify a list of allowed.

I also think you might be right in saying that extending the functionality of envvars might be helpdful/desired using something like CUE . For something 'next version' like that I'd perhaps picture myself packaging up a standard library of envvar/config type definitions for use in multiple projects....somehow...but that's a bit beyond what I'm considering right now.😄

For me regex/string enum validation would suit the majority of use cases that come to mind for making sure a value conforms. I sort of pictured something like above:

      - name: SERVER_ENV
      .....
      .....
      values:
          - dev
          - qa
          - preprod
          - prod

which I think would also work similarly for regexes:

      - name: SERVER_ENV
      .....
      .....
      matchRegex: ^some[0-9]{1,}value$

where both options would be mutually exclusive (defining both would be invalid)

andyt10 avatar Nov 21 '21 23:11 andyt10

What I meant is that values list can be written using regex :)

package main

import (
	"fmt"
	"log"
	"regexp"
)

func main() {
	r, err := regexp.Compile("(^dev$)|(^qa$)|(^preprod$)|(^prod$)")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(r.MatchString("qa"))         // true
	fmt.Println(r.MatchString("preprod"))    // true
	fmt.Println(r.MatchString("production")) // false
}

flemay avatar Nov 22 '21 11:11 flemay