Fix pattern validation for input properties (#912)

## Changes
Fixes bug where input validation would only be done on the first input
parameter in the template schema.

## Tests
Unit test.
This commit is contained in:
shreyas-goenka 2023-10-24 17:56:54 +02:00 committed by GitHub
parent 3411b8aa37
commit f8d7e31118
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 1 deletions

View File

@ -122,7 +122,10 @@ func (s *Schema) validatePattern(instance map[string]any) error {
if !ok { if !ok {
continue continue
} }
return ValidatePatternMatch(k, v, fieldInfo) err := ValidatePatternMatch(k, v, fieldInfo)
if err != nil {
return err
}
} }
return nil return nil
} }

View File

@ -193,3 +193,32 @@ func TestValidateInstancePatternWithCustomMessage(t *testing.T) {
assert.EqualError(t, schema.validatePattern(invalidInstanceValue), "invalid value for foo: \"xyz\". Please enter a string starting with 'a' and ending with 'c'") assert.EqualError(t, schema.validatePattern(invalidInstanceValue), "invalid value for foo: \"xyz\". Please enter a string starting with 'a' and ending with 'c'")
assert.EqualError(t, schema.ValidateInstance(invalidInstanceValue), "invalid value for foo: \"xyz\". Please enter a string starting with 'a' and ending with 'c'") assert.EqualError(t, schema.ValidateInstance(invalidInstanceValue), "invalid value for foo: \"xyz\". Please enter a string starting with 'a' and ending with 'c'")
} }
func TestValidateInstanceForMultiplePatterns(t *testing.T) {
schema, err := Load("./testdata/instance-validate/multiple-patterns-schema.json")
require.NoError(t, err)
// Valid values for both foo and bar
validInstance := map[string]any{
"foo": "abcc",
"bar": "deff",
}
assert.NoError(t, schema.validatePattern(validInstance))
assert.NoError(t, schema.ValidateInstance(validInstance))
// Valid value for bar, invalid value for foo
invalidInstanceValue := map[string]any{
"foo": "xyz",
"bar": "deff",
}
assert.EqualError(t, schema.validatePattern(invalidInstanceValue), "invalid value for foo: \"xyz\". Expected to match regex pattern: ^[a-c]+$")
assert.EqualError(t, schema.ValidateInstance(invalidInstanceValue), "invalid value for foo: \"xyz\". Expected to match regex pattern: ^[a-c]+$")
// Valid value for foo, invalid value for bar
invalidInstanceValue = map[string]any{
"foo": "abcc",
"bar": "xyz",
}
assert.EqualError(t, schema.validatePattern(invalidInstanceValue), "invalid value for bar: \"xyz\". Expected to match regex pattern: ^[d-f]+$")
assert.EqualError(t, schema.ValidateInstance(invalidInstanceValue), "invalid value for bar: \"xyz\". Expected to match regex pattern: ^[d-f]+$")
}

View File

@ -0,0 +1,12 @@
{
"properties": {
"foo": {
"type": "string",
"pattern": "^[a-c]+$"
},
"bar": {
"type": "string",
"pattern": "^[d-f]+$"
}
}
}