owl

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 13 Imported by: 0

README

a zero dependency performant validation library

Install

go get github.com/aacebo/owl

Usage

schema := owl.String().Required()

if err := schema.Validate("..."); err != nil { // nil
	panic(err)
}

Features

Name Status
Any
Bool
Float
Int
String
Object
Array
Time
Union
Custom Error Messages
Custom Rules

Benchmarks

Benchmarks

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnySchema

type AnySchema []Rule

func Any

func Any() *AnySchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Any()

	if err := schema.Validate("..."); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(1); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(true); err != nil { // nil
		panic(err)
	}
}
Output:

func (*AnySchema) Enum

func (self *AnySchema) Enum(values ...any) *AnySchema

func (AnySchema) MarshalJSON

func (self AnySchema) MarshalJSON() ([]byte, error)

func (*AnySchema) Message

func (self *AnySchema) Message(message string) *AnySchema

func (*AnySchema) Required

func (self *AnySchema) Required() *AnySchema

func (*AnySchema) Rule

func (self *AnySchema) Rule(key string, value any, rule RuleFn) *AnySchema
Example
package main

import (
	"errors"
	"net"
	"reflect"

	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Any().Rule("ipv4", nil, func(value reflect.Value) (any, error) {
		if !value.IsValid() {
			return nil, nil
		}

		if ip := net.ParseIP(value.String()); ip == nil {
			return nil, errors.New("must be a valid ipv4 address")
		}

		return value.String(), nil
	})

	if err := schema.Validate("192.168.0.1"); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate("192.168.0"); err != nil { // error
		panic(err)
	}
}
Output:

func (AnySchema) Type

func (self AnySchema) Type() string

func (AnySchema) Validate

func (self AnySchema) Validate(value any) error

type ArraySchema

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

func Array

func Array(schema Schema) *ArraySchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Array(owl.String().Required())

	if err := schema.Validate([]string{"test"}); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate([]int{1}); err != nil { // error
		panic(err)
	}
}
Output:

func (ArraySchema) MarshalJSON

func (self ArraySchema) MarshalJSON() ([]byte, error)

func (*ArraySchema) Max

func (self *ArraySchema) Max(max int) *ArraySchema

func (*ArraySchema) Message

func (self *ArraySchema) Message(message string) *ArraySchema

func (*ArraySchema) Min

func (self *ArraySchema) Min(min int) *ArraySchema

func (*ArraySchema) Required

func (self *ArraySchema) Required() *ArraySchema

func (*ArraySchema) Rule

func (self *ArraySchema) Rule(key string, value any, rule RuleFn) *ArraySchema

func (ArraySchema) Type

func (self ArraySchema) Type() string

func (ArraySchema) Validate

func (self ArraySchema) Validate(value any) error

type BoolSchema

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

func Bool

func Bool() *BoolSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Bool()

	if err := schema.Validate(true); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(false); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate("test"); err != nil { // error
		panic(err)
	}
}
Output:

func (*BoolSchema) Enum

func (self *BoolSchema) Enum(values ...bool) *BoolSchema

func (BoolSchema) MarshalJSON

func (self BoolSchema) MarshalJSON() ([]byte, error)

func (*BoolSchema) Message

func (self *BoolSchema) Message(message string) *BoolSchema

func (*BoolSchema) Required

func (self *BoolSchema) Required() *BoolSchema

func (*BoolSchema) Rule

func (self *BoolSchema) Rule(key string, value any, rule RuleFn) *BoolSchema

func (BoolSchema) Type

func (self BoolSchema) Type() string

func (BoolSchema) Validate

func (self BoolSchema) Validate(value any) error

type Error

type Error struct {
	Rule    string  `json:"rule,omitempty"`
	Key     string  `json:"key,omitempty"`
	Message string  `json:"message,omitempty"`
	Errors  []error `json:"errors,omitempty"`
}

func NewEmptyError

func NewEmptyError(rule string, key string) Error

func NewError

func NewError(rule string, key string, message string) Error

func (Error) Add

func (self Error) Add(err error) Error

func (Error) Error

func (self Error) Error() string

func (Error) String

func (self Error) String() string

type ErrorGroup

type ErrorGroup []error

func (ErrorGroup) Error

func (self ErrorGroup) Error() string

func (ErrorGroup) String

func (self ErrorGroup) String() string

type FloatSchema

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

func Float

func Float() *FloatSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Float()

	if err := schema.Validate(1.0); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(1); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate("test"); err != nil { // error
		panic(err)
	}
}
Output:

