Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input validation in separated file #7

Closed
mehdihadeli opened this issue Jun 27, 2022 · 6 comments
Closed

Input validation in separated file #7

mehdihadeli opened this issue Jun 27, 2022 · 6 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@mehdihadeli
Copy link
Owner

mehdihadeli commented Jun 27, 2022

Currently, I use tags for validating inputs with go-playground/validator package, maybe it's cleaner that we move them to validator.go file in each slice.

@mehdihadeli mehdihadeli added the enhancement New feature or request label Jun 27, 2022
@mehdihadeli mehdihadeli self-assigned this Jun 27, 2022
@mehdihadeli mehdihadeli added the help wanted Extra attention is needed label Aug 26, 2023
@Alan-MQ
Copy link
Contributor

Alan-MQ commented Aug 29, 2023

u mean use custom validator.go file to replace all the validator package right?
btw, I'm new to this project and really interested to help, do you think this issue is great for me to start contribute?

@mehdihadeli
Copy link
Owner Author

Maybe we could put it in same command file and replacing validate attribute and validator pkg with ozzo, for example for create_product_command it will be something like this:

type CreateProduct struct {
    ProductID   uuid.UUID 
    Name        string   
    Description string    
    Price       float64   
    CreatedAt   time.Time 
}

func NewCreateProduct(name string, description string, price float64) (*CreateProduct, error) {
    command := &CreateProduct{
        ProductID:   uuid.NewV4(),
        Name:        name,
        Description: description,
        Price:       price,
        CreatedAt:   time.Now(),
    }
    err := validator.Validate(command)
    if err != nil {
        return nil, err
    }

    return command, nil
}

// using ozzo
func (c *CreateProduct) Validate() error {
    return validation.ValidateStruct(&c,
        validation.Field(&c.ProductID, validation.Required),
        validation.Field(&c.Name, validation.Required, validation.Length(3, 255)),
        ///...
    )
}

If you like, feel free to contribute.

@Alan-MQ
Copy link
Contributor

Alan-MQ commented Aug 29, 2023

thank you for the tips, let me see what I can do .

@mehdihadeli
Copy link
Owner Author

mehdihadeli commented Aug 29, 2023

You're welcome :)

@Alan-MQ
Copy link
Contributor

Alan-MQ commented Aug 29, 2023

I've already made some changes on command/query level, could you please review them before I go further? btw it seems like I don't have permission to create new branch/ create PR. @mehdihadeli

@mehdihadeli
Copy link
Owner Author

mehdihadeli commented Aug 29, 2023

Yes, sure. Did you fork the project? You should put your changes in your fork, and after that you can send a pull request to my main branch. Please follow contribution guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants