自定义构建的JSON模式无法正确验证

时间:2018-08-22 22:27:30

标签: go jsonschema

我有一个自定义构建的JSON模式,其中只有几个顶级。这里的问题是它不能将所有内容都验证到100%。例如,它仅检测出4个字段中的2个,而必填字段根本不起作用,其他属性也不起作用,等等。我在json模式中使用this library

{
    "users": {
        "PUT": {
          "definitions": {},
          "$schema": "http://json-schema.org/draft-07/schema#",
          "$id": "http://example.com/root.json",
          "type": "object",
          "title": "The Root Schema",
          "required": [
            "DisplayName",
            "Username",
            "Email",
            "Password"
          ],
          "properties": {
            "DisplayName": {
              "$id": "#/properties/DisplayName",
              "type": "string",
              "title": "The Displayname Schema",
              "default": "",
              "examples": [
                ""
              ],
              "minLength": 3,
              "maxLength": 24,
              "pattern": "^(.*)$"
            },
            "Username": {
              "$id": "#/properties/Username",
              "type": "string",
              "title": "The Username Schema",
              "default": "",
              "examples": [
                ""
              ],
              "minLength": 3,
              "maxLength": 15,
              "pattern": "^(.*)$"
            },
            "Email": {
              "$id": "#/properties/Email",
              "type": "string",
              "title": "The Email Schema",
              "default": "",
              "examples": [
                ""
              ],
              "minLength": 7,
              "pattern": "^(.*)$",
              "format": "email"
            },
            "Password": {
              "$id": "#/properties/Password",
              "type": "string",
              "title": "The Password Schema",
              "default": "",
              "examples": [
                ""
              ],
              "pattern": "^(.*)$"
            }
        },
        "additionalProperties": false
        }
    }
}

我正在解析所有这样的内容:

func Validate(data interface{}, r *http.Request) (interface{}, error) {
    // Convert the data struct to a readable JSON bytes
    JSONparams, err := json.Marshal(data)
    if err != nil {
        return nil, err
    }

    // Split URL segments so we know what part of the API they are accessing
    modules := strings.Split(r.URL.String(), "/")
    modules = modules[(len(modules) - 1):]

    // Read the schema file
    fileSchema, _ := ioutil.ReadFile("config/schema/schema.json")
    var object interface{}

    // Unmarshal it so we can choose what schema we specifically want
    err = json.Unmarshal(fileSchema, &object)
    if err != nil {
        log.Fatal(err)
    }

    // Choose the preferred schema
    encodedJSON, err := json.Marshal(object.(map[string]interface{})[strings.Join(modules, "") + "s"].(map[string]interface{})[r.Method])
    if err != nil {
        log.Fatal(err)
    }

    // Load the JSON schema
    schema := gojsonschema.NewStringLoader(string(encodedJSON))

    // Load the JSON params
    document := gojsonschema.NewStringLoader(string(JSONparams))

    // Validate the document
    result, err := gojsonschema.Validate(schema, document)
    if err != nil {
        return nil, err
    }

    if !result.Valid() {
        // Map the errors into a new array
        var errors = make(map[string]string)
        for _, err := range result.Errors() {
            errors[err.Field()] = err.Description()
        }

        // Convert the array to an interface that we can convert to JSON
        resultMap := map[string]interface{}{
            "success": false,
            "result": map[string]interface{}{},
            "errors": errors,
        }

        // Convert the interface to a JSON object
        errorObject, err := json.Marshal(resultMap)
        if err != nil {
            return nil, err
        }

        return errorObject, nil
    }

    return nil, nil
}

type CreateParams struct {
    DisplayName     string
    Username        string
    Email           string
    Password        string
}

var (
    response interface{}
    status int = 0
)

func Create(w http.ResponseWriter, r *http.Request) {
    status = 0

    // Parse the request so we can access the query parameters
    r.ParseForm()

    // Assign them to the interface variables
    data := &CreateParams{
        DisplayName: r.Form.Get("DisplayName"),
        Username: r.Form.Get("Username"),
        Email: r.Form.Get("Email"),
        Password: r.Form.Get("Password"),
    }

    // Validate the JSON data
    errors, err := schema.Validate(data, r)

    if err != nil {
        responseJSON  := map[string]interface{}{
            "success": false,
            "result": map[string]interface{}{},
        }

        log.Fatal(err.Error())

        response, err = json.Marshal(responseJSON)
        status = http.StatusInternalServerError
    }

    // Catch any errors generated by the validator and assign them to the response interface
    if errors != nil {
        response = errors
        status = http.StatusBadRequest
    }

    // Status has not been set yet, so it's safe to assume that everything went fine
    if status == 0 {
        responseJSON  := map[string]interface{}{
            "success": true,
            "result": map[string]interface{} {
                "DisplayName": data.DisplayName,
                "Username": data.Username,
                "Email": data.Email,
                "Password": nil,
            },
        }

        response, err = json.Marshal(responseJSON)
        status = http.StatusOK
    }

    // We are going to respond with JSON, so set the appropriate header
    w.Header().Set("Content-Type", "application/json")

    // Write the header and the response
    w.WriteHeader(status)
    w.Write(response.([]byte))
}

之所以这样做,是因为我正在构建REST API,并且如果api/auth/user收到PUT请求,我希望能够为“用户”特别指定数据要求零件采用PUT方法。

有什么想法可以实现吗?

编辑: 我的json数据:

{
  "DisplayName": "1234",
  "Username": "1234",
  "Email": "test@gmail.com",
  "Password": "123456"
}

编辑2: 该数据应与架构一起失败。

{
  "DisplayName": "1", // min length is 3
  "Username": "", // this field is required but is empty here
  "Email": "testgmail.com", // not following the email format
  "Password": "123456111111111111111111111111111111111111111111111" // too long
}

1 个答案:

答案 0 :(得分:0)

如果我使用gojsonschema手动加载架构和数据,它将按预期工作。我怀疑由于您以某种复杂的方式加载架构,因此您放入的架构最终与您期望的有所不同,但是由于您的代码示例都是基于HTTP的,所以我无法真正自己对其进行测试