mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Accept all values as string type in validation
This commit is contained in:
@@ -5,6 +5,7 @@ go 1.14
|
|||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.1
|
github.com/davecgh/go-spew v1.1.1
|
||||||
github.com/go-errors/errors v1.0.1
|
github.com/go-errors/errors v1.0.1
|
||||||
|
github.com/go-openapi/errors v0.19.2
|
||||||
github.com/go-openapi/spec v0.19.5
|
github.com/go-openapi/spec v0.19.5
|
||||||
github.com/go-openapi/strfmt v0.19.5
|
github.com/go-openapi/strfmt v0.19.5
|
||||||
github.com/go-openapi/validate v0.19.8
|
github.com/go-openapi/validate v0.19.8
|
||||||
|
|||||||
@@ -244,8 +244,23 @@ func validateAgainstSchema(ext *CliExtension, sch *spec.Schema) error {
|
|||||||
|
|
||||||
var inputYAML string
|
var inputYAML string
|
||||||
if len(ext.Setter.ListValues) > 0 {
|
if len(ext.Setter.ListValues) > 0 {
|
||||||
tmpl, err := template.New("validator").
|
// tmplText contains the template we will use to produce a yaml
|
||||||
Parse(`{{.key}}:{{block "list" .values}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}`)
|
// document that we can use for validation.
|
||||||
|
var tmplText string
|
||||||
|
if sch.Items != nil && sch.Items.Schema != nil &&
|
||||||
|
sch.Items.Schema.Type.Contains("string") {
|
||||||
|
// If string is one of the legal types for the value, we
|
||||||
|
// output it with quotes in the yaml document to make sure it
|
||||||
|
// is later parsed as a string.
|
||||||
|
tmplText = `{{.key}}:{{block "list" .values}}{{"\n"}}{{range .}}{{printf "- %q\n" .}}{{end}}{{end}}`
|
||||||
|
} else {
|
||||||
|
// If string is not specifically set as the type, we just
|
||||||
|
// let the yaml unmarshaller detect the correct type. Thus, we
|
||||||
|
// do not add quotes around the value.
|
||||||
|
tmplText = `{{.key}}:{{block "list" .values}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}`
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err := template.New("validator").Parse(tmplText)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -259,7 +274,15 @@ func validateAgainstSchema(ext *CliExtension, sch *spec.Schema) error {
|
|||||||
}
|
}
|
||||||
inputYAML = builder.String()
|
inputYAML = builder.String()
|
||||||
} else {
|
} else {
|
||||||
inputYAML = fmt.Sprintf("%s: %s", ext.Setter.Name, ext.Setter.Value)
|
var format string
|
||||||
|
// Only add quotes around the value is string is one of the
|
||||||
|
// types in the schema.
|
||||||
|
if sch.Type.Contains("string") {
|
||||||
|
format = "%s: \"%s\""
|
||||||
|
} else {
|
||||||
|
format = "%s: %s"
|
||||||
|
}
|
||||||
|
inputYAML = fmt.Sprintf(format, ext.Setter.Name, ext.Setter.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
input := map[string]interface{}{}
|
input := map[string]interface{}{}
|
||||||
|
|||||||
@@ -1296,6 +1296,8 @@ openAPI:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestValidateAgainstSchema(t *testing.T) {
|
func TestValidateAgainstSchema(t *testing.T) {
|
||||||
|
maxLength := int64(3)
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
setter *setter
|
setter *setter
|
||||||
@@ -1303,6 +1305,15 @@ func TestValidateAgainstSchema(t *testing.T) {
|
|||||||
shouldValidate bool
|
shouldValidate bool
|
||||||
expectedErrorMsg string
|
expectedErrorMsg string
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
name: "no schema",
|
||||||
|
setter: &setter{
|
||||||
|
Name: "foo",
|
||||||
|
Value: "bar",
|
||||||
|
},
|
||||||
|
schema: spec.SchemaProps{},
|
||||||
|
shouldValidate: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "simple string value",
|
name: "simple string value",
|
||||||
setter: &setter{
|
setter: &setter{
|
||||||
@@ -1348,7 +1359,7 @@ func TestValidateAgainstSchema(t *testing.T) {
|
|||||||
shouldValidate: true,
|
shouldValidate: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "number value should not be accepted as string",
|
name: "number value should be accepted as integer",
|
||||||
setter: &setter{
|
setter: &setter{
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
Value: "45",
|
Value: "45",
|
||||||
@@ -1358,6 +1369,19 @@ func TestValidateAgainstSchema(t *testing.T) {
|
|||||||
},
|
},
|
||||||
shouldValidate: true,
|
shouldValidate: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "string type allows string-specific validations",
|
||||||
|
setter: &setter{
|
||||||
|
Name: "foo",
|
||||||
|
Value: "1234",
|
||||||
|
},
|
||||||
|
schema: spec.SchemaProps{
|
||||||
|
Type: []string{"string"},
|
||||||
|
MaxLength: &maxLength,
|
||||||
|
},
|
||||||
|
shouldValidate: false,
|
||||||
|
expectedErrorMsg: "foo in body should be at most 3 chars long",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "list with int values",
|
name: "list with int values",
|
||||||
setter: &setter{
|
setter: &setter{
|
||||||
@@ -1395,6 +1419,44 @@ func TestValidateAgainstSchema(t *testing.T) {
|
|||||||
shouldValidate: false,
|
shouldValidate: false,
|
||||||
expectedErrorMsg: "foo in body must be of type integer",
|
expectedErrorMsg: "foo in body must be of type integer",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "all values should satisfy type string",
|
||||||
|
setter: &setter{
|
||||||
|
Name: "foo",
|
||||||
|
Value: "1234",
|
||||||
|
},
|
||||||
|
schema: spec.SchemaProps{
|
||||||
|
Type: []string{"string"},
|
||||||
|
},
|
||||||
|
shouldValidate: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "all values should satisfy type string even in arrays",
|
||||||
|
setter: &setter{
|
||||||
|
Name: "foo",
|
||||||
|
ListValues: []string{"123", "456"},
|
||||||
|
},
|
||||||
|
schema: spec.SchemaProps{
|
||||||
|
Type: []string{"array"},
|
||||||
|
Items: &spec.SchemaOrArray{
|
||||||
|
Schema: &spec.Schema{
|
||||||
|
SchemaProps: spec.SchemaProps{
|
||||||
|
Type: []string{"string"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
shouldValidate: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "List values without any schema",
|
||||||
|
setter: &setter{
|
||||||
|
Name: "foo",
|
||||||
|
ListValues: []string{"123", "true", "abc"},
|
||||||
|
},
|
||||||
|
schema: spec.SchemaProps{},
|
||||||
|
shouldValidate: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range testCases {
|
for i := range testCases {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func TestIsYaml1_1NonString(t *testing.T) {
|
|||||||
{val: "2", expected: true},
|
{val: "2", expected: true},
|
||||||
{val: "true", expected: true},
|
{val: "true", expected: true},
|
||||||
{val: "1.0\nhello", expected: false}, // multiline strings should always be false
|
{val: "1.0\nhello", expected: false}, // multiline strings should always be false
|
||||||
{val: "", expected: false}, // empty string should be considered a string
|
{val: "", expected: false}, // empty string should be considered a string
|
||||||
}
|
}
|
||||||
|
|
||||||
for k := range valueToTagMap {
|
for k := range valueToTagMap {
|
||||||
|
|||||||
Reference in New Issue
Block a user