示例#1
0
// ContainerExistsValidation will validate that the specified container name is present in the current YAML.
func ContainerExistsValidation(v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
	// validates that the container exists in the root.components.containers slice

	root, ok := topStruct.Interface().(*RootConfig)
	if !ok {
		// this is an issue with the code and really should be a panic
		return true
	}

	var componentName, containerName string

	if fieldKind != reflect.String {
		// this is an issue with the code and really should be a panic
		return true
	}

	containerName = field.String()

	if param != "" {
		componentField, componentKind, ok := v.GetStructFieldOK(currentStructOrField, param)

		if !ok || componentKind != reflect.String {
			// this is an issue with the code and really should be a panic
			return true
		}

		componentName = componentField.String()
	} else {
		parts := strings.SplitN(containerName, ",", 2)

		if len(parts) < 2 {
			// let "componentcontainer" validation handle this case
			return true
		}

		componentName = parts[0]
		containerName = parts[1]
	}

	if !componentExists(componentName, root) {
		// let "componentexists" validation handle this case
		return true
	}

	return containerExists(componentName, containerName, root)
}
示例#2
0
// RegisterValidations will register all known validation for the libyaml project.
func RegisterValidations(v *validator.Validate) error {
	if err := v.RegisterValidation("configitemtype", ConfigItemTypeValidation); err != nil {
		return err
	}
	if err := v.RegisterValidation("configitemwhen", ConfigItemWhenValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("apiversion", SemverValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("semver", SemverValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("semverrange", SemverRangeValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("hastarget", HasTargetValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("componentexists", ComponentExistsValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("containerexists", ContainerExistsValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("componentcontainer", ComponentContainerFormatValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("absolutepath", IsAbsolutePathValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("volumeoptions", VolumeOptionsValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("isempty", IsEmptyValidation); err != nil {
		return err
	}

	// will handle this in vendor web. this prevents panic from validator.v8 library
	if err := v.RegisterValidation("integrationexists", NoopValidation); err != nil {
		return err
	}

	// will handle this in vendor web. this prevents panic from validator.v8 library
	if err := v.RegisterValidation("externalregistryexists", NoopValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("bytes", IsBytesValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("bool", IsBoolValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("tcpport", IsTCPUDPPortValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("udpport", IsTCPUDPPortValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("graphiteretention", GraphiteRetentionFormatValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("graphiteaggregation", GraphiteAggregationFormatValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("monitorlabelscale", MonitorLabelScaleValidation); err != nil {
		return err
	}

	if err := v.RegisterValidation("fingerprint", Fingerprint); err != nil {
		return err
	}

	if err := v.RegisterValidation("shellalias", ShellAlias); err != nil {
		return err
	}

	if err := v.RegisterValidation("containernameexists", ContainerNameExists); err != nil {
		return err
	}

	if err := v.RegisterValidation("containernameunique", ContainerNameUnique); err != nil {
		return err
	}

	if err := v.RegisterValidation("clusterinstancefalse", ClusterInstanceFalse); err != nil {
		return err
	}

	if err := v.RegisterValidation("requiressubscription", RequiresSubscription); err != nil {
		return err
	}

	return nil
}