Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding general yaml validation #1140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ Baked-in Validations
| semver | Semantic Versioning 2.0.0 |
| ulid | Universally Unique Lexicographically Sortable Identifier ULID |
| cve | Common Vulnerabilities and Exposures Identifier (CVE id) |
| yaml | Yaml document |

### Comparisons:
| Tag | Description |
Expand Down
11 changes: 11 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"golang.org/x/crypto/sha3"
"golang.org/x/text/language"
"gopkg.in/yaml.v3"

"github.com/gabriel-vasile/mimetype"
"github.com/leodido/go-urn"
Expand Down Expand Up @@ -231,6 +232,7 @@ var (
"mongodb": isMongoDB,
"cron": isCron,
"spicedb": isSpiceDB,
"yaml": isYaml,
}
)

Expand Down Expand Up @@ -2878,3 +2880,12 @@ func isCron(fl FieldLevel) bool {
cronString := fl.Field().String()
return cronRegex.MatchString(cronString)
}

// isYaml is the validation for validating if the the current fields value is a valid yaml document
func isYaml(f1 FieldLevel) bool {
document := f1.Field().String()
var parsedDoc interface{}
err := yaml.Unmarshal([]byte(document), &parsedDoc)

return err == nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ require (
require (
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
39 changes: 39 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13487,3 +13487,42 @@ func TestNestedStructValidation(t *testing.T) {
})
}
}

func TestIsYamlValidation(t *testing.T) {

rawLiteral := `
- first_element: element name
a: true
b:
- inner_element: Only element of the inner list.
c:
d: "This string is on the deepest level"`

tests := []struct {
value string `validate:"yaml"`
tag string
expected bool
}{
{"singleline: one line", "yaml", true},
{"multiline: A\nb: B\n", "yaml", true},
{"mixedtype: 1\nb: True\nc: [ 1, 1, 2, 3, 5]", "yaml", true},
{rawLiteral, "yaml", true},
{"a: ?", "yaml", false},
{"...", "yaml", false},
}

validate := New()

for i, test := range tests {
errs := validate.Var(test.value, test.tag)
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf(`Index: %d yaml "%s" failed Error: %s`, i, test.value, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf(`Index: %d yaml "%s" should have errs`, i, test.value)
}
}
}
}