func (in *NetworkChaosSpec) Validate(root interface{}, path *field.Path) field.ErrorList {
	allErrs := field.ErrorList{}
	if in.Action == PartitionAction {
		return nil
	}
	if (in.Direction == From || in.Direction == Both) &&
		in.ExternalTargets != nil && in.Action != PartitionAction {
		allErrs = append(allErrs,
			field.Invalid(path.Child("direction"), in.Direction,
				"external targets cannot be used with `from` and `both` direction in netem action yet"))
	}
	if (in.Direction == From || in.Direction == Both) && in.Target == nil {
		if in.Action != PartitionAction {
			allErrs = append(allErrs,
				field.Invalid(path.Child("direction"), in.Direction,
					"`from` and `both` direction cannot be used when targets is empty in netem action"))
		} else if in.ExternalTargets == nil {
			allErrs = append(allErrs,
				field.Invalid(path.Child("direction"), in.Direction,
					"`from` and `both` direction cannot be used when targets and external targets are both empty"))
		}
	}
	return allErrs
}
ValidateTargets validates externalTargets and Targets
package v1alpha1
import (
	"errors"
	"fmt"
	"reflect"
	"strconv"
	"strings"
	"k8s.io/apimachinery/pkg/util/validation/field"
	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1/genericwebhook"
)
const (
	DefaultJitter = "0ms"
	DefaultCorrelation = "0"
)
func (in *Direction) Default(root interface{}, field *reflect.StructField) {
	if *in == "" {
		*in = To
	}
}
type Rate string
func (in *Rate) Validate(root interface{}, path *field.Path) field.ErrorList {
	allErrs := field.ErrorList{}
	_, err := ConvertUnitToBytes(string(*in))
	if err != nil {
		allErrs = append(allErrs,
			field.Invalid(path, in,
				fmt.Sprintf("parse rate field error:%s", err)))
	}
	return allErrs
}
func ConvertUnitToBytes(nu string) (uint64, error) {
	s := strings.ToLower(strings.TrimSpace(nu))
	for i, u := range []string{"tbps", "gbps", "mbps", "kbps", "bps"} {
		if strings.HasSuffix(s, u) {
			ts := strings.TrimSuffix(s, u)
			s := strings.TrimSpace(ts)
			n, err := strconv.ParseUint(s, 10, 64)
			if err != nil {
				return 0, err
			}
			for j := 4 - i; j > 0; j-- {
				n = n * 1024
			}
			return n, nil
		}
	}
	return 0, errors.New("invalid unit")
}