func (*FloatSchema) Enum

func (self *FloatSchema) Enum(values ...float64) *FloatSchema

func (FloatSchema) MarshalJSON

func (self FloatSchema) MarshalJSON() ([]byte, error)

func (*FloatSchema) Max

func (self *FloatSchema) Max(max float64) *FloatSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Float().Max(5.0)

	if err := schema.Validate(5); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(5.5); err != nil { // error
		panic(err)
	}
}
Output:

func (*FloatSchema) Message

func (self *FloatSchema) Message(message string) *FloatSchema

func (*FloatSchema) Min

func (self *FloatSchema) Min(min float64) *FloatSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Float().Min(5.0)

	if err := schema.Validate(5); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(4.5); err != nil { // error
		panic(err)
	}
}
Output:

func (*FloatSchema) Required

func (self *FloatSchema) Required() *FloatSchema

func (*FloatSchema) Rule

func (self *FloatSchema) Rule(key string, value any, rule RuleFn) *FloatSchema

func (FloatSchema) Type

func (FloatSchema) Type() string

func (FloatSchema) Validate

func (self FloatSchema) Validate(value any) error

type IntSchema

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

func Int

func Int() *IntSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Int()

	if err := schema.Validate(1); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(1); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate("test"); err != nil { // error
		panic(err)
	}
}
Output:

func (*IntSchema) Enum

func (self *IntSchema) Enum(values ...int) *IntSchema

func (IntSchema) MarshalJSON

func (self IntSchema) MarshalJSON() ([]byte, error)

func (*IntSchema) Max

func (self *IntSchema) Max(max int) *IntSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Int().Max(5)

	if err := schema.Validate(5); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(6); err != nil { // error
		panic(err)
	}
}
Output:

func (*IntSchema) Message

func (self *IntSchema) Message(message string) *IntSchema

func (*IntSchema) Min

func (self *IntSchema) Min(min int) *IntSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Int().Min(5)

	if err := schema.Validate(5); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(4); err != nil { // error
		panic(err)
	}
}
Output:

func (*IntSchema) Required

func (self *IntSchema) Required() *IntSchema

func (*IntSchema) Rule

func (self *IntSchema) Rule(key string, value any, rule RuleFn) *IntSchema

func (IntSchema) Type

func (self IntSchema) Type() string

func (IntSchema) Validate

func (self IntSchema) Validate(value any) error

type ObjectSchema

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

func Object

func Object() *ObjectSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Object().Field(
		"email", owl.String().Email().Required(),
	).Field(
		"password", owl.String().Min(5).Max(20).Required(),
	)

	if err := schema.Validate(map[string]any{
		"email":    "test@test.com",
		"password": "mytestpassword",
	}); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(map[string]any{
		"email": "test@test.com",
	}); err != nil { // error
		panic(err)
	}
}
Output:

func (*ObjectSchema) Extend

func (self *ObjectSchema) Extend(schema *ObjectSchema) *ObjectSchema

func (*ObjectSchema) Field

func (self *ObjectSchema) Field(key string, schema Schema) *ObjectSchema

func (*ObjectSchema) Fields

func (self *ObjectSchema) Fields(fields map[string]Schema) *ObjectSchema

func (ObjectSchema) MarshalJSON

func (self ObjectSchema) MarshalJSON() ([]byte, error)

func (*ObjectSchema) Message

func (self *ObjectSchema) Message(message string) *ObjectSchema

func (*ObjectSchema) Required

func (self *ObjectSchema) Required() *ObjectSchema

func (*ObjectSchema) Rule

func (self *ObjectSchema) Rule(key string, value any, rule RuleFn) *ObjectSchema

func (ObjectSchema) Type

func (self ObjectSchema) Type() string

func (ObjectSchema) Validate

func (self ObjectSchema) Validate(value any) error

type Rule

type Rule struct {
	Key     string `json:"key"`
	Value   any    `json:"value"`
	Message string `json:"message"`
	Resolve RuleFn `json:"-"`
}

type RuleFn

type RuleFn func(value reflect.Value) (any, error)

type Schema

type Schema interface {
	Type() string
	Validate(value any) error
	// contains filtered or unexported methods
}

type StringSchema

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

func String

func String() *StringSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.String()

	if err := schema.Validate("test"); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(true); err != nil { // error
		panic(err)
	}
}
Output:

func (*StringSchema) Email

func (self *StringSchema) Email() *StringSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.String().Email()

	if err := schema.Validate("test@gmail.com"); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate("test"); err != nil { // error
		panic(err)
	}
}
Output:

