//NewConfiguration loads a configuration from data //if it is not valid json, it will return an error func NewConfiguration(configData []byte) (*Configuration, error) { var config Configuration err := json.Unmarshal(configData, &config) if err != nil { return nil, err } validate := validator.New(&validator.Config{TagName: "validate"}) err = validate.Struct(config) if err != nil { return nil, getValidationError(err.(validator.ValidationErrors)) } return &config, err }
func TestBoolStringValidate(t *testing.T) { v := validator.New( &validator.Config{TagName: "validate"}, ) err := v.RegisterValidation("bool", libyaml.IsBoolValidation) if err != nil { t.Fatal(err) } type S struct { Bool libyaml.BoolString `yaml:"is_true" validate:"bool"` } s := S{Bool: libyaml.BoolString("true")} err = v.Struct(s) if err != nil { t.Fatal(err) } s = S{Bool: libyaml.BoolString("")} err = v.Struct(s) if err != nil { t.Fatal(err) } s = S{Bool: libyaml.BoolString("0")} err = v.Struct(s) if err != nil { t.Fatal(err) } s = S{Bool: libyaml.BoolString("{{repl blah }}")} err = v.Struct(s) if err != nil { t.Fatal(err) } s = S{Bool: libyaml.BoolString("blah")} err = v.Struct(s) if err == nil { t.Fatal(err) } if !strings.Contains(err.Error(), "failed on the 'bool' tag") { t.Errorf("unexpected error, %v", err) } }