schema

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2024 License: MIT Imports: 5 Imported by: 0

README

go-schema-validator

golang-schema-validator is a zod like API used to validation user input. It allows user to define a validation schema and add a custom message while creating validation schema and returns the custom message on validation failed.

One of the big pain point of using go-playground/validator is that it doesn't allow user to define custom message for validation error. This package provides a simple API to create a validation schema and add custom message for each validation rule.

This package is the wrapper around go-playground/validator package.

Install Package

go get github.com/iambpn/go-schema-validator/schema

Usage

package main

import (
	"fmt"
	"github.com/iambpn/go-schema-validator/schema"
)

func main() {
	// Simple field validation
	customSchema := schema.New().
		AddValidation("min=3", "Must be at least 3 characters").
		AddValidation("max=10", "Must be at most 10 characters").
		AddValidation("required", "This field is required")

	err := customSchema.Validate("hello")
	if err != nil {
		fmt.Println("Validation error:", err)
	}

	// Using helper methods
	emailSchema := schema.New().
		Email("Must be a valid email").
		Required("Email is required")

	err = emailSchema.Validate("not-an-email")
	if err != nil {
		fmt.Println("Email validation error:", err)
	}

	// Struct validation
	type User struct {
		Name  string
		Email string
		Age   int
	}

	userSchema := schema.New().Struct().
		Field("Name", schema.New().
			AddValidation("min=2", "Name must be at least 2 characters").
			AddValidation("max=50", "Name must be at most 50 characters")).
		Field("Email", schema.New().
			Email("Must be a valid email")).
		Field("Age", schema.New().
			Int("Age must be an integer").
			AddValidation("min=18", "Must be at least 18 years old").
			AddValidation("max=120", "Must be at most 120 years old"))

	user := User{
		Name:  "J",
		Email: "not-an-email",
		Age:   15,
	}

	err = userSchema.Validate(user)
	if err != nil {
		fmt.Println("User validation error:", err)
	}
}

Resources

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Schema

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

func New

func New() *Schema

func (*Schema) AddValidation

func (s *Schema) AddValidation(rule string, message ...string) *Schema

Generic method for adding validations

func (*Schema) Email

func (s *Schema) Email(message ...string) *Schema

Helper method for validating email

func (*Schema) Int

func (s *Schema) Int(message ...string) *Schema

Helper method for validating number

func (*Schema) Max

func (s *Schema) Max(val int, message ...string) *Schema

Helper method for validating max number or max length string

func (*Schema) Min

func (s *Schema) Min(val int, message ...string) *Schema

Helper method for validating min number or min length string

func (*Schema) Required

func (s *Schema) Required(message ...string) *Schema

Helper method for validating required fields

func (*Schema) Struct

func (s *Schema) Struct() *structValidation

Method to create a struct validation

func (*Schema) Validate

func (s *Schema) Validate(value interface{}) (err error)

Method to validate a non-struct value

Jump to

Keyboard shortcuts

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