schema package - github.com/Phamiliarize/toml-schema - Go Packages

schema

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 3 Imported by: 0

README

toml-schema

toml-schema utilizes TOML to define validation schemas for input in a simple manner. Under the hood, we generate a map[string]interface{} rule definition that can be used by go-playground/validator.

It was created for use in the sabaresu serverless framework to enable configuration-based validation.

Getting Started

Schema

Schema are written in TOML, like so:

# key = "validators"
name = "required,min=1,max=128"
age = "required,number,min=1,max=150"
ecash = "number,required,min=0,max=150000"
is_cool = "required"

# A nested map can be added with [key]
[location]
address1 = "required"
address2 = "required"

# A slice is added with [[key]]

[[ships]]
id = "required,uuid"
make = "oneof=x-wing y-wing a-wing millenium falcon tie-fighter"

Validate

To validate data against your schema and constraints:

dogShelter := LoadSchema(dogShelterSchema)
err := dogShelter.Validate(myData)
if err != nil {
    panic(err)
}

Supported Types & Constraints

toml-schema bases it's typing with JSON and Lua in mind; this covers the majority of cases.

Type GoLang Type
string string
number float64
boolean bool
array []interface{}
object map[string]interface{}
nil nil

The supported constraints/validations are basically inline with that types offerings for go-playground/validator.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Validator added in v0.1.1

type Validator struct {
	Validator *validate.Validate
	// contains filtered or unexported fields
}

func NewValidator

func NewValidator(v *validate.Validate) Validator

func (*Validator) LoadSchema added in v0.1.1

func (v *Validator) LoadSchema(name string, tomlSchema string) error

Loads schema file and passes it through recursive function to generate a rules map

func (*Validator) ValidateSchema added in v0.1.1

func (v *Validator) ValidateSchema(name string, data map[string]interface{}) map[string]interface{}

Jump to

Keyboard shortcuts

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