Documentation
¶
Index ¶
- Variables
- type APIField
- func (field APIField) WithAPIName(apiName string) APIField
- func (field APIField) WithAlias(alias string) APIField
- func (field APIField) WithDBColumn(dbColumn string) APIField
- func (field APIField) WithDefault(defaultValue any) APIField
- func (field APIField) WithNested(nested APIFields) APIField
- func (field APIField) WithRequired(required bool) APIField
- func (field APIField) WithSource(source string) APIField
- func (field APIField) WithType(typ string) APIField
- func (field APIField) WithValidate(validate []string) APIField
- type APIFields
- type ErrValidationData
- type FieldError
- type MapInputHandler
Constants ¶
This section is empty.
Variables ¶
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 ¶
WithAPIName returns a copy of APIField with APIName set.
Parameters:
- apiName: The API name.
Returns:
- APIField: The new APIField.
func (APIField) WithAlias ¶
WithAlias returns a copy of APIField with Alias set.
Parameters:
- alias: The alias.
Returns:
- APIField: The new APIField.
func (APIField) WithDBColumn ¶
WithDBColumn returns a copy of APIField with DBColumn set.
Parameters:
- dbColumn: The DB column.
Returns:
- APIField: The new APIField.
func (APIField) WithDefault ¶
WithDefault returns a copy of APIField with Default set.
Parameters:
- defaultValue: The default value.
Returns:
- APIField: The new APIField.
func (APIField) WithNested ¶
Nested returns a copy of APIField with Nested set.
Parameters:
- nested: The nested APIFields.
Returns:
- APIField: The new APIField.
func (APIField) WithRequired ¶
WithRequired returns a copy of APIField with Required set.
Parameters:
- required: The required flag.
Returns:
- APIField: The new APIField.
func (APIField) WithSource ¶
WithSource returns a copy of APIField with Source set.
Parameters:
- source: The source.
Returns:
- APIField: The new APIField.
func (APIField) WithType ¶
WithType returns a copy of APIField with Type set.
Parameters:
- typ: The type.
Returns:
- APIField: The new APIField.
func (APIField) WithValidate ¶
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 ¶
NewAPIFields returns a new APIFields.
Parameters:
- fields: The fields.
Returns:
- APIFields: The new APIFields.
func (APIFields) GetAPIField ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.