Skip to content

Latest commit

 

History

History
44 lines (37 loc) · 1.18 KB

README.md

File metadata and controls

44 lines (37 loc) · 1.18 KB

A simple json validator for Golang

The json.Unmarshal function ignores null values which may cause confusing results as discussed here. This package provides additional json tag to validate if the json bytes of specific field is null, zero value or has fields that is not defined in the model.

Usage

package main

import (
	"fmt"

	"github.com/wuxu92/json-validator"
)

type TestStruct struct {
	ID         int `json:"id"`
	NotNulLID  int `json:"not_null_id,omitempty,not_null"`
	NotZeroID  int `json:"not_zero_id,not_zero"`
	RequiredID int `json:"required_id,required"`
	Embed      struct {
		NotNulLID  int `json:"not_null_id,omitempty,not_null"`
		NotZeroID  int `json:"not_zero_id,not_zero"`
		RequiredID int `json:"required_id,required"`
	} `json:"embed,required"`
}

func main() {
	data := []byte(`{
        "id": 1,
        "not_null_id": null,
        "not_zero_id": 0,
        "required_id": 1,
        "embed": {
            "not_null_id": null,
            "not_zero_id": 0,
            "required_id": 1
        }
    }`)
	if err := Validate(data, TestStruct{}, WithNoRedundant); err != nil {
		fmt.Printf("validate with: %+v", err)
	}
}