validator

package
v0.1.25 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2022 License: MIT Imports: 6 Imported by: 0

README

Ssi Core / Validator

It has been developed to provide validation infrastructure related to i18n to our microservices. Custom also includes some tags and the microservice we use amy not load it if it wants to.

Type

type ErrorResponse struct {
	Field   string      `json:"field"`
	Message string      `json:"message"`
	Value   interface{} `json:"value"`
}

Functions

func New(i *i18n.I18n) *Validator

func (v *Validator) ValidateStruct(s interface{}, languages ...string) []*ErrorResponse
func (v *Validator) ConnectCustom()
func (v *Validator) RegisterTagName()

Example

package main

import (
    "github.com/ssibrahimbas/ssi-core/pkg/i18n"
    "github.com/ssibrahimbas/ssi-core/pkg/validator"
    "fmt"
)

type User struct {
    Name string `validate:"required"`
    Age  int    `validate:"required"`
}

func main() {
    i := i18n.New("tr")
    i.LoadLanguages("./locales", "tr", "en")
    v := validator.New(i)

    u := &User{
        Name: "Sami",
        Age:  20,
    }
    	if errors := v.ValidateStruct(u, "en", "tr"); len(errors) > 0 {
		fmt.Println("An error occurred while validating the user.")
	}
}

Load Custom Validators

Custom validators: [username, password, locale, object_id, slug]

package main

import (
    "github.com/ssibrahimbas/ssi-core/pkg/i18n"
    "github.com/ssibrahimbas/ssi-core/pkg/validator"
    "fmt"
)

type User struct {
    Name string `validate:"required"`
    Age  int    `validate:"required"`
}

func main() {
    i := i18n.New("tr")
    i.LoadLanguages("./locales", "tr", "en")
    v := validator.New(i)
    v.ConnectCustom()
    v.RegisterTagName()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorResponse

type ErrorResponse struct {
	Field   string      `json:"field"`
	Message string      `json:"message"`
	Value   interface{} `json:"value"`
}

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

func New

func New(i *i18n.I18n) *Validator

func (*Validator) ConnectCustom

func (v *Validator) ConnectCustom()

func (*Validator) RegisterTagName

func (v *Validator) RegisterTagName()

func (*Validator) ValidateStruct

func (v *Validator) ValidateStruct(s interface{}, languages ...string) []*ErrorResponse

Jump to

Keyboard shortcuts

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