package validators import ( "fmt" "strings" "github.com/go-playground/validator/v10" ) func errorMessage(fe validator.FieldError) string { prefix := "validation-error:" switch fe.Tag() { case "required": return fmt.Sprintf("%s %s is required", prefix, fe.Field()) case "virt_type": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(virtTypeValues)) case "vm_status": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(vmStatusValues)) case "cpu_units": return fmt.Sprintf("%s %s must be between %s", prefix, fe.Field(), joinRange(cpuUnitsRange)) case "cpu_limit": return fmt.Sprintf("%s %s must be between %s", prefix, fe.Field(), joinRange(cpuLimitRange)) case "video_ram": return fmt.Sprintf("%s %s must be between %s", prefix, fe.Field(), joinRange(videoRamRange)) case "io_priority": return fmt.Sprintf("%s %s must be between %s", prefix, fe.Field(), joinRange(ioPriorityRange)) case "mem_guarantee_size": return fmt.Sprintf("%s %s must be between %s", prefix, fe.Field(), joinRange(memGuaranteeSizeRange)) case "ha_status": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(haStatusValues)) case "visibility": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(visibilityValues)) case "metrics_period": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(metricPeriodValues)) case "network_type": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(networkTypeValues)) case "image_type": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(imageTypeValues)) case "image_status": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(imageStatusValues)) case "extstorage_status": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(extStorageStatusValues)) case "extstorage_type": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(extStorageTypeValues)) case "maintenance_status": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(nodeMaintenanceStatusValues)) case "agent_update_status": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(nodeAgentStatusValues)) case "node_status_in_ha_cluster": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(nodeStatusInHaClusterValues)) case "affinity": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(affinityRuleValues)) case "guest_os": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(guestOSValues)) case "guest_os_version": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(guestOSVersionValues)) case "auto_start": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(autoStartValues)) case "auto_stop": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(autoStopValues)) case "vnc_mode": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(vncModeValues)) case "chipset": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(chipsetValues)) case "clock_offset": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(clockOffsetValues)) case "template_status": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), joinValues(templateStatusValues)) case "is_bool": return fmt.Sprintf("%s %s must be bool type", prefix, fe.Field()) case "url": return fmt.Sprintf("%s %s: unexpected URL format", prefix, fe.Field()) } return fe.Error() } func joinValues(values []string) string { return strings.Join(values, ", ") } func joinRange(lim []int64) string { return fmt.Sprintf("%d and %d", lim[0], lim[1]) }