package validator

import (
	"regexp"

	"github.com/fiskerinc/cloud-services/pkg/logger"
	"github.com/go-playground/validator/v10"
	"github.com/pkg/errors"
)

func validateCertSerial(fl validator.FieldLevel) bool {
	ok, err := ValidateCertSerialSimple(fl.Field().String())
	if err != nil {
		logger.Err(err).Msg("Unable to validate certificate serial number")

	}
	return ok
}

func ValidateCertSerialSimple(serial string) (bool, error) {
	matched, err := regexp.Match(`^([a-zA-Z0-9]{2}[:-]{1}){18,19}[a-zA-Z0-9]{2}$`, []byte(serial))
	if err != nil {
		return matched, errors.WithStack(err)
	}
	return matched, nil
}
