Library providing opanapi3 and Go types for store/validation and transfer of ISO-4217, ISO-3166, and other types.

Overview

Go Report Card Build Status codecov

go-types

This library has been created with the purpose to facilitate the store, validation, and transfer of Go ISO-3166/ISO-4217/timezones/emails/URL types. There is a openapi3 spec of that type and make you able to include it into your spec. All types has own ozzo.Validate, json.Unmarshaler, Stringer and driver.Valuer implementations.

Installation

go get github.com/mikekonan/go-types

Usage:

//1. use in your structs
type User struct {
	Name    string             `json:"name" db:"name"`
	Country country.Alpha2Code `json:"country" db:"country"`
}

func main() {
	user := User{}
	//2. use in your wire
	json.Unmarshal([]byte(`{"name":"name", "country": "ca"}`), &user)
	//3. check is set
	user.Country.IsSet() //check user country is provided
	//4. validate using ozzo-validation
	if err := validation.ValidateStruct(&user, validation.Field(&user.ountry, validation.Required, user.Country)); err != nil {
		log.Fatal(err)
	}
	//5. lookup by alpha2, alpha3, country name
	if userCountry, ok := country.ByAlpha2Code(user.Country); ok {
		fmt.Printf("country name - '%s', alpha-2 - '%s', alpha-3 - '%s'", serCountry.Name(), userCountry.Alpha2Code(), userCountry.lpha3Code())
	}
	//6. store in db
	fmt.Println(user.Country.Value()) //prints 'CA'
	//7. use specific countries
	fmt.Println(country.Canada.Alpha2Code())
}

Links:

You might also like...
Gin Middleware to extract json tag value from playground validator's errors validation

Json Tag Extractor for Go-Playground Validator This is Gin Middleware that aim to extract json tag and than store it to FieldError.Field() object. Ins

Validator - Replace the validation framework used by gin

validator Replace the validation framework used by gin replace mod:replace githu

The Hyperscale InputFilter library provides a simple inputfilter chaining mechanism by which multiple filters and validator may be applied to a single datum in a user-defined order.

Hyperscale InputFilter Branch Status Coverage master The Hyperscale InputFilter library provides a simple inputfilter chaining mechanism by which mult

This project is a Library to validate a pull secret
This project is a Library to validate a pull secret

lib-ps-validator Description This repository and its contents are completely UNSUPPORTED in any way and are not part of official documentation. Lib to

A library to remove special characters from a string.

spechar Is a small library for removing special characters from strings. Install First you have to install the package: go get github.com/gowizzard/sp

A tool/library to run custom validations on Kubernetes resources in parallel

cluster-validator cluster-validator is a tool/library for performing resource validations in parallel on a Kubernetes cluster. For example, validating

[Go] Package of validators and sanitizers for strings, numerics, slices and structs

govalidator A package of validators and sanitizers for strings, structs and collections. Based on validator.js. Installation Make sure that Go is inst

Provide check digit algorithms and calculators written in Go

checkdigit About Provide check digit algorithms and calculators written by Go. Provided methods Algorithms Luhn Verhoeff Damm Calculators ISBN-10 ISBN

A norms and conventions validator for Terraform

This tool will help you ensure that a terraform folder answer to your norms and conventions rules. This can be really useful in several cases : You're

Comments
  • Compilation error with ozzo validator.

    Compilation error with ozzo validator.

    cannot user.Country (variable of type country.Alpha2Code) as validation.Rule value in argument to validation.Field: wrong type for method Validate (have func() (err error), want func(value interface{}) error)

    My Code

    package foo
    
    import (
        validation "github.com/go-ozzo/ozzo-validation"
        "github.com/mikekonan/go-types/country"
    )
    
    type Foo struct {
        CountryCode country.Alpha2Code
    }
    func (r *Foo) Validate() error {
        return validation.ValidateStruct(r,
            validation.Field(r.Country, validation.Required, r.Country),
        ),
    }
    
    question 
    opened by hsblhsn 4
  • add custom NullTime. upgrade go -> 1.18

    add custom NullTime. upgrade go -> 1.18

    Добавил нашу оберткну над null.Time для обработки кейса:

    {
      "nullTime": ""
    }
    

    На стандартном null.Time падает такая ошибка: "error":"parsing time """" as ""2006-01-02T15:04:05Z07:00"": cannot parse """ as "2006""

    opened by asechiho 0
Owner
Mikalai Konan
Software Engineer
Mikalai Konan
Golang parameter validation, which can replace go-playground/validator, includes ncluding Cross Field, Map, Slice and Array diving, provides readable,flexible, configurable validation.

Checker 中文版本 Checker is a parameter validation package, can be use in struct/non-struct validation, including cross field validation in struct, elemen

Liang Yaopei 77 Sep 5, 2022
Opinionated go to validation library

?? valeed Your opinionated go-to validation library. Struct tag-based. Validate here, validate there, validate everywhere. Sleek and simple validation

Avré Barra 0 Jul 21, 2022
:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving

Package validator Package validator implements value validations for structs and individual fields based on tags. It has the following unique features

Go Playgound 11.5k Sep 27, 2022
Gookit 783 Sep 24, 2022
Swagger builder and input validation for Go servers

crud A Swagger/OpenAPI builder and validation library for building HTTP/REST APIs. Heavily inspired by hapi and the hapi-swagger projects. No addition

Jake Coffman 37 Aug 20, 2022
Go package containing implementations of efficient encoding, decoding, and validation APIs.

encoding Go package containing implementations of encoders and decoders for various data formats. Motivation At Segment, we do a lot of marshaling and

Segment 866 Sep 24, 2022
Struct validation using tags

Govalid Use Govalid to validate structs. Documentation For full documentation see pkg.go.dev. Example package main import ( "fmt" "log" "strings"

Travis Harmon 30 Sep 6, 2022
Validate Golang request data with simple rules. Highly inspired by Laravel's request validation.

Validate golang request data with simple rules. Highly inspired by Laravel's request validation. Installation Install the package using $ go get githu

Saddam H 1.1k Sep 22, 2022
An interesting go struct tag expression syntax for field validation, etc.

go-tagexpr An interesting go struct tag expression syntax for field validation, etc. Usage Validator: A powerful validator that supports struct tag ex

Bytedance Inc. 1.3k Sep 30, 2022
Simple module for validation inn control number

simple module for validation inn control number

Pavel 1 Sep 4, 2022