func NewTemplateParams(body io.ReadCloser) (TemplateParams, error) {
	defer body.Close()

	var template TemplateParams
	validator := valiant.NewValidator(body)

	err := validator.Validate(&template)
	if err != nil {
		switch err.(type) {
		case valiant.RequiredFieldError:
			return template, webutil.ValidationError([]string{err.Error()})
		default:
			return template, webutil.ParseError{}
		}
	}

	if template.Metadata == nil {
		template.Metadata = json.RawMessage("{}")
	}

	err = template.validateSyntax()
	if err != nil {
		return TemplateParams{}, err
	}

	template.setDefaults()

	return template, nil
}
func (h UpdatePreferencesHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, context stack.Context) {
	database := context.Get("database").(DatabaseInterface)
	connection := database.Connection()

	token := context.Get("token").(*jwt.Token)

	if _, ok := token.Claims["user_id"]; !ok {
		h.errorWriter.Write(w, webutil.MissingUserTokenError("Missing user_id from token claims."))
		return
	}

	userID := token.Claims["user_id"].(string)

	builder := services.NewPreferencesBuilder()
	validator := valiant.NewValidator(req.Body)
	err := validator.Validate(&builder)
	if err != nil {
		h.errorWriter.Write(w, webutil.ValidationError([]string{err.Error()}))
		return
	}

	preferences, err := builder.ToPreferences()
	if err != nil {
		h.errorWriter.Write(w, webutil.ValidationError([]string{err.Error()}))
		return
	}

	transaction := connection.Transaction()
	transaction.Begin()
	err = h.preferences.Update(transaction, preferences, builder.GlobalUnsubscribe, userID)
	if err != nil {
		transaction.Rollback()

		switch err.(type) {
		case services.MissingKindOrClientError, services.CriticalKindError:
			h.errorWriter.Write(w, webutil.ValidationError([]string{err.Error()}))
		default:
			h.errorWriter.Write(w, err)
		}
		return
	}

	err = transaction.Commit()
	if err != nil {
		h.errorWriter.Write(w, models.NewTransactionCommitError(err.Error()))
		return
	}

	w.WriteHeader(http.StatusNoContent)
}
Beispiel #3
0
func NewNotificationParams(body io.Reader) (NotificationUpdateParams, error) {
	var params NotificationUpdateParams

	validator := valiant.NewValidator(body)
	err := validator.Validate(&params)
	if err != nil {
		switch err.(type) {
		case valiant.RequiredFieldError:
			return params, webutil.ValidationError([]string{err.Error()})
		default:
			return params, webutil.ParseError{}
		}
	}
	return params, nil
}
func (h UpdateUserPreferencesHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, context stack.Context) {
	database := context.Get("database").(DatabaseInterface)
	connection := database.Connection()

	userGUID := regexp.MustCompile(".*/user_preferences/(.*)").FindStringSubmatch(req.URL.Path)[1]

	builder := services.NewPreferencesBuilder()
	validator := valiant.NewValidator(req.Body)
	err := validator.Validate(&builder)
	if err != nil {
		h.errorWriter.Write(w, webutil.ValidationError([]string{err.Error()}))
		return
	}

	preferences, err := builder.ToPreferences()
	if err != nil {
		h.errorWriter.Write(w, err)
		return
	}

	transaction := connection.Transaction()
	transaction.Begin()
	err = h.preferences.Update(transaction, preferences, builder.GlobalUnsubscribe, userGUID)
	if err != nil {
		transaction.Rollback()

		switch err.(type) {
		case services.MissingKindOrClientError, services.CriticalKindError:
			h.errorWriter.Write(w, webutil.ValidationError([]string{err.Error()}))
		default:
			h.errorWriter.Write(w, err)
		}
		return
	}

	err = transaction.Commit()
	if err != nil {
		h.errorWriter.Write(w, models.NewTransactionCommitError(err.Error()))
		return
	}

	w.WriteHeader(http.StatusNoContent)
}
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Validate", func() {
	Context("when the data is one level deep", func() {
		It("succeeds when the json is valid", func() {
			data := strings.NewReader(`{"name":"Boshy", "email": true}`)

			type Person struct {
				Name  string `json:"name"    validate-required:"true"`
				Email bool   `json:"email"   validate-required:"false"`
			}

			var someone Person
			validator := valiant.NewValidator(data)
			err := validator.Validate(&someone)
			Expect(err).ToNot(HaveOccurred())
		})

		It("returns an error when the json is missing a required field", func() {
			data := strings.NewReader(`{"email": true}`)

			type Person struct {
				Name  string `json:"name"    validate-required:"true"`
				Email bool   `json:"email"   validate-required:"false"`
			}

			var someone Person

			validator := valiant.NewValidator(data)