func TestUserDefinedType(t *testing.T) { dat := map[string]interface{}{ "a": "asdasd1", } scheme1 := map[string]interface{}{ "a": map[string]interface{}{ "type": "myType", "must": true, }, } ex, err := sanitize.New(scheme1) if err != nil { t.Fatal() } ex.AddFuncs(sanitize.FuncMap{ "myType": func(dat interface{}, s sanitize.Scheme) (interface{}, error) { val, ok := dat.(string) if !ok { return nil, fmt.Errorf("Baaad.") } if val == "asdasd" { return val, nil } return nil, fmt.Errorf("This is baad too.") }, }) _, err = ex.Extract(dat) if err == nil { t.Fatal() } }
func TestComposite(t *testing.T) { dat := map[string]interface{}{ "a": "asdasd", "b": "20", "c": "Hey there.", } scheme1 := map[string]interface{}{ "a": map[string]interface{}{ "type": "bool", }, "b": map[string]interface{}{ "type": "float", "must": true, }, "c": map[string]interface{}{ "type": "string", "must": true, }, } ex, err := sanitize.New(scheme1) if err != nil { t.Fatal() } val, err := ex.Extract(dat) if err != nil || len(val) != 2 { t.Fatal() } }
func TestIgnoreMust(t *testing.T) { dat := map[string]interface{}{ "a": "asdsadasd", "b": "asdsad", "c": "Hello.", } scheme1 := map[string]interface{}{ "a": map[string]interface{}{ "type": "string", "ignore": true, "min": 1000, "must": true, }, "b": 1, "c": 1, } ex, err := sanitize.New(scheme1) if err != nil { t.Fatal() } _, err = ex.Extract(dat) if err == nil { t.Fatal() } }
// Special fields in query: // parentf, sort, limit, skip, page func processMap(inp map[string]interface{}, hooks iface.Hooks) *data { d := &data{} if inp == nil { inp = map[string]interface{}{} } int_sch := map[string]interface{}{ "type": "int", } sch := map[string]interface{}{ //"parentf": 1, "sort": map[string]interface{}{ "slice": true, "type": "string", }, "skip": int_sch, "limit": int_sch, "page": int_sch, } ex, err := sanitize.New(sch) if err != nil { panic(err) } dat, err := ex.Extract(inp) if err != nil { panic(err) } for i := range sch { delete(inp, i) } mods := &Mods{} //if dat["parentf"] != nil { // d.parentField = dat["parentf"].(string) //} if dat["sort"] != nil { mods.sort = convert.ToStringSlice(dat["sort"].([]interface{})) } if dat["skip"] != nil { mods.skip = int(dat["skip"].(int64)) } if dat["limit"] != nil { mods.limit = int(dat["limit"].(int64)) } else { mods.limit = 20 } if dat["page"] != nil { page := int(dat["page"].(int64)) mods.skip = (page - 1) * mods.limit } d.mods = mods if hooks != nil { hooks.Select("ProcessMap").Fire(inp) // We should let the subscriber now the subject name maybe. } d.query = toQuery(inp) return d }
// Currently, Verbs having no input scheme will receive the incoming data untouched. func (h *HighLev) validate(noun, verb string, data map[string]interface{}) (map[string]interface{}, error) { schemeMap, ok := jsonp.GetM(h.nouns, fmt.Sprintf("%v.verbs.%v.input", noun, verb)) if ok { ex, err := sanitize.New(schemeMap) if err != nil { return nil, err } h.hooks.Select("SanitizerMangler").Fire(ex) data, err = ex.Extract(data) if err != nil { return nil, err } } h.hooks.Select("SanitizedDataMangler").Fire(data) return data, nil }
func (t *Top) validate(noun, verb string, data map[string]interface{}) (map[string]interface{}, error) { scheme_map, ok := jsonp.GetM(t.uni.Opt, fmt.Sprintf("nouns.%v.verbs.%v.input", noun, verb)) if !ok { return nil, fmt.Errorf("Can't find scheme for %v %v.", noun, verb) } ex, err := sanitize.New(scheme_map) if err != nil { return nil, err } t.uni.Ev.Fire("SanitizerMangler", ex) data, err = ex.Extract(data) if err != nil { return nil, err } t.uni.Ev.Fire("SanitizedDataMangler", data) return data, nil }
func TestSlice(t *testing.T) { dat := map[string]interface{}{ "a": []interface{}{30, 20, "xxd"}, } scheme1 := map[string]interface{}{ "a": map[string]interface{}{ "type": "string", "slice": true, }, } ex, err := sanitize.New(scheme1) if err != nil { t.Fatal() } val, err := ex.Extract(dat) if err != nil || len(val["a"].([]interface{})) != 1 { t.Fatal() } }
func TestStringMax(t *testing.T) { dat := map[string]interface{}{ "a": "Hey there!", } scheme1 := map[string]interface{}{ "a": map[string]interface{}{ "type": "string", "max": 3, }, } ex, err := sanitize.New(scheme1) if err != nil { t.Fatal() } val, err := ex.Extract(dat) if err != nil || len(val) != 0 { t.Fatal() } }
func TestBoolMust(t *testing.T) { dat := map[string]interface{}{ "a": "asdasd", } scheme1 := map[string]interface{}{ "a": map[string]interface{}{ "type": "bool", "must": true, }, } ex, err := sanitize.New(scheme1) if err != nil { t.Fatal() } _, err = ex.Extract(dat) if err == nil { t.Fatal() } }
func TestMin(t *testing.T) { dat := map[string]interface{}{ "a": 500, } scheme1 := map[string]interface{}{ "a": map[string]interface{}{ "type": "int", "min": 600, "must": true, }, } ex, err := sanitize.New(scheme1) if err != nil { t.Fatal() } _, err = ex.Extract(dat) if err == nil { t.Fatal() } }
func TestIntMust(t *testing.T) { dat := map[string]interface{}{ "a": "Hey there!", } scheme1 := map[string]interface{}{ "a": map[string]interface{}{ "type": "int", "max": 3, "must": true, }, } ex, err := sanitize.New(scheme1) if err != nil { t.Fatal() } _, err = ex.Extract(dat) if err == nil { t.Fatal() } }
func TestStringMinMust(t *testing.T) { dat := map[string]interface{}{ "a": "Hey there!", } scheme1 := map[string]interface{}{ "a": map[string]interface{}{ "must": true, "type": "string", "min": 100, }, } ex, err := sanitize.New(scheme1) if err != nil { t.Fatal() } _, err = ex.Extract(dat) if err == nil { t.Fatal() } }
func TestConst(t *testing.T) { dat := map[string]interface{}{} scheme := map[string]interface{}{ "a": map[string]interface{}{ "type": "const", "value": "example", }, } ex, err := sanitize.New(scheme) if err != nil { t.Fatal() } res, err := ex.Extract(dat) if err != nil { t.Fatal(err) } if res["a"] != "example" { t.Fatal(res) } }
func TestShorthand1(t *testing.T) { dat := map[string]interface{}{ "a": 1, "b": "asdsad", "c": "Hello.", } scheme1 := map[string]interface{}{ "a": 1, "b": 1, "c": 1, } ex, err := sanitize.New(scheme1) if err != nil { t.Fatal() } val, err := ex.Extract(dat) if err != nil || len(val) != 2 { t.Fatal(err, val) } }
func TestSliceMustAllOrNothing(t *testing.T) { dat := map[string]interface{}{ "a": []interface{}{30, 20, "xxd"}, } scheme1 := map[string]interface{}{ "a": map[string]interface{}{ "type": "string", "slice": true, "allOrNothing": true, "must": true, }, } ex, err := sanitize.New(scheme1) if err != nil { t.Fatal(err) } _, err = ex.Extract(dat) if err == nil { t.Fatal() } }
func TestIgnore(t *testing.T) { dat := map[string]interface{}{ "a": "asdsadasd", "b": "asdsad", "c": "Hello.", } scheme1 := map[string]interface{}{ "a": map[string]interface{}{ "type": "string", "ignore": true, }, "b": 1, "c": 1, } ex, err := sanitize.New(scheme1) if err != nil { t.Fatal() } val, err := ex.Extract(dat) if err != nil || len(val) != 2 { t.Fatal() } }
// Special fields in query: // parentf, sort, limit, skip, page func processQuery(hooks iface.Hooks, coll string, scheme, inp map[string]interface{}) *data { d := &data{} if inp == nil { inp = map[string]interface{}{} } intSch := map[string]interface{}{ "type": "int", } sch := map[string]interface{}{ //"parentf": 1, "sort": 1, "skip": intSch, "limit": intSch, "page": intSch, } ex, err := sanitize.New(sch) if err != nil { panic(err) } dat, err := ex.Extract(inp) if err != nil { panic(err) } for i := range sch { delete(inp, i) } mods := &Mods{} //if dat["parentf"] != nil { // d.parentField = dat["parentf"].(string) //} if dat["skip"] != nil { mods.skip = int(dat["skip"].(int64)) } if dat["limit"] != nil { mods.limit = int(dat["limit"].(int64)) } else { mods.limit = 20 } if dat["page"] != nil { page := int(dat["page"].(int64)) mods.skip = (page - 1) * mods.limit } if dat["sort"] != nil { mods.sort = []string{dat["sort"].(string)} } d.mods = mods if hooks != nil { hooks.Select("ProcessQuery").Fire(inp) // We should let the subscriber now the subject name maybe. hooks.Select(coll + "ProcessQuery").Fire(inp) } if scheme != nil && len(scheme) != 0 { ex, err = sanitize.New(scheme) if err != nil { panic(err) } hooks.Select("SanitizerMangler").Fire(ex) dat, err := ex.Extract(inp) if err != nil { panic(err) } inp = dat } d.query = toQuery(inp) return d }