Swiftybase
Function StackCustom Function

Get Self

Obtains a reference to the struct that a method is attached to, allowing you to access and modify the struct's fields within a function stack.

Purpose

The Get Self function provides access to the current struct instance when working with struct methods. This is particularly useful for:

  • Validating struct data
  • Modifying struct fields
  • Accessing struct properties for calculations
  • Implementing business logic that depends on struct state

Config

ParameterDescription
AliasName of the variable that will hold the struct reference (e.g., self, this, s)
Cast Struct TypeThe type of struct that will be passed to the function (should be a pointer type for modifications)
Self RequiredDetermines if the function requires a valid struct reference to execute

Example: Validating API Input Automatically

This example demonstrates how to use Get Self to implement automatic input validation for API requests. We'll create a struct with an attached validation function that will be called automatically when parsing request data.

Create UserReq Struct

First, create a struct named UserReq to hold basic user information like name and age.

Create Validation Function

Create a custom function named ValidateUserReq. The name must start with Validate to be automatically called when a new request is parsed.

Define Function Output

Configure the function to take no parameters and return either a single FieldError or a list of FieldError types. For most cases, returning the first error encountered is recommended.

Use Get Self to Access the Struct

Add the Get Self function to access the struct passed to the validation function.

Set f as the variable name and define the type as a pointer to UserReq. Using a pointer type is essential as it allows you to update fields directly.

Implement Validation Logic

In this example:

  1. First, trim the name field to remove any spaces and assign the updated value back to the field
  2. Use an "If Not All Of" condition to check if any validation rules fail
  3. Validate that the name length is between 2 and 15 characters
  4. Check that the name starts with "rebaz"

Create Field Error

If validation fails, return a FieldError by using "Initiate Struct" to create a new instance of the FieldError type. Fill in the name, message, and optionally any details you want to return to the user.

Complete the struct with appropriate values:

Return the Error

Return the created error:

Alternatively, you can inline the error creation and return it directly:

Best Practices

  • Use Pointers: Always use pointer types when you need to modify struct fields
  • Validation Functions: Name validation functions with the Validate prefix for automatic execution
  • Sanitization: Use Get Self to both validate and sanitize input data
  • Error Details: Include specific field names and helpful messages in validation errors
  • Reusability: Define reusable structs outside of API handlers for better code organization
  • Concise Validation: Return the first validation error encountered rather than collecting all errors
  • Default Validations: Remember that struct typing already provides basic type validation; use Get Self for additional business logic checks

For optimal code organization, define frequently used structs and their validation functions outside specific API handlers. This approach improves reusability and maintains cleaner API definitions.

On this page