Skip to content

Sdedelbrock/goschema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 

Repository files navigation

go schema

A library for validating and mutating JSON data into a struct by utilizing go struct tags. Designed to be a drop in replacement for json.Marshal/json.Unmarshal. Built to help normalize & mutate unsructured json into schema based structured formats suitable for storage.

go get github.com/Sdedelbrock/goschema

##usage

package main

import (
	"github.com/Sdedelbrock/goschema"
	"fmt"
)

type Person struct {
	// req means this field is required, if it is not present it will throw an error
	FirstName string `json:"first-name" schema:"req"`
	// truncate(n) will truncate the string to n characters
	LastName string `json:"last-name" schema:"req,truncate(4)"`
}

func main(){
	var i = Person{}
	err := schema.Unmarshal([]byte(`{}`), &i)
	fmt.Println(err)  // Schema: The Field FirstName is required
	err = schema.Unmarshal([]byte(`{"first-name":"Charlie", "last-name":"Chaplin"}`), &i)
	fmt.Println(err,i)  //<nil> {Charlie Chap}
}

About

JSON validation and manipulation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages