Skip to content

Commit

Permalink
feat: added more validations
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Feb 9, 2024
1 parent 9fb8c3b commit 4d5e11b
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 183 deletions.
183 changes: 0 additions & 183 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package model

import (
"encoding/json"
"fmt"
"github.com/goccy/go-yaml"
"regexp"
)

type Template struct {
Expand Down Expand Up @@ -106,184 +104,3 @@ func FromYAML(yamlString string) (Template, error) {

return t, nil
}

func (v Variable) Validate() []error {
var errors []error

// Check that type is set
if v.Type == "" {
errors = append(errors, newValidationError(v, "type is required"))
}

// Min and max are only applicable to number types
if v.Min != 0 || v.Max != 0 {
if v.Type != "number" {
errors = append(errors, newValidationError(v, "min and max are only applicable to number types"))
}
}

// Regex is only applicable to text types
if v.Regex != "" {
if v.Type != "text" {
errors = append(errors, newValidationError(v, "regex is only applicable to text types"))
}
}

// Multiline is only applicable to text types
if v.Multiline {
if v.Type != "text" {
errors = append(errors, newValidationError(v, "multiline is only applicable to text types"))
}
}

// Options are only applicable to select and multiselect types
if len(v.Options) > 0 {
if v.Type != "select" && v.Type != "multiselect" {
errors = append(errors, newValidationError(v, "options are only applicable to select and multiselect types"))
}
}

// Specific type validations
switch v.Type {
case "number":
var value float64
if v.Value != nil {
var ok bool
value, ok = v.Value.(float64)
if !ok {
errors = append(errors, newValidationError(v, "value must be a number"))
}
}

if v.Min != 0 || v.Max != 0 { // min or max is set
if v.Min > v.Max {
errors = append(errors, newValidationError(v, "min must be less than max"))
}

if v.Value != nil {
if value < v.Min || value > v.Max {
errors = append(errors, newValidationError(v, "value must be between min and max"))
}
}
}
case "text":
var value string
if v.Value != nil {
var ok bool
value, ok = v.Value.(string)
if !ok {
errors = append(errors, newValidationError(v, "value must be a string"))
}
}

// Validate regex
if v.Regex != "" {
if v.Value != nil {
re, err := regexp.Compile(v.Regex)
if err != nil {
errors = append(errors, newValidationError(v, "invalid regex"))
}

if !re.MatchString(value) {
errors = append(errors, newValidationError(v, "value does not match regex"))
}
}
}

case "boolean":
if v.Value != nil {
_, ok := v.Value.(bool)
if !ok {
errors = append(errors, newValidationError(v, "value must be a boolean"))
}
}

case "select":
if len(v.Options) == 0 {
errors = append(errors, newValidationError(v, "options are required"))
}

if v.Value != nil {
var ok bool
_, ok = v.Value.(string)
if !ok {
errors = append(errors, newValidationError(v, "value must be a string"))
}
}

case "multiselect":
if len(v.Options) == 0 {
errors = append(errors, newValidationError(v, "options are required"))
}

if v.Value != nil {
// Value must be either string or string slice
switch v.Value.(type) {
case string:
// Check if value is in options
found := false
for _, o := range v.Options {
if v.Value == o.Value {
found = true
break
}
}

if !found {
errors = append(errors, newValidationError(v, "value is not in options"))
}

case []string:
// Check if all values are in options
for _, value := range v.Value.([]string) {
found := false
for _, o := range v.Options {
if value == o.Value {
found = true
break
}
}

if !found {
errors = append(errors, newValidationError(v, "value is not in options"))
}
}

default:
errors = append(errors, newValidationError(v, "value must be a string or string slice"))
}
}

}

if len(errors) > 0 {
return errors
}

return nil
}

func (t Template) Validate() []error {
var errors []error

if t.Template == "" {
errors = append(errors, fmt.Errorf("template is required"))
}

for _, v := range t.Variables {
errs := v.Validate()
if errs != nil {
errors = append(errors, errs...)
}
}

if len(errors) > 0 {
return errors
}

return nil
}

func newValidationError(v Variable, message string) error {
return fmt.Errorf("variable %s: %s", v.Name, message)
}
Loading

0 comments on commit 4d5e11b

Please sign in to comment.