Documentation ¶
Overview ¶
Package types provides most common structures that can be used in a http server This package also implements a validation function for all the types that check the validity of the values in the fields of these structures
Package types provides custom types and utility functions for handling various data types.
Index ¶
- Constants
- func Validate(rules ...Rule) error
- func ValidateUUID(data ...string) error
- type Address
- type AppDetails
- type CorrelationID
- type Currency
- type Date
- type Datetime
- type Duration
- type Email
- type Enum
- type Health
- type Latitude
- type Location
- type Longitude
- type Phone
- type Raw
- type RawWithOptions
- type Response
- type Rule
- type Time
- type TimeInterval
- type TimeZone
Constants ¶
const CorrelationIDKey contextKey = "correlationID"
CorrelationIDKey is the key denoting correlation ID
Variables ¶
This section is empty.
Functions ¶
func Validate ¶
Validate takes a list of validation rules and checks them one by one. It returns the first error encountered, or nil if all rules pass.
func ValidateUUID ¶
ValidateUUID validates a slice of UUID strings and returns an error if any are invalid.
Types ¶
type Address ¶
type Address struct { AddressLines []string `json:"addressLines"` // Address lines. CityTown string `json:"cityTown"` // City or town. StateProvince string `json:"stateProvince"` // State or province. CountryCode string `json:"countryCode"` // ISO country code. PostalCode string `json:"postalCode"` // Postal code. Company string `json:"company,omitempty"` // Company name (optional). Name string `json:"name,omitempty"` // Name (optional). Residential bool `json:"residential,omitempty"` // Indicates if it is a residential address (optional). DeliveryPoint string `json:"deliveryPoint,omitempty"` // Delivery point (optional). CarrierRoute string `json:"carrierRoute,omitempty"` // Carrier route (optional). TaxID string `json:"taxId,omitempty"` // Tax ID (optional). Status string `json:"status,omitempty"` // Status (optional). Phone Phone `json:"phone,omitempty"` // Phone number (optional). Email Email `json:"email,omitempty"` // Email address (optional). Fax Phone `json:"fax,omitempty"` // Fax number (optional). County string `json:"county,omitempty"` // County (optional). }
Address represents a structured address with various fields, such as address lines, city or town, state or province, postal code, and more. It is designed to store information related to a physical address.
The Check method is used to validate the fields of the Address struct to ensure that they adhere to specific criteria. This method performs checks on mandatory fields and validates optional fields like phone numbers, email addresses, postal codes, and more. It returns an error if any of the validation checks fail.
func (*Address) Check ¶
Check performs validation checks on the Address struct fields.
It verifies the format and content of the address fields to ensure they meet the required criteria. Specifically, it checks for mandatory fields, validates the length of address lines, verifies the country code and postal code, and performs additional checks on optional fields like phone numbers, email addresses, and more.
If any of the validation checks fail, Check returns an error describing the specific parameter that is invalid.
type AppDetails ¶
type AppDetails struct { // Name denotes the name of the application. Name string `json:"name"` // Version denotes the version of the application. Version string `json:"version"` // Framework denotes the GoFr framework version the application is using. Framework string `json:"framework"` }
AppDetails represents information about an application built using the GoFr framework.
It includes details about the application, such as its name, version, and the GoFr framework version it's using.
The AppDetails type is used to encapsulate essential information about the application, making it convenient to access and display information about the application's identity and technology stack.
type CorrelationID ¶
type CorrelationID string
CorrelationID is a 16-byte array with at least one non-zero byte. Used for tracing and logging It will be a part of every incoming request and will be propagated to every outgoing request
func GenerateCorrelationID ¶
func GenerateCorrelationID(ctx context.Context) CorrelationID
GenerateCorrelationID generates a new CorrelationID based on the provided context.
func GetCorrelationIDFromContext ¶
func GetCorrelationIDFromContext(ctx context.Context) CorrelationID
GetCorrelationIDFromContext retrieves the correlation ID from a context.
func (CorrelationID) SetInContext ¶
SetInContext sets a CorrelationID in a context.
func (CorrelationID) String ¶
func (c CorrelationID) String() string
String returns the string representation of a CorrelationID.
func (CorrelationID) Validate ¶
func (c CorrelationID) Validate() error
Validate checks if a CorrelationID is valid.
type Currency ¶
type Currency string
Currency represents a string type for currency values in the format "ISO 4217 Currency Code Amount". Example: "USD 34.55".
The Check method is used to validate the Currency value. It ensures that the Currency value adheres to the required format and that the currency code is a valid ISO 4217 currency code. If the value is not valid, it returns an error specifying the invalid parameter.
func (Currency) Check ¶
Check validates the Currency value by ensuring it adheres to the expected format and checks the validity of the ISO 4217 currency code and the currency amount.
If the Currency value is not in the expected format, or the currency code or amount is invalid, this function returns an error indicating the specific parameter that is invalid. - "currency" is returned if the format is incorrect. - "currencyCountryCode" is returned if the ISO 4217 currency code is invalid. - "currencyValue" is returned if the currency amount is not a valid floating-point number.
type Date ¶
type Date string
Date represents a date in string format (e.g., "2006-01-02") and provides functionality to validate its format.
type Datetime ¶
Datetime represents a date and time along with timezone information in RFC3339 format.
Datetime values consist of two fields: - Value: A string field that holds the full date and time in RFC3339 format. - Timezone: A string field that holds the timezone information about the Datetime value.
This type provides functionality for validating Datetime values, including format and timezone.
type Duration ¶
type Duration string
Duration represents a duration string and provides functionality for validating its format.
A duration in ISO 8601 format consists of components for years (Y), months (M), days (D), hours (H), minutes (M), seconds (S), and weeks (W). The format can be written as PnYnMnDTnHnMnS or PnW.
Duration values should adhere to this ISO 8601 format, and the Check method validates the provided Duration string to ensure it conforms to the expected format. If the provided Duration does not meet the format criteria, the method returns an error with the "duration" parameter.
type Email ¶
type Email string
Email represents an email address in string format and offers functionality to validate its format.
Email addresses are expected to conform to the typical email format, and the Check method utilizes a regular expression to verify whether the provided string matches the expected email address format.
If the input Email string does not adhere to the email format, the Check method returns an error with the "emailAddress" parameter, indicating that the email is invalid.
type Enum ¶
type Enum struct { ValidValues []string `json:"validValues"` Value string `json:"value"` Parameter string }
Enum represents an enum data type with a set of valid values and a current value.
It is designed to define and manage enum values. The Enum type has three fields: - ValidValues: A slice of strings representing all the valid enum values. - Value: A string field holding the current enum value. - Parameter: A string field that denotes the parameter name associated with the enum values.
This type is useful for scenarios where you want to work with predefined, discrete choices. It allows you to specify the valid values for an enum and track the current value.
func (Enum) Check ¶
Check validates if the Value field of the Enum conforms to one of the valid enumeration values. It checks if the provided Value is in UPPER_SNAKE format (uppercase letters and underscores) and matches one of the ValidValues. If the Value doesn't match any of the valid values or doesn't conform to the format, it returns an InvalidParam error.
type Health ¶
type Health struct { // Name denotes the name of the dependency. Name string `json:"name"` // Status denotes the status of the dependency, which can be "UP" or "DOWN". Status string `json:"status"` // Host denotes the host of the dependency used to check its status. (Optional) // This field is typically not applicable for certain types of services. Host string `json:"host,omitempty"` // Database denotes the name of the database that the application is using. (Optional) // This field is relevant when the dependency represents a database. Database string `json:"database,omitempty"` // Details can hold additional information or details about the dependency's health. (Optional) // It can contain any arbitrary data, such as error messages, diagnostic information, or custom data. Details interface{} `json:"details,omitempty"` }
Health represents the health status of a dependency used by an application.
It provides information about the dependency's health, including its name, status (UP or DOWN), host information (optional), the associated database (optional), and additional details (optional).
The Health type is typically used for monitoring and reporting the status of various dependencies such as databases, services, or external components that an application relies on.
type Latitude ¶
type Latitude float64
Latitude represents latitude information as a floating-point value and offers functionality for validating its value.
Latitude values are typically expressed in degrees and should fall within the valid range of -90 degrees (South) to 90 degrees (North). The Check method ensures that the provided Latitude value is within this range. If the value is outside this range, it returns an error with the "lat" parameter.
type Location ¶
type Location struct { // Latitude denotes the latitude of a location Latitude *Latitude `json:"lat"` // Longitude denotes the longitude of a location Longitude *Longitude `json:"lng"` }
Location denotes a location with the lat, long values
type Longitude ¶
type Longitude float64
Longitude denotes the longitude information and provides functionality to validate it
type Phone ¶
type Phone string
Phone denotes the phone number in string format and provides functionality to validate it
type Raw ¶
type Raw struct {
Data interface{}
}
Raw denotes a raw datatype which can hold any type of data
type RawWithOptions ¶
type RawWithOptions struct { // Data the raw data that needs to be served Data interface{} // ContentType denotes the type of data ContentType string // Headers contains any headers that needs to be passed while serving the data Header map[string]string }
RawWithOptions denotes a Raw type but can hold more information about the data
type Response ¶
type Response struct { // Data holds the data that needs to be served Data interface{} `json:"data" xml:"data"` // Meta holds the metadata that is requested Meta interface{} `json:"meta,omitempty" xml:"meta,omitempty"` }
Response denotes the response to a incoming Http request
type Rule ¶
type Rule interface { // Check provides functionality to perform validations Check() error }
Rule provides the functionality to implement a validation rule
type TimeInterval ¶
type TimeInterval string
TimeInterval denotes a time interval in string and provides functionality to validate it
func (TimeInterval) Check ¶
func (t TimeInterval) Check() error
Check validates the TimeInterval value. It ensures that the time interval follows the ISO 8601 standard format and contains valid timestamps. The format should be either "start_time/PnYnMnDTnHnMnS" or "start_time/end_time". If any validation fails, it returns an InvalidParam error with the corresponding parameter name.
func (TimeInterval) GetStartAndEndTime ¶
func (t TimeInterval) GetStartAndEndTime() (startTime, endTime time.Time, err error)
GetStartAndEndTime will return startTime and endTime for a given TimeInterval In case of an error, will return zero value of time.Time along with error