コード例 #1
0
ファイル: users.go プロジェクト: maqiv/gohackernews
// validateParams these parameters conform to AcceptedParams, and pass validation
func validateParams(unsafeParams map[string]string) error {

	// Now check params are as we expect

	if len(unsafeParams["name"]) > 0 {
		err := validate.Length(unsafeParams["name"], 1, 100)
		if err != nil {
			return router.BadRequestError(err, "Name too short", "Your name must be between 1 and 100 characters long.")
		}
	}

	if len(unsafeParams["email"]) > 0 {
		err := validate.Length(unsafeParams["email"], 3, 100)
		if err != nil {
			return router.BadRequestError(err, "Email too short", "Your email must be between 3 and 100 characters long.")
		}
	}

	// Password may be blank
	if len(unsafeParams["password"]) > 0 {
		// Report error for length between 0 and 5 chars
		err := validate.Length(unsafeParams["password"], 5, 100)
		if err != nil {
			return router.BadRequestError(err, "Password too short", "Your password must be at least 5 characters long.")
		}

		ep, err := auth.HashPassword(unsafeParams["password"])
		if err != nil {
			return err
		}
		unsafeParams["encrypted_password"] = ep

	}

	// Delete password param
	delete(unsafeParams, "password")

	return nil
}
コード例 #2
0
ファイル: stories.go プロジェクト: ibmendoza/gohackernews
// validateParams checks these params pass validation checks
// TODO: reconsider best interface for this - don't like the bool
func validateParams(params map[string]string, checkAll bool) error {

	if checkAll || len(params["name"]) > 0 {
		err := validate.Length(params["name"], 2, 300)
		if err != nil {
			return router.BadRequestError(err, "Invalid Name", "The name must be over 2 characters")
		}
	}

	if checkAll || len(params["url"]) > 0 {

		err := validate.Length(params["url"], 5, 1000)
		if err != nil {
			return router.BadRequestError(err, "Invalid URL", "The URL must be over 5 characters")
		}

		if !strings.HasPrefix(params["url"], "http://") && !strings.HasPrefix(params["url"], "https://") {
			return router.BadRequestError(nil, "Invalid URL", "The URL must have scheme https:// or http://")
		}

	}

	return nil
}