libaddress

package module
v0.0.0-...-a2a55bd Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2020 License: MIT Imports: 10 Imported by: 0

README

libaddress

[WIP] libaddress is a Go pkg that validates addresses using data generated from Google's Address Data Service

Documentation

Overview

Package libaddress is a library that validates address using data generated from Google's Address Data Service

Code generated by libaddress. DO NOT EDIT.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidCountryCode indicates that the country code
	// used to create an address is invalid
	ErrInvalidCountryCode = errors.New("invalid:Country")

	// ErrInvalidDependentLocality indicates that the dependent
	// locality is invalid. This is usually due to the country
	// having a pre-determined list of dependent localities and
	// the value does not match any of the keys in the list of
	// dependent localities.
	ErrInvalidDependentLocality = errors.New("invalid:DependentLocality")

	// ErrInvalidLocality indicates that the locality is invalid.
	// This is usually due to the country having a pre-determined
	// list of localities and the value does not match any of the
	// keys in the list of localities
	ErrInvalidLocality = errors.New("invalid:Locality")

	// ErrInvalidAdministrativeArea indicates that the administrative
	// area is invalid. This is usually due to the country having a
	// pre-determined list of administrative areas and the value
	// does not match any of the keys in the list of administrative
	// areas.
	ErrInvalidAdministrativeArea = errors.New("invalid:AdministrativeArea")

	// ErrInvalidPostCode indicates that the post code did not validate
	// using the regular expression of the country
	ErrInvalidPostCode = errors.New("invalid:PostCode")
)

Functions

func ListFormatFunc

func ListFormatFunc(es []error) string

func Validate

func Validate(address Address) error

Validate checks an address to determine if it is valid. To create a valid address, the `address.NewValid()` function can do it in one call.

func WithAdministrativeArea

func WithAdministrativeArea(administrativeArea string) func(*Address)

WithAdministrativeArea sets the administrative area (commonly known as the state) of an address. If the country of the address has a list of administrative areas, then the key of the administrative area should be used, otherwise, the validation will fail.

func WithCountry

func WithCountry(cc string) func(*Address)

WithCountry sets the country code of an address. The country code must be an ISO 3166-1 country code.

func WithDependentLocality

func WithDependentLocality(dependentLocality string) func(*Address)

WithDependentLocality sets the dependent locality (commonly know as the suburb) of an address. If the country of the address has a list of dependent localities, then the key of the dependent locality should be used, otherwise the validation will fail.

func WithLocality

func WithLocality(locality string) func(*Address)

WithLocality sets the locality (commonly known as the city) of an address. If the country of the address has a list of localities, then the keys of the locality should be used, otherwise validation will fail.

func WithName

func WithName(name string) func(*Address)

WithName sets the addressee's name of an address.

func WithOrganization

func WithOrganization(organization string) func(*Address)

WithOrganization sets teh addressee's organization of an address

func WithPostCode

func WithPostCode(postCode string) func(*Address)

WithPostCode sets the post code of an address.

func WithSortingCode

func WithSortingCode(sortingCode string) func(*Address)

WithSortingCode sets the sorting code of an address.

func WithStreetAddress

func WithStreetAddress(streetAddress []string) func(*Address)

WithStreetAddress sets the street address of an address. The street address is a slice of strings, with each element representing an address line.

Types

type Address

type Address struct {
	Country            string
	Name               string
	Organization       string
	StreetAddress      []string
	DependentLocality  string
	Locality           string
	AdministrativeArea string
	PostCode           string
	SortingCode        string
}

Address represents a valid address made up of its child components

func New

func New(fields ...func(*Address)) Address

New creates a new unvalidated address. The validity of the address should be checked using the validator.

func NewValid

func NewValid(fields ...func(*Address)) (Address, error)

