Z - Go

A simple variant of Zod but for Go.

Installation

go get github.com/m10rten/suite/pkg

Usage

package main
 
import (
  "fmt"
 
  z "github.com/m10rten/suite/pkg"
)
 
var (
  z = Z.Z
)
 
func main() {
  schema := z.Object(map[string]Z.Validator{
		"name": z.String(),
		"age":  z.Number(),
	})
 
	unknownData := map[string]interface{}{
		"name": "John",
		"age":  25,
	}
	data, success, err := schema.Parse(unknownData)
}

API

Z

Z.Z

func (z Z) Z() Z

StringValidator

Z.String

func (z Z) String() StringValidator

StringValidator.Parse

func (sv StringValidator) Parse(input interface{}) (interface{}, bool, error)

NumberValidator

Z.Number

func (z Z) Number() NumberValidator

NumberValidator.Parse

func (nv NumberValidator) Parse(input interface{}) (interface{}, bool, error)

ObjectValidator

Z.Object

func (z Z) Object(schema map[string]Validator) ObjectValidator

ObjectValidator.Parse

func (ov ObjectValidator) Parse(input interface{}) (map[string]interface{}, bool, error)

ParsedData

type ParsedData map[string]interface{}

Validator

This is what you need to implement to create a custom validator.

type Validator interface {
  Parse(interface{}) (interface{}, bool, error)
}