validators

package
v0.14.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsUUID

func IsUUID() validator.String

func StringIsDuration added in v0.14.3

func StringIsDuration(i any, k string) (warnings []string, errors []error)

StringIsDuration is a SchemaValidateFunc which tests to make sure the supplied string is valid duration.

func StringIsElasticDuration added in v0.14.3

func StringIsElasticDuration(i any, k string) (warnings []string, errors []error)

StringIsElasticDuration is a SchemaValidateFunc which tests to make sure the supplied string is valid duration using Elastic time units: d, h, m, s, ms, micros, nanos. (see the [API conventions time units documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/api-conventions.html#time-units) for more details)

Types

type Condition added in v0.14.3

type Condition struct {
	// contains filtered or unexported fields
}

condition represents a validation rule that enforces conditional requirements based on the value of a dependent field. It contains either a static path or a path expression to the field that this condition depends on, and a list of allowed values for that field. When the dependent field matches one of the allowed values, additional validation logic can be applied to the current field. Use dependentPath for absolute paths, or dependentPathExpression for relative paths.

func AllowedIfDependentPathEquals added in v0.11.19

func AllowedIfDependentPathEquals(dependentPath path.Path, requiredValue string) Condition

AllowedIfDependentPathEquals returns a condition that allows a field to be set only if the value at the specified dependent path equals the required value. This is a convenience function that wraps AllowedIfDependentPathOneOf with a single value slice.

Parameters:

  • dependentPath: The path to the field whose value determines if this field is allowed
  • requiredValue: The exact string value that the dependent field must equal

Returns:

  • condition: A validation condition that enforces the dependency rule

func AllowedIfDependentPathExpressionOneOf added in v0.14.0

func AllowedIfDependentPathExpressionOneOf(dependentPathExpression path.Expression, allowedValues []string) Condition

AllowedIfDependentPathExpressionOneOf creates a validation condition that allows the current attribute to be set only when a dependent attribute matched by the path expression has one of the allowed values. This uses a relative path expression that is resolved relative to the field being validated.

Parameters:

  • dependentPathExpression: The path expression to match the dependent attribute relative to the current field
  • allowedValues: A slice of string values that the dependent attribute must match

Returns:

  • condition: A validation condition that can be used with conditional validators

Example:

// Only allow "ssl_cert" to be set when a sibling "protocol" field is "https"
AllowedIfDependentPathExpressionOneOf(path.MatchRelative().AtParent().AtName("protocol"), []string{"https"})

func AllowedIfDependentPathOneOf added in v0.11.19

func AllowedIfDependentPathOneOf(dependentPath path.Path, allowedValues []string) Condition

AllowedIfDependentPathOneOf creates a validation condition that allows the current attribute to be set only when a dependent attribute at the specified path has one of the allowed values.

Parameters:

  • dependentPath: The path to the attribute that this validation depends on
  • allowedValues: A slice of string values that the dependent attribute must match

Returns:

  • condition: A validation condition that can be used with conditional validators

Example:

// Only allow "ssl_cert" to be set when "protocol" is "https"
AllowedIfDependentPathOneOf(path.Root("protocol"), []string{"https"})

func DependantPathOneOf added in v0.11.19

func DependantPathOneOf(dependentPath path.Path, allowedValues []string) Condition

DependantPathOneOf creates a condition that validates a dependent path's value is one of the allowed values. It returns a condition that checks if the value at dependentPath matches any of the provided allowedValues. If the dependent field does not have an allowed value, it generates a diagnostic error indicating which values are permitted and what the current value is.

Parameters:

  • dependentPath: The path to the attribute that must have one of the allowed values
  • allowedValues: A slice of strings representing the valid values for the dependent path

Returns:

  • condition: A condition struct that can be used for validation

func ForbiddenIfDependentPathExpressionOneOf added in v0.14.0

func ForbiddenIfDependentPathExpressionOneOf(dependentPathExpression path.Expression, allowedValues []string) Condition

ForbiddenIfDependentPathExpressionOneOf creates a validation condition that forbids setting a value when a dependent field matched by the path expression has one of the specified allowed values. This uses a relative path expression that is resolved relative to the field being validated.

Parameters:

  • dependentPathExpression: The path expression to match the dependent attribute relative to the current field
  • allowedValues: A slice of string values that, when matched by the dependent field, will trigger the forbidden condition

Returns:

  • condition: A validation condition that will generate an error if the current field is set while the dependent field matches any of the allowed values

Example usage:

validator := ForbiddenIfDependentPathExpressionOneOf(
  path.MatchRelative().AtParent().AtName("type"),
  []string{"basic", "simple"},
)
// This will prevent setting the current attribute when sibling "type" equals "basic" or "simple"

func ForbiddenIfDependentPathOneOf added in v0.11.19

func ForbiddenIfDependentPathOneOf(dependentPath path.Path, allowedValues []string) Condition

ForbiddenIfDependentPathOneOf creates a validation condition that forbids setting a value when a dependent field matches one of the specified allowed values.

This validator is useful for creating mutually exclusive configuration scenarios where certain attributes should not be set when another attribute has specific values.

Parameters:

  • dependentPath: The path to the field whose value determines the validation behavior
  • allowedValues: A slice of string values that, when matched by the dependent field, will trigger the forbidden condition

Returns:

  • condition: A validation condition that will generate an error if the current field is set while the dependent field matches any of the allowed values

Example usage:

validator := ForbiddenIfDependentPathOneOf(
  path.Root("type"),
  []string{"basic", "simple"},
)
// This will prevent setting the current attribute when "type" equals "basic" or "simple"

func RequiredIfDependentPathEquals added in v0.11.19

func RequiredIfDependentPathEquals(dependentPath path.Path, requiredValue string) Condition

RequiredIfDependentPathEquals returns a condition that makes a field required when the value at the specified dependent path equals the given required value. This is a convenience function that wraps RequiredIfDependentPathOneOf with a single value slice.

Parameters:

  • dependentPath: The path to the field whose value will be checked
  • requiredValue: The value that, when present at dependentPath, makes this field required

Returns:

  • condition: A validation condition function

func RequiredIfDependentPathExpressionOneOf added in v0.14.0

func RequiredIfDependentPathExpressionOneOf(dependentPathExpression path.Expression, allowedValues []string) Condition

RequiredIfDependentPathExpressionOneOf returns a condition that validates an attribute is required when a dependent attribute matched by the path expression has a value matching one of the specified allowed values. This uses a relative path expression that is resolved relative to the field being validated.

Parameters:

  • dependentPathExpression: The path expression to match the dependent attribute relative to the current field
  • allowedValues: A slice of string values that trigger the requirement when matched

Returns:

  • condition: A validation condition that enforces the requirement rule

Example usage:

validator := RequiredIfDependentPathExpressionOneOf(
  path.MatchRelative().AtParent().AtName("type"),
  []string{"custom", "advanced"},
)
// This would require the current attribute when sibling "type" equals "custom" or "advanced"

func RequiredIfDependentPathOneOf added in v0.11.19

func RequiredIfDependentPathOneOf(dependentPath path.Path, allowedValues []string) Condition

RequiredIfDependentPathOneOf returns a condition that validates an attribute is required when a dependent attribute's value matches one of the specified allowed values.

The condition checks if the dependent attribute (specified by dependentPath) has a value that is present in the allowedValues slice. If the dependent attribute matches any of the allowed values, then the attribute being validated must not be null or unknown.

Parameters:

  • dependentPath: The path to the attribute whose value determines the requirement
  • allowedValues: A slice of string values that trigger the requirement when matched

Returns:

  • condition: A validation condition that enforces the requirement rule

Example usage:

validator := RequiredIfDependentPathOneOf(
  path.Root("type"),
  []string{"custom", "advanced"},
)
// This would require the current attribute when "type" equals "custom" or "advanced"

func (Condition) Description added in v0.14.3

func (v Condition) Description(_ context.Context) string

Description describes the validation in plain text formatting.

func (Condition) MarkdownDescription added in v0.14.3

func (v Condition) MarkdownDescription(_ context.Context) string

MarkdownDescription describes the validation in Markdown formatting.

func (Condition) ValidateBool added in v0.14.4

func (v Condition) ValidateBool(ctx context.Context, request validator.BoolRequest, response *validator.BoolResponse)

func (Condition) ValidateInt64 added in v0.14.3

func (v Condition) ValidateInt64(ctx context.Context, request validator.Int64Request, response *validator.Int64Response)

func (Condition) ValidateList added in v0.14.3

func (v Condition) ValidateList(ctx context.Context, request validator.ListRequest, response *validator.ListResponse)

func (Condition) ValidateObject added in v0.14.3

func (v Condition) ValidateObject(ctx context.Context, request validator.ObjectRequest, response *validator.ObjectResponse)

func (Condition) ValidateString added in v0.14.3

func (v Condition) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL