46 lines
1.5 KiB
Go
46 lines
1.5 KiB
Go
package validation
|
|
|
|
import (
|
|
"GoMembership/internal/models"
|
|
"GoMembership/internal/repositories"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
// ValidateNewSubscription validates a new subscription model being created
|
|
func ValidateSubscription(sl validator.StructLevel) {
|
|
subscription := sl.Current().Interface().(models.Subscription)
|
|
|
|
if subscription.Name == "" {
|
|
sl.ReportError(subscription.Name, "Name", "name", "required", "")
|
|
}
|
|
if sl.Parent().Type().Name() == "" {
|
|
// This is modifying a subscription directly
|
|
if subscription.Details == "" {
|
|
sl.ReportError(subscription.Details, "Details", "details", "required", "")
|
|
}
|
|
|
|
if subscription.MonthlyFee < 0 {
|
|
sl.ReportError(subscription.MonthlyFee, "MonthlyFee", "monthly_fee", "gte", "0")
|
|
}
|
|
|
|
if subscription.HourlyRate < 0 {
|
|
sl.ReportError(subscription.HourlyRate, "HourlyRate", "hourly_rate", "gte", "0")
|
|
}
|
|
|
|
if subscription.IncludedPerYear < 0 {
|
|
sl.ReportError(subscription.IncludedPerYear, "IncludedPerYear", "included_hours_per_year", "gte", "0")
|
|
}
|
|
|
|
if subscription.IncludedPerMonth < 0 {
|
|
sl.ReportError(subscription.IncludedPerMonth, "IncludedPerMonth", "included_hours_per_month", "gte", "0")
|
|
}
|
|
} else {
|
|
// This is a nested probably user struct. We are only checking if the model exists
|
|
existingSubscription, err := repositories.GetSubscriptionByName(&subscription.Name)
|
|
if err != nil || existingSubscription == nil {
|
|
sl.ReportError(subscription.Name, "subscription.name", "name", "duplicate", "")
|
|
}
|
|
}
|
|
}
|