NewValid creates a new Address. If the address is invalid, an error is returned. In the case where an error is returned, the error is a hashicorp/go-multierror (https://github.com/hashicorp/go-multierror). You can use a type switch to get a list of validation errors for the address.

type AdministrativeAreaData

type AdministrativeAreaData struct {
	ID   string
	Name string

	Localities LocalitySlice
}

AdministrativeAreaData contains the name and ID of an administrative area. The ID must be passed to WithAdministrativeArea() when creating an address. The name is useful for displaying to the end user.

type AdministrativeAreaSlice

type AdministrativeAreaSlice []AdministrativeAreaData

type CountryData

type CountryData struct {
	Format                     string                             `json:"format"`
	LatinizedFormat            string                             `json:"latinized_format"`
	Required                   []Field                            `json:"required"`
	Allowed                    []Field                            `json:"allowed"`
	DefaultLanguage            string                             `json:"default_language"`
	AdministrativeAreaNameType FieldName                          `json:"administrative_area_name_type"`
	LocalityNameType           FieldName                          `json:"locality_name_type"`
	DependentLocalityNameType  FieldName                          `json:"dependent_locality_name_type"`
	PostCodeNameType           FieldName                          `json:"post_code_name_type"`
	PostCodeRegex              PostCodeRegexData                  `json:"post_code_regex"`
	AdministrativeAreas        map[string]AdministrativeAreaSlice `json:"administrative_areas"`
}

CountryData contains the address data for a country. The AdministrativeAreas field contains a list of nested subdivisions (administrative areas, localities and dependent localities) grouped by their translated languages. They are also sorted according to the sort order of the languages they are in.

func GetCountry

func GetCountry(cc string) CountryData

Get country returns address information for a given country.

type CountryList

type CountryList []CountryListItem

CountryList contains a list of countries that can be used to create addresses.

func ListCountries

func ListCountries(l string) CountryList

ListCountries returns a list of countries that can be used to create addresses. Language must be a valid ISO 639-1 language code such as jp, zh etc. If the language does not have any translations or is invalid then english is used as the fallback. The returned list of countries is sorted according to the chosen language.

func (CountryList) Bytes

func (c CountryList) Bytes(i int) []byte

Bytes returns a country name in bytes. This is used for sorting the countries and would not be generally used in client code.

func (CountryList) Len

func (c CountryList) Len() int

Len returns the number of countries in the list. This is used for sorting the countries and would not be generally used in client code.

func (CountryList) Swap

func (c CountryList) Swap(i, j int)

Swap swaps 2 countries in the list. This is used for sorting the countries and would not be generally used in client code.

type CountryListItem

type CountryListItem struct {
	Code string `json:"code"`
	Name string `json:"name"`
}

CountryListItem represents a single country containing the ISO 3166-1 code and the name of the country.

type DependentLocalityData

type DependentLocalityData struct {
	ID   string
	Name string
}

DependentLocalityData contains the name and ID of a dependent locality. The ID must be passed to WithDependentLocalityData() when creating an address. The name is useful for displaying to the end user.

type DependentLocalityDataSlice

type DependentLocalityDataSlice []DependentLocalityData

type DisplayField

type DisplayField struct {
	ID      Field  `json:"id"`
	Display string `json:"display"`
}

type DisplayFieldName

type DisplayFieldName struct {
	ID      FieldName `json:"id"`
	Display string    `json:"display"`
}

type ErrMissingRequiredFields

type ErrMissingRequiredFields struct {
	Fields []Field
	// contains filtered or unexported fields
}

ErrMissingRequiredFields indicates that a required address field is missing. The Fields field can be used can be used to get a list of missing field.

func (ErrMissingRequiredFields) Error

func (e ErrMissingRequiredFields) Error() string

type ErrUnsupportedFields

type ErrUnsupportedFields struct {
	Fields []Field
	// contains filtered or unexported fields
}

ErrUnsupportedFields indicates that an address field is provided, but it is not supported by the address format of the country. The Fields field can be used to get a list of unsupported fields.

func (ErrUnsupportedFields) Error

func (e ErrUnsupportedFields) Error() string

type ExternalCountry

type ExternalCountry struct {
	Required                   []*DisplayField                    `json:"required"`
	Allowed                    []*DisplayField                    `json:"allowed"`
	Language                   string                             `json:"language"`
	AdministrativeAreaNameType *DisplayFieldName                  `json:"administrative_area_name_type"`
	LocalityNameType           *DisplayFieldName                  `json:"locality_name_type"`
	DependentLocalityNameType  *DisplayFieldName                  `json:"dependent_locality_name_type"`
	PostCodeNameType           *DisplayFieldName                  `json:"post_code_name_type"`
	AdministrativeAreas        map[string]AdministrativeAreaSlice `json:"administrative_areas"`
}

func Externalize

func Externalize(c country) ExternalCountry

func GetExternalCountry

func GetExternalCountry(cc string) ExternalCountry

Get external country returns readadble address information for a given country.

type Field

type Field int

Field is an address field type.

const (
	Country Field = iota + 1
	Name
	Organization
	StreetAddress
	DependentLocality
	Locality
	AdministrativeArea
	PostCode
	SortingCode
)

func (Field) Key

func (i Field) Key() string

Key returns the corresponding one-letter abbreviation used by Google to refer to address fields. This is useful for parsing the address format for a country. See https://github.com/googlei18n/libaddressinput/wiki/AddressValidationMetadata for more information

func (Field) Readable

func (i Field) Readable() string

func (Field) String

func (i Field) String() string

type FieldName

type FieldName int

FieldName is the name to be used when referring to a field. For example, in India the postal code is called PIN Code instead of PostalCode. The field name allows you to display the appropriate form labels to the user.

const (
	Area FieldName = iota + 1
	City
	County
	Department
	District
	DoSi
	Eircode
	Emirate
	Island
	Neighborhood
	Oblast
	PINCode
	Parish
	PostTown
	PostalCode
	Prefecture
	Province
	State
	Suburb
	Townland
	VillageTownship
	ZipCode
)

func (FieldName) Readable

func (i FieldName) Readable() string

func (FieldName) String

func (i FieldName) String() string

type LocalityData

type LocalityData struct {
	ID   string
	Name string

	DependentLocalities DependentLocalityDataSlice
}

LocalityData contains the name and ID of a locality. The ID must be passed to WithLocalityData() when creating an address. The name is useful for displaying to the end user.

type LocalitySlice

type LocalitySlice []LocalityData

type PostCodeRegexData

type PostCodeRegexData struct {
	Regex            string
	SubdivisionRegex map[string]PostCodeRegexData
}

PostCodeRegexData contains regular expressions for validating post codes for a given country. If the country has subdivisions (administrative areas, localities and dependent localities), the SubdivisionRegex field may contain further regular expressions to Validate the post code.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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