简体   繁体   English

如何将 JSON 模式中的默认值添加到 Java 中的 JSON 文档

[英]How to add default values from JSON schema to JSON document in Java

I need to validate a JSON document AND add default values defined in the schema if the values are missing in the given document.如果给定文档中缺少值,我需要验证 JSON 文档并添加模式中定义的默认值。 I'm using networknt/json-schema-validator for validation, but I'm not sure how to go about adding default values.我正在使用networknt/json-schema-validator进行验证,但我不确定如何添加默认值。 Is there a way to do that using the library above, or some other tool that would let me do that?有没有办法使用上面的库或其他一些工具来做到这一点?

There's not a well defined way to do this in JSON Schema.在 JSON Schema 中没有明确定义的方法来做到这一点。


While there is a "default" keyword in JSON Schema, it's primarily for user interface usage, to provide a default initial value.虽然 JSON Schema 中有一个“default”关键字,但它主要用于用户界面使用,以提供默认初始值。 Consider the schema:考虑架构:

{ "type":"string", "minLength":1, "default":"" }

In this case, the meaning is "when I create an instance of this schema, give it an initial value of an empty string."在这种情况下,意思是“当我创建这个模式的一个实例时,给它一个空字符串的初始值。”

First, note how this default document will still be invalid—it must be changed by the user before it will become valid.首先,请注意这个默认文档如何仍然无效——它必须由用户更改才能生效。 The "default" merely needs to be well-formed JSON. “默认”只需要格式良好的 JSON。 It's provided because, in this case, a blank string is a better default than an empty document (which would be invalid JSON).之所以提供它,是因为在这种情况下,空字符串比空文档(这将是无效的 JSON)更好。

Second, JSON Schema doesn't assume that that an undefined value is the same as the provided default value.其次,JSON Schema 不假设未定义的值与提供的默认值相同。 Filling in undefined properties with default values is allowed to change the meaning of the instance.允许使用默认值填充未定义的属性来更改实例的含义。 For example, given this schema:例如,给定这个架构:

{ "properties":
    "port": { "type":"number", "default": 80 }
}

It might be the case that an undefined property means one thing, but once I create that property, it should default to 80.一个未定义的属性可能意味着一件事,但是一旦我创建了该属性,它应该默认为 80。

Third, the "default" keyword doesn't even apply unless the instance exists so that the keyword may be considered.第三,“default”关键字甚至不适用,除非实例存在以便可以考虑该关键字。 If you're trying to fill in properties in an object, in order for the JSON Schema validator to "see" the "default" keyword, it has to first apply the instance against a schema with such a keyword.如果您尝试在对象中填充属性,为了让 JSON 模式验证器“看到”“默认”关键字,它必须首先将实例应用于具有此类关键字的模式。

Using the schema above, if you have an instance like {} , the validator will never even encounter the "default" keyword because the instance has no "port" property that would load the sub-schema containing it.使用上面的架构,如果您有一个像{}的实例,验证器甚至永远不会遇到“default”关键字,因为该实例没有会加载包含它的子架构的“端口”属性。


I'm not aware of a library that can do anything like what you're describing.我不知道一个图书馆可以做任何你所描述的事情。

However if you want to write one, I would suggest writing a schema like this instead:但是,如果你想写一个,我建议你写一个这样的模式:

{
  "default": {
    "port": 80
  },
  "properties": {
    "port": { "type":"integer" }
  }
}

This would make the value of the "default" keyword accessible to the validator, even when the "port" property is missing.这将使验证器可以访问“default”关键字的值,即使缺少“port”属性也是如此。 Your could then program a short script that merges in any missing properties.然后您可以编写一个简短的脚本来合并任何缺失的属性。

See https://github.com/json-schema-org/json-schema-spec/issues/858 for a similar in the JSON Schema issue tracker.请参阅https://github.com/json-schema-org/json-schema-spec/issues/858以了解 JSON Schema 问题跟踪器中的类似内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM