2020-03-14 14:24:17 +00:00
|
|
|
package config
|
|
|
|
|
2020-03-16 08:20:00 +00:00
|
|
|
import "git.xdrm.io/go/aicra/datatype"
|
2020-03-14 23:27:54 +00:00
|
|
|
|
2020-03-28 11:28:58 +00:00
|
|
|
// Validate implements the validator interface
|
|
|
|
func (param *Parameter) Validate(datatypes ...datatype.T) error {
|
2020-03-14 14:24:17 +00:00
|
|
|
// missing description
|
|
|
|
if len(param.Description) < 1 {
|
|
|
|
return ErrMissingParamDesc
|
|
|
|
}
|
|
|
|
|
|
|
|
// invalid type
|
|
|
|
if len(param.Type) < 1 || param.Type == "?" {
|
|
|
|
return ErrMissingParamType
|
|
|
|
}
|
|
|
|
|
2020-03-28 11:28:58 +00:00
|
|
|
// optional type transform
|
2020-03-14 14:24:17 +00:00
|
|
|
if param.Type[0] == '?' {
|
|
|
|
param.Optional = true
|
|
|
|
param.Type = param.Type[1:]
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:12:47 +00:00
|
|
|
// assign the datatype
|
|
|
|
datatypeFound := false
|
|
|
|
for _, dtype := range datatypes {
|
|
|
|
param.Validator = dtype.Build(param.Type, datatypes...)
|
|
|
|
if param.Validator != nil {
|
|
|
|
datatypeFound = true
|
|
|
|
param.ExtractType = dtype.Type()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !datatypeFound {
|
|
|
|
return ErrUnknownDataType
|
|
|
|
}
|
|
|
|
|
2020-03-14 14:24:17 +00:00
|
|
|
return nil
|
|
|
|
}
|