validation: Add ValidateRFC3339TimeString
This commit is contained in:
parent
26058acdef
commit
ea64b24cd9
|
@ -6,6 +6,7 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/hashicorp/terraform/helper/structure"
|
"github.com/hashicorp/terraform/helper/structure"
|
||||||
|
@ -210,3 +211,12 @@ func ValidateRegexp(v interface{}, k string) (ws []string, errors []error) {
|
||||||
}
|
}
|
||||||
return
|
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: %s", k, err))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -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(`cannot parse "1/2018" as "2006"`)),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
val: "03-01-2018",
|
||||||
|
f: ValidateRFC3339TimeString,
|
||||||
|
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`cannot parse "1-2018" as "2006"`)),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
val: "2018-03-01",
|
||||||
|
f: ValidateRFC3339TimeString,
|
||||||
|
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`cannot parse "" as "T"`)),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
val: "2018-03-01T",
|
||||||
|
f: ValidateRFC3339TimeString,
|
||||||
|
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`cannot parse "" as "15"`)),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
val: "2018-03-01T00:00:00",
|
||||||
|
f: ValidateRFC3339TimeString,
|
||||||
|
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`cannot parse "" as "Z07:00"`)),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
val: "2018-03-01T00:00:00Z05:00",
|
||||||
|
f: ValidateRFC3339TimeString,
|
||||||
|
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`extra text: 05:00`)),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
val: "2018-03-01T00:00:00Z-05:00",
|
||||||
|
f: ValidateRFC3339TimeString,
|
||||||
|
expectedErr: regexp.MustCompile(regexp.QuoteMeta(`extra text: -05:00`)),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestValidateJsonString(t *testing.T) {
|
func TestValidateJsonString(t *testing.T) {
|
||||||
type testCases struct {
|
type testCases struct {
|
||||||
Value string
|
Value string
|
||||||
|
|
Loading…
Reference in New Issue