<commit_before>package pb

import (
	"fmt"

	"github.com/fagongzi/gateway/pkg/pb/metapb"
)

// ValidateRouting validate routing
func ValidateRouting(value *metapb.Routing) error {
	if value.ClusterID == 0 {
		return fmt.Errorf("missing cluster")
	}

	if value.TrafficRate <= 0 || value.TrafficRate > 100 {
		return fmt.Errorf("error traffic rate: %d", value.TrafficRate)
	}

	return nil
}

// ValidateCluster validate cluster
func ValidateCluster(value *metapb.Cluster) error {
	if value.Name == "" {
		return fmt.Errorf("missing name")
	}

	return nil
}

// ValidateServer validate server
func ValidateServer(value *metapb.Server) error {
	if value.Addr == "" {
		return fmt.Errorf("missing server address")
	}

	if value.MaxQPS == 0 {
		return fmt.Errorf("missing server max qps")
	}

	return nil
}

// ValidateAPI validate api
func ValidateAPI(value *metapb.API) error {
	if value.Name == "" {
		return fmt.Errorf("missing api name")
	}

	if value.URLPattern == "" {
		return fmt.Errorf("missing url pattern")
	}

	if value.Method == "" {
		return fmt.Errorf("missing http method")
	}

	return nil
}<commit_msg>fix: crash if api url regexp compile failed<commit_after>package pb

import (
	"fmt"
	"regexp"

	"github.com/fagongzi/gateway/pkg/pb/metapb"
)

// ValidateRouting validate routing
func ValidateRouting(value *metapb.Routing) error {
	if value.ClusterID == 0 {
		return fmt.Errorf("missing cluster")
	}

	if value.TrafficRate <= 0 || value.TrafficRate > 100 {
		return fmt.Errorf("error traffic rate: %d", value.TrafficRate)
	}

	return nil
}

// ValidateCluster validate cluster
func ValidateCluster(value *metapb.Cluster) error {
	if value.Name == "" {
		return fmt.Errorf("missing name")
	}

	return nil
}

// ValidateServer validate server
func ValidateServer(value *metapb.Server) error {
	if value.Addr == "" {
		return fmt.Errorf("missing server address")
	}

	if value.MaxQPS == 0 {
		return fmt.Errorf("missing server max qps")
	}

	return nil
}

// ValidateAPI validate api
func ValidateAPI(value *metapb.API) error {
	if value.Name == "" {
		return fmt.Errorf("missing api name")
	}

	if value.URLPattern == "" {
		return fmt.Errorf("missing url pattern")
	}

	if value.Method == "" {
		return fmt.Errorf("missing http method")
	}

	if _, err := regexp.Compile(value.URLPattern); err != nil {
		return err
	}

	return nil
}
{'fraction_non_alphanumeric': 0.13000852514919012, 'fraction_numerical': 0.005115089514066497, 'mean_word_length': 3.8592132505175982, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1737078', 'n_tokens_mistral': 820, 'n_tokens_neox': 791, 'n_words': 205}