func (*StringSchema) Enum

func (self *StringSchema) Enum(values ...string) *StringSchema

func (StringSchema) MarshalJSON

func (self StringSchema) MarshalJSON() ([]byte, error)

func (*StringSchema) Max

func (self *StringSchema) Max(max int) *StringSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.String().Max(5)

	if err := schema.Validate("test"); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate("tester"); err != nil { // error
		panic(err)
	}
}
Output:

func (*StringSchema) Message

func (self *StringSchema) Message(message string) *StringSchema

func (*StringSchema) Min

func (self *StringSchema) Min(min int) *StringSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.String().Min(5)

	if err := schema.Validate("tester"); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate("test"); err != nil { // error
		panic(err)
	}
}
Output:

func (*StringSchema) Regex

func (self *StringSchema) Regex(re *regexp.Regexp) *StringSchema
Example
package main

import (
	"regexp"

	"github.com/aacebo/owl"
)

func main() {
	schema := owl.String().Regex(regexp.MustCompile("^[0-9a-zA-Z_-]+$"))

	if err := schema.Validate("test"); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate("hello world"); err != nil { // error
		panic(err)
	}
}
Output:

func (*StringSchema) Required

func (self *StringSchema) Required() *StringSchema

func (*StringSchema) Rule

func (self *StringSchema) Rule(key string, value any, rule RuleFn) *StringSchema

func (StringSchema) Type

func (self StringSchema) Type() string

func (*StringSchema) URL

func (self *StringSchema) URL() *StringSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.String().URL()

	if err := schema.Validate("https://www.google.com"); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate("test"); err != nil { // error
		panic(err)
	}
}
Output:

func (*StringSchema) UUID

func (self *StringSchema) UUID() *StringSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.String().UUID()

	if err := schema.Validate("afefc1ab-b8f2-4803-8e9a-60515854141a"); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate("test"); err != nil { // error
		panic(err)
	}
}
Output:

func (StringSchema) Validate

func (self StringSchema) Validate(value any) error

type TimeSchema

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

func Time

func Time() *TimeSchema
Example
package main

import (
	"time"

	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Time()

	if err := schema.Validate(time.Now()); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(time.Now().Format(time.RFC3339)); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate("test"); err != nil { // error
		panic(err)
	}
}
Output:

func (*TimeSchema) Layout

func (self *TimeSchema) Layout(layout string) *TimeSchema

func (TimeSchema) MarshalJSON

func (self TimeSchema) MarshalJSON() ([]byte, error)

func (*TimeSchema) Max

func (self *TimeSchema) Max(max time.Time) *TimeSchema
Example
package main

import (
	"time"

	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Time().Max(time.Now())

	if err := schema.Validate(time.Now().AddDate(1, 0, 0)); err != nil { // error
		panic(err)
	}
}
Output:

func (*TimeSchema) Message

func (self *TimeSchema) Message(message string) *TimeSchema

func (*TimeSchema) Min

func (self *TimeSchema) Min(min time.Time) *TimeSchema
Example
package main

import (
	"time"

	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Time().Min(time.Now())

	if err := schema.Validate(time.Now().AddDate(-1, 0, 0)); err != nil { // error
		panic(err)
	}
}
Output:

func (*TimeSchema) Required

func (self *TimeSchema) Required() *TimeSchema

func (*TimeSchema) Rule

func (self *TimeSchema) Rule(key string, value any, rule RuleFn) *TimeSchema

func (TimeSchema) Type

func (self TimeSchema) Type() string

func (TimeSchema) Validate

func (self TimeSchema) Validate(value any) error

type UnionSchema

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

func Union

func Union(anyOf ...Schema) *UnionSchema
Example
package main

import (
	"github.com/aacebo/owl"
)

func main() {
	schema := owl.Union(
		owl.String().Required(),
		owl.Int().Required(),
	)

	if err := schema.Validate("test"); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(1); err != nil { // nil
		panic(err)
	}

	if err := schema.Validate(true); err != nil { // error
		panic(err)
	}
}
Output:

func (UnionSchema) MarshalJSON

func (self UnionSchema) MarshalJSON() ([]byte, error)

func (*UnionSchema) Message

func (self *UnionSchema) Message(message string) *UnionSchema

func (*UnionSchema) Rule

func (self *UnionSchema) Rule(key string, value any, rule RuleFn) *UnionSchema

func (UnionSchema) Type

func (self UnionSchema) Type() string

func (UnionSchema) Validate

func (self UnionSchema) Validate(value any) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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