Giter VIP home page Giter VIP logo

toml-schema's Introduction

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 = "string,required,min=1,max=128"
age = "number,required,int,min=1,max=150"
ecash = "number,required,min=0,max=150000"
is_cool = "boolean,required"

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

# A slice is added with [[key]]

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

Validate

To validate data against your schema and constraints:

import (
    "github.com/Phamiliarize/toml-schema"
    "github.com/go-playground/validator/v10"
)

// Use any v10 instance of a go-playground/validator
// This allows you to extend the potential validators
v := schema.NewValidator(validator.New())
err := v.LoadSchema("mySchema", `test = "string,required"`)
if err != nil {
    panic(err)
}

// Returns a map[string]validation.ValidationErrors
err := v.ValidateSchema("mySchema", myData)
if err != nil {
    panic(err)
}

Basic Types & Constraints

The supported constraints/validations are basically inline with go-playground/validator but since the end usecase is for JSON and Lua we require a "type constraint" on all fields.

Every field must start with a basic type. The basic types supported are:

Type GoLang Type Validators
string string string
number float64 or int number
boolean bool boolean

toml-schema's People

Contributors

phamiliarize avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.