Add tests for validate config

This commit is contained in:
Shreyas Goenka 2023-05-17 14:42:24 +02:00
parent 1d5e09bd39
commit fb2da25f28
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
2 changed files with 127 additions and 0 deletions

View File

@ -92,6 +92,14 @@ func validateType(v any, fieldType FieldType) error {
// TODO: add validation check for regex for string types
func (schema Schema) ValidateConfig(config map[string]any) error {
// assert all fields are defined in
for k := range schema {
if _, ok := config[k]; !ok {
return fmt.Errorf("input parameter %s is not defined in config", k)
}
}
// validate types defined in config
for k, v := range config {
fieldMetadata, ok := schema[k]
if !ok {

View File

@ -168,3 +168,122 @@ func TestTemplateSchemaValidateType(t *testing.T) {
err = validateType(false, FieldTypeString)
assert.ErrorContains(t, err, "expected type string, but value is false")
}
func TestTemplateSchemaValidateConfig(t *testing.T) {
// define schema for config
schemaJson := `{
"int_val": {
"type": "integer"
},
"float_val": {
"type": "float"
},
"bool_val": {
"type": "boolean"
},
"string_val": {
"type": "string"
}
}`
var schema Schema
err := json.Unmarshal([]byte(schemaJson), &schema)
require.NoError(t, err)
// define the config
config := map[string]any{
"int_val": 1,
"float_val": 1.1,
"bool_val": true,
"string_val": "abc",
}
err = schema.ValidateConfig(config)
assert.NoError(t, err)
}
func TestTemplateSchemaValidateConfigFailsForUnknownField(t *testing.T) {
// define schema for config
schemaJson := `{
"int_val": {
"type": "integer"
},
"float_val": {
"type": "float"
},
"bool_val": {
"type": "boolean"
},
"string_val": {
"type": "string"
}
}`
var schema Schema
err := json.Unmarshal([]byte(schemaJson), &schema)
require.NoError(t, err)
// define the config
config := map[string]any{
"foo": 1,
"float_val": 1.1,
"bool_val": true,
"string_val": "abc",
}
err = schema.ValidateConfig(config)
assert.ErrorContains(t, err, "foo is not defined as an input parameter for the template")
}
func TestTemplateSchemaValidateConfigFailsForWhenIncorrectTypes(t *testing.T) {
// define schema for config
schemaJson := `{
"int_val": {
"type": "integer"
},
"float_val": {
"type": "float"
},
"bool_val": {
"type": "boolean"
},
"string_val": {
"type": "string"
}
}`
var schema Schema
err := json.Unmarshal([]byte(schemaJson), &schema)
require.NoError(t, err)
// define the config
config := map[string]any{
"int_val": 1,
"float_val": 1.1,
"bool_val": "true",
"string_val": "abc",
}
err = schema.ValidateConfig(config)
assert.ErrorContains(t, err, "incorrect type for bool_val. expected type boolean, but value is \"true\"")
}
func TestTemplateSchemaValidateConfigFailsForWhenMissingInputParams(t *testing.T) {
// define schema for config
schemaJson := `{
"int_val": {
"type": "integer"
},
"string_val": {
"type": "string"
}
}`
var schema Schema
err := json.Unmarshal([]byte(schemaJson), &schema)
require.NoError(t, err)
// define the config
config := map[string]any{
"int_val": 1,
}
err = schema.ValidateConfig(config)
assert.ErrorContains(t, err, "input parameter string_val is not defined in config")
}