2021-06-21 19:30:33 +00:00
|
|
|
package validator
|
2020-03-14 15:13:38 +00:00
|
|
|
|
2020-03-28 17:48:27 +00:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
)
|
2020-03-14 15:13:38 +00:00
|
|
|
|
2021-06-21 19:30:33 +00:00
|
|
|
// BoolType makes the "bool" type available in the aicra configuration
|
|
|
|
// It considers valid:
|
|
|
|
// - booleans
|
|
|
|
// - strings containing "true" or "false"
|
|
|
|
// - []byte containing "true" or "false"
|
|
|
|
type BoolType struct{}
|
2020-03-14 15:13:38 +00:00
|
|
|
|
2021-06-21 19:30:33 +00:00
|
|
|
// GoType returns the `bool` type
|
|
|
|
func (BoolType) GoType() reflect.Type {
|
2020-03-28 18:11:23 +00:00
|
|
|
return reflect.TypeOf(true)
|
2020-03-28 17:48:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 19:30:33 +00:00
|
|
|
// Validator for bool values
|
|
|
|
func (BoolType) Validator(typename string, avail ...Type) ValidateFunc {
|
|
|
|
if typename != "bool" {
|
2020-03-14 15:13:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(value interface{}) (interface{}, bool) {
|
|
|
|
switch cast := value.(type) {
|
|
|
|
case bool:
|
|
|
|
return cast, true
|
|
|
|
|
|
|
|
case string:
|
|
|
|
strVal := string(cast)
|
|
|
|
return strVal == "true", strVal == "true" || strVal == "false"
|
|
|
|
case []byte:
|
|
|
|
strVal := string(cast)
|
|
|
|
return strVal == "true", strVal == "true" || strVal == "false"
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|