input

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrValidation    = apierror.NewAPIError("VALIDATION_ERROR")
	ErrInputDecoding = apierror.NewAPIError("ERROR_DECODING_INPUT")
	ErrInvalidInput  = apierror.NewAPIError("INVALID_INPUT")
)

Commmon input errors.

Functions

This section is empty.

Types

type APIField

type APIField struct {
	APIName  string
	Alias    string
	DBColumn string
	Required bool
	Default  any
	Source   string
	Validate []string
	Nested   APIFields
	Type     string
}

APIField holds the core mapping information.

func (APIField) WithAPIName

func (field APIField) WithAPIName(apiName string) APIField

WithAPIName returns a copy of APIField with APIName set.

Parameters:

  • apiName: The API name.

Returns:

  • APIField: The new APIField.

func (APIField) WithAlias

func (field APIField) WithAlias(alias string) APIField

WithAlias returns a copy of APIField with Alias set.

Parameters:

  • alias: The alias.

Returns:

  • APIField: The new APIField.

func (APIField) WithDBColumn

func (field APIField) WithDBColumn(dbColumn string) APIField

WithDBColumn returns a copy of APIField with DBColumn set.

Parameters:

  • dbColumn: The DB column.

Returns:

  • APIField: The new APIField.

func (APIField) WithDefault

func (field APIField) WithDefault(defaultValue any) APIField

WithDefault returns a copy of APIField with Default set.

Parameters:

  • defaultValue: The default value.

Returns:

  • APIField: The new APIField.

func (APIField) WithNested

func (field APIField) WithNested(nested APIFields) APIField

Nested returns a copy of APIField with Nested set.

Parameters:

  • nested: The nested APIFields.

Returns:

  • APIField: The new APIField.

func (APIField) WithRequired

func (field APIField) WithRequired(required bool) APIField

WithRequired returns a copy of APIField with Required set.

Parameters:

  • required: The required flag.

Returns:

  • APIField: The new APIField.

func (APIField) WithSource

func (field APIField) WithSource(source string) APIField

WithSource returns a copy of APIField with Source set.

Parameters:

  • source: The source.

Returns:

  • APIField: The new APIField.

func (APIField) WithType

func (field APIField) WithType(typ string) APIField

WithType returns a copy of APIField with Type set.

Parameters:

  • typ: The type.

Returns:

  • APIField: The new APIField.

func (APIField) WithValidate

func (field APIField) WithValidate(validate []string) APIField

WithValidate returns a copy of APIField with Validate set.

Parameters:

  • validate: The validation rules.

Returns:

  • APIField: The new APIField.

type APIFields

type APIFields []APIField

APIFields is a slice of APIField.

func NewAPIFields

func NewAPIFields(fields ...APIField) APIFields

NewAPIFields returns a new APIFields.

Parameters:

  • fields: The fields.

Returns:

  • APIFields: The new APIFields.

func (APIFields) GetAPIField

func (a APIFields) GetAPIField(field string) (APIField, error)

GetAPIField looks up a single field definition by its API name.

Parameters:

  • field: The field name.

Returns:

  • APIField: The field definition.
  • error: An error if the field is not found.

func (APIFields) GetAPIFields

func (a APIFields) GetAPIFields(fields []string) (APIFields, error)

GetAPIFields looks up multiple field definitions by their API names.

Parameters:

  • fields: The field names.

Returns:

  • APIFields: The field definitions.
  • error: An error if a field is not found.

func (APIFields) MustGetAPIField

func (a APIFields) MustGetAPIField(field string) APIField

MustGetAPIField looks up a single field definition by its API name. It panics if the field is not found.

Parameters:

  • field: The field name.

Returns:

  • APIField: The field definition.

func (APIFields) WithFields

func (a APIFields) WithFields(fields ...APIField) APIFields

WithFields returns a copy of APIFields with fields appended.

Parameters:

  • fields: The fields to append.

Returns:

  • APIFields: The new APIFields.

type ErrValidationData

type ErrValidationData struct {
	Errors []FieldError `json:"errors"`
}

ErrValidationData contains a list of field-level validation errors

type FieldError

type FieldError struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

FieldError represents a field-level validation error

type MapInputHandler

type MapInputHandler[Input any] struct {
	// contains filtered or unexported fields
}

MapInputHandler handles the input of a request as a map.

func NewMapInputHandler

func NewMapInputHandler[Input any](
	apiFields APIFields,
	conversionMap map[string]func(any) any,
	customRules map[string]func(any) error,
	inputFactoryFn func() *Input,
) *MapInputHandler[Input]

NewMapInputHandler creates a new MapInputHandler.

Parameters:

  • apiFields: The APIFields to use for validation.
  • conversionMap: A map of conversion functions for fields.
  • customRules: A map of custom validation rules for fields.
  • inputFactoryFn: A function that returns a new Input.

Returns:

  • *MapInputHandler: The new MapInputHandler.

func (*MapInputHandler[Input]) Handle

func (h *MapInputHandler[Input]) Handle(
	w http.ResponseWriter, r *http.Request,
) (*Input, error)

Handle processes the request input by creating a map presentation from it and validating it.

Parameters:

  • w: The HTTP response writer.
  • r: The HTTP request.

Returns:

  • *Input: The map presentation of the input.
  • error: Any error that occurred during processing.

func (*MapInputHandler[Input]) MustValidateAPIFields

func (h *MapInputHandler[Input]) MustValidateAPIFields() *MapInputHandler[Input]

MustValidateAPIFields validates the provided APIFields. It panics if an error occurs.

Returns:

  • *MapInputHandler: The MapInputHandler.

func (*MapInputHandler[Input]) ValidateAPIFields

func (h *MapInputHandler[Input]) ValidateAPIFields() (*MapInputHandler[Input], error)

ValidateAPIFields validates the provided APIFields. It returns the MapInputHandler if they are valid.

Returns:

  • *MapInputHandler: The MapInputHandler.
  • error: Any error that occurred during validation.

Jump to

Keyboard shortcuts

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