コード例 #1
0
ファイル: or_test.go プロジェクト: vpol/govalid
func TestOr(t *testing.T) {
	test(t, "combination1", true, v.Or(v.String(v.StrIs("a")), v.String(v.StrIs("b"))), "a")
	test(t, "combination2", true, v.Or(v.String(v.StrIs("a")), v.String(v.StrIs("b"))), "b")
	test(t, "combination3", false, v.Or(v.String(v.StrIs("a")), v.String(v.StrIs("b"))), 3)

	test(t, "combination4", true, v.Or(v.String(v.StrIs("a")), v.String(v.StrIs("b")), v.String(v.StrIs("c"))), "b")

	test(t, "combination5", true, v.Or(v.String(v.StrIs("a"))), "a")
	test(t, "combination6", false, v.Or(v.String(v.StrIs("a"))), "b")

	test(t, "combination7", true, v.Or(), nil)
}
コード例 #2
0
ファイル: performance_test.go プロジェクト: vpol/govalid
func init() {
	// set up raw json data
	rawJson := []byte(`
		{
    	"status": true,
      "data": {
      	"token": "CAFED00D",
	      "debug": 69306,
      	"items": [
      	  { "url": "https://news.ycombinator.com/", "comment": "clickbaits" },
          { "url": "http://golang.org/", "comment": "some darn gophers" },
          { "url": "http://www.kickstarter.com/", "comment": "opensource projects. yeah.." }
       	],
       	"ghost2": null,
       	"meta": {
       	 "g": 1,
         "xyzzy": 0.25,
         "blöö": 0.5
       	}
      }
		}`)

	// decode json
	if err := json.Unmarshal(rawJson, &decoded); err != nil {
		log.Panic("JSON parsing failed. Err =", err)
	}

	// set up a custom validator function
	myValidatorFunc := func(data interface{}) (path string, err error) {
		path = "myValidatorFunc"

		validate, ok := data.(string)
		if !ok {
			return path, fmt.Errorf("expected string, got %v", reflect.TypeOf(data))
		}

		if validate != "CAFED00D" {
			return path, fmt.Errorf("expected CAFED00D, got %s", validate)
		}

		return "", nil
	}

	// construct the schema which is used to validate data
	schema = v.Object(
		v.ObjKV("status", v.Boolean()),
		v.ObjKV("data", v.Object(
			v.ObjKV("token", v.Function(myValidatorFunc)),
			v.ObjKV("debug", v.Number(v.NumMin(1), v.NumMax(99999))),
			v.ObjKV("items", v.Array(v.ArrEach(v.Object(
				v.ObjKV("url", v.String(v.StrMin(1))),
				v.ObjKV("comment", v.Optional(v.String())),
			)))),
			v.ObjKV("ghost", v.Optional(v.String())),
			v.ObjKV("ghost2", v.Optional(v.String())),
			v.ObjKV("meta", v.Object(
				v.ObjKeys(v.String()),
				v.ObjValues(v.Or(v.Number(v.NumMin(.01), v.NumMax(1.1)), v.String())),
			)),
		)),
	)
}