Beispiel #1
0
func TestOptional(t *testing.T) {
	var np *bool
	test(t, "string", true, v.Optional(v.String(v.StrIs("a"))), "a")
	test(t, "wrong string", false, v.Optional(v.String(v.StrIs("a"))), "b")

	test(t, "nil ptr", true, v.Optional(v.String(v.StrIs("a"))), np)
}
Beispiel #2
0
// Validates config structure
func validateConfig(configMap map[string]interface{}) error {
	schema := v.Object(v.ObjKeys(v.String()),
		v.ObjValues(v.Array(v.ArrEach(v.Object(
			v.ObjKV("docker", v.Object(
				v.ObjKV("image", v.String()),
				v.ObjKV("command", v.String()),
				v.ObjKV("permissions", v.Optional(v.Object(
					v.ObjKV("cwd", v.Boolean()),
				))),
			)),
		)))),
	)
	if path, err := schema.Validate(configMap); err != nil {
		return fmt.Errorf("Invalid hop definition at %s. Error (%s)",
			path, err)
	}
	return nil
}
Beispiel #3
0
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())),
			)),
		)),
	)
}
Beispiel #4
0
func (this SimulationView) GetValidationSchema() valid.Validator {
	return valid.Object(
		valid.ObjKV("data", valid.Object(
			valid.ObjKV("pairs", valid.Array(valid.ArrEach(valid.Optional(valid.Object(
				valid.ObjKV("request", valid.Object(
					valid.ObjKV("requestType", valid.Optional(valid.String())),
					valid.ObjKV("path", valid.Optional(valid.String())),
					valid.ObjKV("method", valid.Optional(valid.String())),
					valid.ObjKV("scheme", valid.Optional(valid.String())),
					valid.ObjKV("query", valid.Optional(valid.String())),
					valid.ObjKV("body", valid.Optional(valid.String())),
					valid.ObjKV("headers", valid.Optional(valid.Object())),
				)),
				valid.ObjKV("response", valid.Object(
					valid.ObjKV("status", valid.Optional(valid.Number())),
					valid.ObjKV("body", valid.Optional(valid.String())),
					valid.ObjKV("encodedBody", valid.Optional(valid.Boolean())),
					valid.ObjKV("headers", valid.Optional(valid.Object())),
				)),
			))))),
			valid.ObjKV("globalActions", valid.Optional(valid.Object(
				valid.ObjKV("delays", valid.Array(valid.ArrEach(valid.Optional(valid.Object(
					valid.ObjKV("urlPattern", valid.Optional(valid.String())),
					valid.ObjKV("httpMethod", valid.Optional(valid.String())),
					valid.ObjKV("delay", valid.Optional(valid.Number())),
				))))),
			))),
		)),
		valid.ObjKV("meta", valid.Object(
			valid.ObjKV("schemaVersion", valid.String()),
		)),
	)
}