@ -136,6 +136,8 @@ type Schema struct {
// logic. It is yielded the provided config value as an interface{} that is
// guaranteed to be of the proper Schema type, and it can yield warnings or
// errors based on inspection of that value.
//
// ValidateFunc currently only works for primitive types.
ValidateFunc SchemaValidateFunc
}
@ -513,6 +515,13 @@ func (m schemaMap) InternalValidate(topSchemaMap schemaMap) error {
}
}
}
if v . ValidateFunc != nil {
switch v . Type {
case TypeList , TypeSet , TypeMap :
return fmt . Errorf ( "ValidateFunc is only supported on primitives." )
}
}
}
return nil
@ -1128,6 +1137,7 @@ func (m schemaMap) validatePrimitive(
return nil , nil
}
var decoded interface { }
switch schema . Type {
case TypeBool :
// Verify that we can parse this as the correct type
@ -1135,28 +1145,36 @@ func (m schemaMap) validatePrimitive(
if err := mapstructure . WeakDecode ( raw , & n ) ; err != nil {
return nil , [ ] error { err }
}
decoded = n
case TypeInt :
// Verify that we can parse this as an int
var n int
if err := mapstructure . WeakDecode ( raw , & n ) ; err != nil {
return nil , [ ] error { err }
}
decoded = n
case TypeFloat :
// Verify that we can parse this as an int
var n float64
if err := mapstructure . WeakDecode ( raw , & n ) ; err != nil {
return nil , [ ] error { err }
}
decoded = n
case TypeString :
// Verify that we can parse this as a string
var n string
if err := mapstructure . WeakDecode ( raw , & n ) ; err != nil {
return nil , [ ] error { err }
}
decoded = n
default :
panic ( fmt . Sprintf ( "Unknown validation type: %#v" , schema . Type ) )
}
if schema . ValidateFunc != nil {
return schema . ValidateFunc ( decoded )
}
return nil , nil
}
@ -1186,16 +1204,6 @@ func (m schemaMap) validateType(
"%q: [REMOVED] %s" , k , schema . Removed ) )
}
if len ( es ) == 0 && schema . ValidateFunc != nil {
ws2 , es2 := schema . ValidateFunc ( raw )
for _ , w := range ws2 {
ws = append ( ws , fmt . Sprintf ( "%q: %s" , k , w ) )
}
for _ , e := range es2 {
es = append ( es , fmt . Errorf ( "%q: %s" , k , e ) )
}
}
return ws , es
}