Skip to content

Custom Validation#259

Draft
jfmario wants to merge 9 commits intolivebud:mainfrom
jfmario:issue/171
Draft

Custom Validation#259
jfmario wants to merge 9 commits intolivebud:mainfrom
jfmario:issue/171

Conversation

@jfmario
Copy link
Copy Markdown
Contributor

@jfmario jfmario commented Aug 26, 2022

This implements validation by calling Validate when appropriate. There are still several other things to do, so this should be a draft. I am curious what you think of this approach so far.

Copy link
Copy Markdown
Contributor

@matthewmueller matthewmueller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for kicking this off! I've just reviewed. I really like where you started with this PR because it would remain generic while still allowing you to stick an existing validator library in there and do something like:

type Input struct {
  name string `validate:len>5`
}

var validate = validator.New()

func(i *Input) Validate() error {
  return validate.Struct(i)
}

(not sure on exact syntax)

Comment thread framework/controller/controller.gotext Outdated
if err := in.Validate(); err != nil {
return &response.Format{
{{- if ne $action.Method "GET" }}
HTML: response.Status(http.StatusSeeOther).RedirectBack(httpRequest.URL.Path),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The big question is going to be what happens with the error message? Right now if a form fails, there's no feedback sent back to the user.

Flash messages are typically the solution to this where you store a value in a cookie that gets removed after it's pulled out. I think the ultimate solution is to have the ability to add custom flash messages via the session, but if there's any temporary solution to get this working where you set the cookie, pull it out and pass to the view, I'd be open to pursuing that. Here's what I've thought about so far with sessions: #56.

Otherwise maybe we only enable this for JSON? Does your use case fit with that limitation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I think flash messages should be the behavior here. I'm thinking to add some other stuff to this PR but then put in on pause until we have sessions and something like flash messages.

Copy link
Copy Markdown
Contributor

@matthewmueller matthewmueller Aug 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, sounds good! I'm going to get back to feature development very soon. Just trying to wrap up generator caching to allow for running more expensive generators (e.g. generators that call go run), so hopefully we'll get sessions soon!

Comment thread framework/controller/controller.gotext Outdated
@jfmario
Copy link
Copy Markdown
Contributor Author

jfmario commented Aug 26, 2022

Ok, I'll clean this up.

Also, I just re-read the discussion here: #15

Validate() (or Valid()) also has to work on types besides structs, so I'll re-work this a bit.

@jfmario jfmario changed the title Issue/171 Custom Validation Aug 26, 2022
@jfmario
Copy link
Copy Markdown
Contributor Author

jfmario commented Sep 2, 2022

I changed the name to Valid() but this is still a draft and depends on sessions, I think.

@matthewmueller matthewmueller marked this pull request as draft September 3, 2022 04:43
@matthewmueller
Copy link
Copy Markdown
Contributor

Yah, might be best to wait for sessions. In the meantime, you can always add the validate function to the method body.

I'll try and get sessions added soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants