golang-web参数校验

2021-05-29 21:02

阅读:552

标签:ejs   gen   infer   not   ola   document   range   相关   package   

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参数校验

标签:ejs   gen   infer   not   ola   document   range   相关   package   

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


评论


亲,登录后才可以留言!