go 语言校验器

package main

import (
	"fmt"
	"github.com/faceair/jio"
	"log"
)

func typeof(v interface{}) string {
	return fmt.Sprintf("%T", v)
}

func main() {
	data := []byte(`{
        "debug": "on",
        "window": {
            "title": "Sample Widget",
            "size": [500, 500]
        }
    }`)
	// json的校验器,传入bytes[]第一个参数,第二个参数是校验规则(就是一个结构)
	_, err := jio.ValidateJSON(&data, jio.Object().Keys(jio.K{  // 这就表示是字典的结构
		//  表示debug接受的是一个bool值,使用truthy表示on代表true,required表示必须
		"debug": jio.Bool().Truthy("on").Required(),
		"window": jio.Object().Keys(jio.K{
			// 约束最大和最小长度
			"title": jio.String().Min(3).Max(18),
			"size":  jio.Array().Items(jio.Number().Integer()).Length(2).Required(),
		}).Without("name", "title").Required(), // 不禁止这些键存在
	}))
	if err != nil {
		panic(err)
	}
	log.Printf("%s", data) // {"debug":true,"window":{"size":[500,500],"title":"Sample Widget"}}
}

  

官网文档:https://github.com/faceair/jio

原文地址:https://www.cnblogs.com/double-W/p/13306453.html