CatalogApi v0.0.16 CatalogApi.Address View Source

Defines the CatalogApi.Address struct and functions which are responsible for validation and interpretation of physical shipping addresses as they relate to CatalogApi.

To see the CatalogApi documentation for what is and isn’t a valid Address see http://username.catalogapi.com/docs/methods/cart_methods/#cart_set_address

An overview of the address fields is as follows:

  • first_name (required): The first name of the person receiving shipment.
  • last_name (required): The last name of the person receiving shipment.
  • address_1 (required): The street address.
  • address_2 (optional): The second line of the street address.
  • address_3 (optional): The third line of the street address.
  • city (required): The city.
  • state_province (required): The state or province. If it is a US state, this should be the 2 digit abbreviation. (Example: OH)
  • postal_code (required) : The postal code. This should be a string.
  • country (required): The ISO 3166-1 alpha-2 country code.
  • email (optional): The email of the person receiving shipment.
  • phone_number (optional): The phone number of the person receiving shipment.

Link to this section Summary

Functions

Returns a valid fake address. Useful for testing

Validates an address struct to ensure that its values will not be rejected by CatalogApi endpoints. This ensures that an error can be thrown before the CatalogApi endpoint is actually hit

Validates a specific address field in the context of what is valid as input to a CatalogApi address

Validates a map with string or atom keys that is intended to represent a CatalogApi address

Link to this section Types

Link to this type invalid_address_error() View Source
invalid_address_error() :: {:invalid_address, [{atom(), [String.t()]}]}
Link to this type t() View Source
t() :: %CatalogApi.Address{
  address_1: term(),
  address_2: term(),
  address_3: term(),
  city: term(),
  country: term(),
  email: term(),
  first_name: term(),
  last_name: term(),
  phone_number: term(),
  postal_code: term(),
  state_province: term()
}

Link to this section Functions

Link to this function cast(address_json) View Source
cast(map()) :: t()
Link to this function extract_address_from_json(arg1) View Source
Link to this function fake_valid_address() View Source
fake_valid_address() :: t()

Returns a valid fake address. Useful for testing.

Link to this function validate(address) View Source
validate(t()) :: :ok | {:error, invalid_address_error()}

Validates an address struct to ensure that its values will not be rejected by CatalogApi endpoints. This ensures that an error can be thrown before the CatalogApi endpoint is actually hit.

If the Address struct is valid, :ok is returned.

If there are validation errors, than an error tuple is returned which enumerates the field specific errors.

To see the CatalogApi documentation for what is and isn’t a valid Address see http://username.catalogapi.com/docs/methods/cart_methods/#cart_set_address

Examples

iex> address = %CatalogApi.Address{
...>   first_name: "Jo",
...>   last_name: "Bob",
...>   address_1: "123 Street Road",
...>   city: "Cleveland",
...>   state_province: "OH",
...>   postal_code: "44444",
...>   country: "US"}
...> CatalogApi.Address.validate(address)
:ok

iex> address = %CatalogApi.Address{
...>   first_name: "Jo",
...>   last_name: "Bob",
...>   address_1: "123 Street Road",
...>   city: "",
...>   state_province: "OH",
...>   postal_code: "44444",
...>   country: "AJ"}
...> CatalogApi.Address.validate(address)
{:error, {:invalid_address, %{country: ["country code must be valid ISO 3166-1 alpha 2 country code"], city: ["cannot be blank"]}}}
Link to this function validate_field(arg1, first_name) View Source
validate_field(atom(), any()) :: map()

Validates a specific address field in the context of what is valid as input to a CatalogApi address.

Link to this function validate_params(address) View Source
validate_params(t() | map()) :: :ok | {:error, invalid_address_error()}

Validates a map with string or atom keys that is intended to represent a CatalogApi address.

If the params are valid, :ok is returned.

If there are validation errors, than an error tuple is returned which enumerates the field specific errors.

To see the CatalogApi documentation for what is and isn’t a valid Address see http://username.catalogapi.com/docs/methods/cart_methods/#cart_set_address

Example

iex> address = %{
...>   first_name: "Jo",
...>   last_name: "Bob",
...>   address_1: "123 Street Road",
...>   city: "Cleveland",
...>   state_province: "OH",
...>   postal_code: "44444",
...>   country: "US"}
...> CatalogApi.Address.validate_params(address)
:ok

This function also properly validates a map where the keys are strings.

Example

iex> address = %{
...>   "first_name" => "Jo",
...>   "last_name" => "Bob",
...>   "address_1" => "123 Street Road",
...>   "city" => "Cleveland",
...>   "state_province" => "OH",
...>   "postal_code" => "44444",
...>   "country" => "US"}
...> CatalogApi.Address.validate_params(address)
:ok