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

validation: Add ValidateRFC3339TimeString #17484

Merged
merged 2 commits into from
Mar 15, 2018
Merged
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
10 changes: 10 additions & 0 deletions helper/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"regexp"
"strings"
"time"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/structure"
Expand Down Expand Up @@ -210,3 +211,12 @@ func ValidateRegexp(v interface{}, k string) (ws []string, errors []error) {
}
return
}

// ValidateRFC3339TimeString is a ValidateFunc that ensures a string parses
// as time.RFC3339 format
func ValidateRFC3339TimeString(v interface{}, k string) (ws []string, errors []error) {
if _, err := time.Parse(time.RFC3339, v.(string)); err != nil {
errors = append(errors, fmt.Errorf("%q: invalid RFC3339 timestamp", k))
}
return
}
52 changes: 52 additions & 0 deletions helper/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,58 @@ func TestValidationRegexp(t *testing.T) {
})
}

func TestValidateRFC3339TimeString(t *testing.T) {
runTestCases(t, []testCase{
{
val: "2018-03-01T00:00:00Z",
f: ValidateRFC3339TimeString,
},
{
val: "2018-03-01T00:00:00-05:00",
f: ValidateRFC3339TimeString,
},
{
val: "2018-03-01T00:00:00+05:00",
f: ValidateRFC3339TimeString,
},
{
val: "03/01/2018",
f: ValidateRFC3339TimeString,
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`invalid RFC3339 timestamp`)),
},
{
val: "03-01-2018",
f: ValidateRFC3339TimeString,
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`invalid RFC3339 timestamp`)),
},
{
val: "2018-03-01",
f: ValidateRFC3339TimeString,
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`invalid RFC3339 timestamp`)),
},
{
val: "2018-03-01T",
f: ValidateRFC3339TimeString,
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`invalid RFC3339 timestamp`)),
},
{
val: "2018-03-01T00:00:00",
f: ValidateRFC3339TimeString,
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`invalid RFC3339 timestamp`)),
},
{
val: "2018-03-01T00:00:00Z05:00",
f: ValidateRFC3339TimeString,
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`invalid RFC3339 timestamp`)),
},
{
val: "2018-03-01T00:00:00Z-05:00",
f: ValidateRFC3339TimeString,
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`invalid RFC3339 timestamp`)),
},
})
}

func TestValidateJsonString(t *testing.T) {
type testCases struct {
Value string
Expand Down