jsonValidator

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 8 Imported by: 0

README

JsonValidator

JsonValidator is a package that helps create CRUD APIs by validating HTTP JSON bodies against a predefined "form" struct.

Available validations

Types
type Object struct {
    Name           string    `validations:"type=string`
    Code           int       `validations:"type=int`
    Price          float64   `validations:"type=float`
    Successful     bool      `validations:"type=bool`
    Person         Person    `validations:"type=struct`
    Owners         []string  `validations:"type=[]string`
    PreviousCodes  []int     `validations:"type=[]int`
    PreviousPrices []float64 `validations:"type=[]float`
    PersonList     []Person  `validations:"type=[]struct`
}

The package is capable of transforming data if necessary. For example if the form is type struct {Count int `validations:"type=int"`} and the received JSON is {'count': '12345'} the package will cast the '12345' string into an int.

Required
type Object struct {
    Name           string    `validations:"type=string;required=true`
}
Min and Max
type Object struct {
    Name           string    `validations:"type=string;min=1;max=10"`
    Code           int       `validations:"type=int;min=1;max=10"`
    Price          float64   `validations:"type=float;min=1;max=10"`
    Owners         []string  `validations:"type=[]string;min=1;max=2"`
    PreviousCodes  []int     `validations:"type=[]int;min=1;max=2"`
    PreviousPrices []float64 `validations:"type=[]float;min=1;max=2"`
    PersonList     []Person  `validations:"type=[]struct;min=1;max=2"`
}

Any type can have a min and max besides the 'bool' type.

The min and max functionality depends on each type:

  • For type=string the min and max are the minimum/maximum length of the string.
  • For type=int the min and max are the minimum/maximum number for the int.
  • For type=float the min and max are the minimum/maximum number for the float.
  • For type=[]string or type=[]int or type=[]float or type=[]struct the min and max are the minimum/maximum length for the array. Basically how many "options" can be selected.
Choices
type Object struct {
    Name           string    `validations:"type=string;choices=Daniel,Jaime,Carolina"`
    Code           int       `validations:"type=int;choices=1,2,3"`
    Price          float64   `validations:"type=float;choices=1.0,2.0,3.0"`
    Owners         []string  `validations:"type=[]string;choices=Daniel,Jaime,Carolina"`
    PreviousCodes  []int     `validations:"type=[]int;choices=1,2,3"`
    PreviousPrices []float64 `validations:"type=[]float;choices=1.0,2.0,3.0"`
}

The package will validate the received JSON against the available choices

Structs
type Person struct {
    Name string `validations:"type=string"`
    Age  int    `validations:"type=int"`
}
type Object struct {
    Person     Person   `validations:"type=struct"`
    PersonList []Person `validations:"type=[]struct"`
}

This package is also capable of validating validations inside the defined struct

Errors

Last but not least we have the errors. The package will return the errors in the ValidationError slice.

type ValidationError struct {
	Field   string
	Message string
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultChoicesSeparator = ","
View Source
var DefaultMessages = map[string]string{
	"InvalidField":     "This field is invalid.",
	"InvalidFormat":    "This field has an invalid format (%v).",
	"InvalidMinString": "This field must have at least %v characters.",
	"InvalidMaxString": "This field must not have more than %v characters.",
	"InvalidMinNumber": "This field must be bigger than %v.",
	"InvalidMaxNumber": "This field must be smaller than %v.",
	"InvalidMinList":   "This field must have at least %v elements.",
	"InvalidMaxList":   "This field must not have more than %v elements.",
	"RequiredField":    "This field is required.",
	"InvalidChoice":    "This field has an invalid choice (%v). The valid choices are (%v)",
}
View Source
var DefaultSeparator = ";"
View Source
var DefaultTagName = "validations"

Functions

func LowerCase

func LowerCase(str string) string

func TitleCase

func TitleCase(str string) string

func Validate

func Validate(jsonData []byte, form any) []error

Validate validates the json data against a form received and update the form with the parsed data.

Types

type ValidationError

type ValidationError struct {
	Field   string
	Message string
}

func (ValidationError) Error

func (vr ValidationError) Error() string

type Validations

type Validations struct {
	Type     string
	Required bool
	Min      float64
	Max      float64
	Choices  []any
}

Jump to

Keyboard shortcuts

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