Home

Awesome

omg.jsonParser

Go Go Reference Coverage Status Go Report Card

omg.jsonParser is a simple JSON parser with a simple condition validation. It's a wrapper on standard go JSON lib. With it help you can add the validation condition via golang structure fields tags.

Example

package main

import (
	"fmt"

	"github.com/dedalqq/omg.jsonparser"
)

func main() {
	jsonData := `{"name": ""}`

	st := struct {
		Name string `json:"name,notEmpty"` // added notEmpty for enable validation for it field
	}{}

	err := jsonparser.Unmarshal([]byte(jsonData), &st)
	if err != nil {
		fmt.Println(err.Error()) // print: value [name] must be not empty
	}
}

Tag struct

json:"[name][,option]..."

Tag examples

Here are some the tag examples:

package main

type data struct {
    F1 string   `json:"f1,notEmpty"`          // the field must be not empty but can be "null" or may not exist
    F2 string   `json:"f2,notEmpty,required"` // the field is required and must be not empty but may be the "null" value
    F3 string   `json:"f3,notEmpty,notNull"`  // the field must be not empty and not "null" but may not exist
    F4 []string `json:"f4,notNull,min:3"`     // the field must be not "null" and contains 3 or more items but may not exist
}

Available tags options

NameTypesDescription
requiredanyThe field must be exist with any value or null
notEmptyanyThe field can be not exist but if exist value must be not zero value but can be null
notEmptysliceThe field must have one element or more but may be null or not exist
notNullanyThe field should not be null, but may not exist
uniq[]stringThe strings slice must contains only unique strings
min:nsliceThe slice must have n items or more
max:nsliceThe slice must have n items or less
min:nstringThe string must have n runes or more
max:nstringThe string must have n runes or less
min:nint/uintThe value must be equals n or more
max:nint/uintThe value must be equals n or less