mehdihadeli / go-food-delivery-microservices

🍕 A practical and imaginary food delivery microservices, built with golang, domain-driven design, cqrs, event sourcing, vertical slice architecture, event-driven architecture, and the latest technologies.
MIT License
845 stars 87 forks source link

Input validation in separated file #7

Closed mehdihadeli closed 1 year ago

mehdihadeli commented 2 years ago

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.

Alan-MQ commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

mehdihadeli commented 1 year ago

You're welcome :)

Alan-MQ commented 1 year ago

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 commented 1 year ago

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.