Skip to content
On this page

Validation

All validator function

Base on

  1. asaskevich/govalidator: [Go] Package of validators and sanitizers for strings, numerics, slices and structs (github.com)
  2. thedevsaddam/gojsonq: A simple Go package to Query over JSON/YAML/XML/CSV Data (github.com)

Example

go
package requests

import (
  "gitlab.finema.co/finema/golang-template/models"
  core "gitlab.finema.co/finema/idin-core"
)

type UserCreate struct {
  core.BaseValidator 
  Email    *string `json:"email"`      // Email field for user creation
  FullName *string `json:"full_name"`  // Full name field for user creation
}


func (r *UserCreate) Valid(ctx core.IContext) core.IError {
  // Check if the email is valid format
  r.Must(r.IsEmail(r.Email, "email"))
  
  // Check if the email is not empty
  r.Must(r.IsStrRequired(r.Email, "email"))
  
  // Check if the email is unique in the database
  r.Must(r.IsStrUnique(ctx, r.Email, models.User{}.TableName(), "email", "", "email"))

  // Check if the full name is not empty
  r.Must(r.IsStrRequired(r.FullName, "full_name")) 

  return r.Error()
}
go
package user

import (
	"gitlab.finema.co/finema/golang-template/requests"
	"gitlab.finema.co/finema/golang-template/services"
	core "gitlab.finema.co/finema/idin-core"
	"gitlab.finema.co/finema/idin-core/utils"
	"net/http"
)

type UserController struct {
}

func (m UserController) Create(c core.IHTTPContext) error {
	// Create an instance of UserCreate struct to hold the request data
	input := &requests.UserCreate{}

	// Bind the request body to the input struct and validate the input
	if err := c.BindWithValidate(input); err != nil {
		return c.JSON(err.GetStatus(), err.JSON())
	}

	// Create an instance of UserService
	userSvc := services.NewUserService(c)

	// Create a payload to pass to the userSvc.Create method
	payload := &services.UserCreatePayload{}
	_ = utils.Copy(payload, input)

	// Call the userSvc.Create method to create a new user
	user, err := userSvc.Create(payload)
	if err != nil {
		return c.JSON(err.GetStatus(), err.JSON())
	}

	// Return the created user as JSON response
	return c.JSON(http.StatusCreated, user)
}

Example response (Return HTTP Status 400)

json
{
    "code": "INVALID_PARAMS",
    "message": "Invalid parameters",
    "fields": {
        "password": {
            "code": "REQUIRED",
            "message": "The password field is required"
        },
        "username": {
            "code": "REQUIRED",
            "message": "The username field is required"
        }
    }
}

All validators


Here's the list of functions organized by function category in a table format:

String


Function NameFunction NameFunction Name
IsStrInIsStrMaxIsStrMin
IsStrRequiredIsStrUniqueIsStringContain
IsStringEndWithIsStringLowercaseIsStringNotContain
IsStringNumberIsStringNumberMinIsStringStartWith
IsStringUppercase

Array

Function NameFunction NameFunction Name
IsArrayBetweenIsArrayBetweenIsArrayMax
IsArrayMaxIsArrayMinIsArrayMin
IsArraySizeIsArraySize

Number


Function NameFunction NameFunction Name
IsFloatNumberBetweenIsFloatNumberMaxIsFloatNumberMin
IsNumberBetweenIsNumberMaxIsNumberMin

Date and Time


Function NameFunction NameFunction Name
IsDateTimeIsDateTimeAfterIsDateTimeBefore
IsTimeIsTimeAfterIsTimeBefore
IsTimeRequired

JSON


Function NameFunction NameFunction Name
IsJSONArrayIsJSONArrayMaxIsJSONArrayMin
IsJSONBoolPathRequiredIsJSONObjectIsJSONObjectNotEmpty
IsJSONObjectPathIsJSONPathRequireNotEmptyIsJSONPathRequired
IsJSONPathStrInIsJSONRequiredIsJSONStrPathRequired
LoopJSONArray

Email


Function Name
IsEmail

Database


Function NameFunction Name
IsExistsIsExistsWithCondition
IsMongoExistsWithConditionIsMongoStrUnique
IsMongoExistsWithConditionIsStrUnique

Base64

Function Name
IsBase64

Boolean


Function NameFunction Name
IsBoolRequired

Custom

Function NameFunction Name
IsCustom

IP Address


Function Name
IsIP

Required


Function NameFunction Name
IsRequiredIsRequiredArray

URL


Function Name
IsURL

Maintained by Passakon Puttasuwan & Dev Core Team.