JSON模式验证的问题

发布于 2025-02-10 08:51:18 字数 1781 浏览 2 评论 0原文

我遇到了JSON模式验证的问题。

我有一个像这样的Java类

package com.ebc.jackson.exampe.data;

public class FormElement {
    
    private String fieldName;
    
    private String fieldType;
    
    private Object value;

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public String getFieldType() {
        return fieldType;
    }

    public void setFieldType(String fieldType) {
        this.fieldType = fieldType;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }
}

,我正在使用以下代码来生成架构:

JsonSchemaGenerator generator =
                    new JsonSchemaGenerator(objectMapper);
            JsonNode schema = generator.generateJsonSchema(FormElement.class);
            String strSchema = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);

生成以下模式

{
  "$schema" : "http://json-schema.org/draft-04/schema#",
  "title" : "Form Element",
  "type" : "object",
  "additionalProperties" : false,
  "properties" : {
    "fieldName" : {
      "type" : "string"
    },
    "fieldType" : {
      "type" : "string"
    },
    "value" : {
      "$ref" : "#/definitions/Object"
    }
  },
  "definitions" : {
    "Object" : {
      "type" : "object",
      "additionalProperties" : false,
      "properties" : { }
    }
  }
}

此代码在请求中

{
  "fieldName": "user-name",
  "fieldType": "textInput",
  "value": "Alex"
}

,我可以接收一个JSON对象,例如我尝试针对JSON模式验证此对象时是无效的,它期望一对“键:值”对,但是找到了一个字符串。 在Java类中,值属性是对象,因为它可能是字符串,整数,布尔值等。

我的问题是,如何生成一个模式以支持“值”属性的不同数据类型?

I'm running into an issue with a Json schema validation.

I have a Java class like this

package com.ebc.jackson.exampe.data;

public class FormElement {
    
    private String fieldName;
    
    private String fieldType;
    
    private Object value;

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public String getFieldType() {
        return fieldType;
    }

    public void setFieldType(String fieldType) {
        this.fieldType = fieldType;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }
}

I'm using below code to generate the schema:

JsonSchemaGenerator generator =
                    new JsonSchemaGenerator(objectMapper);
            JsonNode schema = generator.generateJsonSchema(FormElement.class);
            String strSchema = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);

this code generates below schema

{
  "$schema" : "http://json-schema.org/draft-04/schema#",
  "title" : "Form Element",
  "type" : "object",
  "additionalProperties" : false,
  "properties" : {
    "fieldName" : {
      "type" : "string"
    },
    "fieldType" : {
      "type" : "string"
    },
    "value" : {
      "$ref" : "#/definitions/Object"
    }
  },
  "definitions" : {
    "Object" : {
      "type" : "object",
      "additionalProperties" : false,
      "properties" : { }
    }
  }
}

In the request, I could receive a Json Object like

{
  "fieldName": "user-name",
  "fieldType": "textInput",
  "value": "Alex"
}

When I try to validate this object against the Json schema, "value" attribute is not valid, it is expecting a "key: value" pair but, a string is found.
In the Java class the value attribute is Object because it could be a String, Integer, Boolean, etc.

My question is, how can I generate a schema to support different data types for "value" attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风疏影 2025-02-17 08:51:18

您可以使用架构的任何属性:

尝试以下一个:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Form Element",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "fieldName": {
      "type": "string"
    },
    "fieldType": {
      "type": "string"
    },
    "value": {
      "$ref": "#/definitions/Object"
    }
  },
  "definitions": {
    "Object": {
      "format": "",
      "anyOf": [
        {
          "type": "object"
        },
        {
          "type": "string"
        },
        {
          "type": "number"
        },
        {
          "type": "integer"
        },
        {
          "type": "boolean",
          "x-enumFlags": true
        },
        {
          "type": "null"
        }
      ],
      "additionalProperties": false,
      "properties": {}
    }
  }
}

或者可以尝试使用类型的管道分离器。在提供的架构中,只需将类型的值更改为“类型”:“字符串| object | object | integer | number”。该符号对我不起作用,但是您可以检查它是否适合您。

You can use the anyOf property of schema :

Try the below one :

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Form Element",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "fieldName": {
      "type": "string"
    },
    "fieldType": {
      "type": "string"
    },
    "value": {
      "$ref": "#/definitions/Object"
    }
  },
  "definitions": {
    "Object": {
      "format": "",
      "anyOf": [
        {
          "type": "object"
        },
        {
          "type": "string"
        },
        {
          "type": "number"
        },
        {
          "type": "integer"
        },
        {
          "type": "boolean",
          "x-enumFlags": true
        },
        {
          "type": "null"
        }
      ],
      "additionalProperties": false,
      "properties": {}
    }
  }
}

Or you could try the pipe separator in type.In the schema you provided just change the value of type to "type": "string|object|integer|number". This notation didn't work for me in swagger but you can check if it works for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文