šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Sign inDemoInstall
Socket

github.com/danangkonang/validation

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/danangkonang/validation

v0.0.0-20250102050600-2f0fea033881
Source
Go
Version published
Created
Source

Simple GoLang Validation

Example

package main

import (
	"encoding/json"
	"fmt"

	"github.com/danangkonang/validation"
)

func main() {
	type Children struct {
		Level string `json:"level" enum:"beginner,intermediate,advanced"`
	}
	type T struct {
		Username        string     `json:"username" validate:"required,email"`
		Password        string     `json:"password" validate:"min=20"`
		ConfirmPassword string     `json:"confirm_password" validate:"eqfield=Password"`
		Children        []Children `json:"children" validate:"required"`
	}
	data := T{
		Username:        "",
		Password:        "foo",
		ConfirmPassword: "bar",
		Children: []Children{
			{
				Level: "",
			},
		},
	}
	v := validation.New()
	customMessage := map[string]string{
		"required": "your custom message",
		"min":      "minimum {{.}} char",
	}
	v.SetLanguage(customMessage)
	ValidationErrorMessage, err := v.Validate(data)
	if err != nil {
		userJson, _ := json.Marshal(ValidationErrorMessage)
		fmt.Println(string(userJson))
		// [
		// 	{
		// 		"key": "username",
		// 		"message": [
		// 			"This field is required",
		// 			"This field must be a valid email address"
		// 		]
		// 	},
		// 	{
		// 		"key": "password",
		// 		"message": [
		// 			"This field must be a minimum 20"
		// 		]
		// 	},
		// 	{
		// 		"key": "confirm_password",
		// 		"message": [
		// 			"This field must be a equal with password"
		// 		]
		// 	},
		// 	{
		// 		"key": "children",
		// 		"message": [
		// 			{
		// 				"key": "level",
		// 				"message": [
		// 					"This field must be one of [beginner,intermediate,advanced]"
		// 				]
		// 			}
		// 		]
		// 	}
		// ]
	}
}

Rules

  • required
  • alpha
  • alphanum
  • number
  • numeric
  • email
  • latitude
  • longitude
  • ip
  • boolean
  • ipv4
  • ipv6
  • url
  • date
  • min
  • max
  • eqfield
  • enum

FAQs

Package last updated on 02 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts