首页 > Web开发 > 详细

golang-web参数校验

时间:2021-04-25 23:36:14      阅读:25      评论:0      收藏:0      [点我收藏+]

golang-web参数校验

json schema

package main

import (
    "fmt"
    "github.com/xeipuuv/gojsonschema"
)

func main() {

    schemaLoader := gojsonschema.NewReferenceLoader("file:///home/me/schema.json")
    documentLoader := gojsonschema.NewReferenceLoader("file:///home/me/document.json")

    result, err := gojsonschema.Validate(schemaLoader, documentLoader)
    if err != nil {
        panic(err.Error())
    }

    if result.Valid() {
        fmt.Printf("The document is valid\n")
    } else {
        fmt.Printf("The document is not valid. see errors :\n")
        for _, desc := range result.Errors() {
            fmt.Printf("- %s\n", desc)
        }
    }
}

json转json-schema

在线转换:
https://www.jsonschema.net/home
https://easy-json-schema.github.io/
https://www.liquid-technologies.com/online-json-to-schema-converter

Python转换:

https://github.com/gonvaled/jskemator
https://github.com/perenecabuto/json_schema_generator
https://github.com/rnd0101/json_schema_inferencer
https://pypi.python.org/pypi/genson/
https://pypi.python.org/pypi/skinfer

NodeJS转换:

https://github.com/Nijikokun/generate-schema
https://github.com/easy-json-schema/easy-json-schema

红宝石:

https://github.com/maxlinc/json-schema-generator

NodeJS:

npm install generate-schema --save

var GenerateSchema = require(‘generate-schema‘)

// Capture Schema Output
var schema = GenerateSchema.json(‘Product‘, [
    {
        "id": 2,
        "name": "An ice sculpture",
        "price": 12.50,
        "tags": ["cold", "ice"],
        "dimensions": {
            "length": 7.0,
            "width": 12.0,
            "height": 9.5
        },
        "warehouseLocation": {
            "latitude": -78.75,
            "longitude": 20.4
        }
    },
    {
        "id": 3,
        "name": "A blue mouse",
        "price": 25.50,
        "dimensions": {
            "length": 3.1,
            "width": 1.0,
            "height": 1.0
        },
        "warehouseLocation": {
            "latitude": 54.4,
            "longitude": -32.7
        }
    }
])

参考:https://www.ctolib.com/generate-schema.html
json-chema转golang struct: https://zhuanlan.zhihu.com/p/350162109

相关链接

https://www.lsdcloud.com/go/middleware/json-schema.html#_1-什么是json
https://www.cnblogs.com/makelu/p/11828274.html
http://json-schema.org/implementations.html#validator-go
https://github.com/xeipuuv/gojsonschema
https://github.com/santhosh-tekuri/jsonschema

golang-web参数校验

原文:https://www.cnblogs.com/tomtellyou/p/14701515.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!