堆栈溢出| Google Group |吉特聊天| subreddit | YouTube |文档|贡献指南|
这是JSON Schema核心验证的JSON Schema Core Draft V4,V6,V7,V2019-09和V2020-12的JSON CORE DRAFT V4,V6,V7,V7,V2019-09的JAVA实现。该实现支持自定义元框架,词汇,关键字和格式。
此外,使用适当的元智能支持OpenAPI 3请求/响应验证。对于想要根据模式从JSON节点收集信息的用户,步行者可以提供帮助。使用的JSON解析器是Jackson Parser。由于它是我们的Light-4J微服务框架中的一个关键组件,可以在运行时验证RESTAPI规范的请求/响应针对Light-Rest-4J和RPC架构的openapi规范,因此性能是设计中最重要的方面。
有关每个版本的兼容性支持的信息,包括已知问题,可以在与JSON模式版本文档的兼容性中找到。
自2019 - 09年草案以来, format
关键字仅默认生成注释,并且不会产生断言。
可以通过将setFormatAssertionsEnabled
设置为true
在SchemaValidatorsConfig
或ExecutionConfig
中的true来生成断言。
该库可以包含可能需要更改代码更改的minor
版本的破坏更改。
升级库时著名或破坏更改的信息可以在升级到新版本文档中找到。
发布页面将包含有关最新版本的信息。
Creek的JSON模式验证比较项目对基于JVM的模式验证实现进行了丰富的比较,该实现比较了许多不同Java实现的功能和性能特征。
Bowtie项目的报告比较了不同实现的功能特征,包括非Java实现,但没有进行任何性能基准测试。
这应该是最快的Java JSON Schema验证器实现。
以下是使用Java Microbench Markness的JSON Schema验证器Perftest项目的基准结果。
请注意,基准结果高度取决于用于验证的输入数据工作负载和模式。
在这种情况下,此工作负载使用4个规范草案,并在很大程度上测试了评估properties
关键字的性能。您可以参考基于JSSON的JSON Schema验证实现基准结果的绩效比较结果,以进行更典型的工作负载
如果性能是一个重要的考虑因素,则应对特定的样本工作负载进行基准测试,因为使用某些关键字时的性能特征不同。例如,使用unevaluatedProperties
或unevaluatedItems
关键字将在相关验证器中触发注释集合,例如properties
或items
验证器,注释收集将对性能产生不利影响。
Benchmark Mode Cnt Score Error Units
NetworkntBenchmark.testValidate thrpt 10 8352.126 ± 61.870 ops/s
NetworkntBenchmark.testValidate:gc.alloc.rate thrpt 10 721.296 ± 5.342 MB/sec
NetworkntBenchmark.testValidate:gc.alloc.rate.norm thrpt 10 90560.013 ± 0.001 B/op
NetworkntBenchmark.testValidate:gc.count thrpt 10 61.000 counts
NetworkntBenchmark.testValidate:gc.time thrpt 10 68.000 ms
Benchmark Mode Cnt Score Error Units
EveritBenchmark.testValidate thrpt 10 3775.453 ± 44.023 ops/s
EveritBenchmark.testValidate:gc.alloc.rate thrpt 10 1667.345 ± 19.437 MB/sec
EveritBenchmark.testValidate:gc.alloc.rate.norm thrpt 10 463104.030 ± 0.003 B/op
EveritBenchmark.testValidate:gc.count thrpt 10 140.000 counts
EveritBenchmark.testValidate:gc.time thrpt 10 158.000 ms
针对JSON模式测试套件测试了此实现。随着测试不断添加到套件中,这些测试结果可能不是最新的。
实施 | 全面的 | draft_03 | draft_04 | draft_06 | draft_07 | draft_2019_09 | draft_2020_12 |
---|---|---|---|---|---|---|---|
网络 | 通过:R:4803(100.0%)O:2372(100.0%) 失败:R:0(0.0%)O:0(0.0%) | 通过:R:610(100.0%)O:251(100.0%) 失败:R:0(0.0%)O:0(0.0%) | 通过:R:822(100.0%)o:318(100.0%) 失败:R:0(0.0%)O:0(0.0%) | 通过:R:906(100.0%)o:541(100.0%) 失败:R:0(0.0%)O:0(0.0%) | 通过:R:1220(100.0%)o:625(100.0%) 失败:R:0(0.0%)O:0(0.0%) | 通过:R:1245(100.0%)o:637(100.0%) 失败:R:0(0.0%)O:0(0.0%) |
JoniRegularExpressionFactory
进行pattern
和format
regex
测试。 该图书馆使用Jackson,这是Java JSON解析器,在其他项目中广泛使用。如果您已经在项目中使用了Jackson解析器,那么选择此库而不是其他图书馆进行模式验证是很自然的。
该库与JSON和YAML一起在模式定义和输入数据上使用。
OpenAPI 3.0规范使用JSON模式来验证请求/响应,但是存在一些差异。使用配置文件,您可以使库与OpenAPI 3.0验证一起使用。
遵循光平台的设计原理,该库具有最小的依赖关系,以确保使用时没有依赖性冲突。
以下是当包含此库时将自动包含的依赖项。
< dependency >
<!-- Used for logging -->
< groupId >org.slf4j</ groupId >
< artifactId >slf4j-api</ artifactId >
< version >${version.slf4j}</ version >
</ dependency >
< dependency >
<!-- Used to process JSON -->
< groupId >com.fasterxml.jackson.core</ groupId >
< artifactId >jackson-databind</ artifactId >
< version >${version.jackson}</ version >
</ dependency >
< dependency >
<!-- Used to process YAML -->
< groupId >com.fasterxml.jackson.dataformat</ groupId >
< artifactId >jackson-dataformat-yaml</ artifactId >
< version >${version.jackson}</ version >
</ dependency >
< dependency >
<!-- Used to validate RFC 3339 date and date-time -->
< groupId >com.ethlo.time</ groupId >
< artifactId >itu</ artifactId >
< version >${version.itu}</ version >
</ dependency >
以下是某些选项可能需要的可选依赖项。
这些不会自动包含,并且在不添加库的情况下设置相关选项将导致ClassNotFoundException
。
< dependency >
<!-- Used to validate ECMA 262 regular expressions -->
<!-- Approximately 50 MB in dependencies -->
<!-- GraalJSRegularExpressionFactory -->
< groupId >org.graalvm.js</ groupId >
< artifactId >js</ artifactId >
< version >${version.graaljs}</ version >
</ dependency >
< dependency >
<!-- Used to validate ECMA 262 regular expressions -->
<!-- Approximately 2 MB in dependencies -->
<!-- JoniRegularExpressionFactory -->
< groupId >org.jruby.joni</ groupId >
< artifactId >joni</ artifactId >
< version >${version.joni}</ version >
</ dependency >
以下是自动包含的所需依赖项,但如果不需要,则可以明确排除。
如果不需要,则可以排除YAML依赖性。试图处理YAML的模式或输入将导致ClassNotFoundException
。
< dependency >
< groupId >com.networknt</ groupId >
< artifactId >json-schema-validator</ artifactId >
< exclusions >
< exclusion >
< groupId >com.fasterxml.jackson.dataformat</ groupId >
< artifactId >jackson-dataformat-yaml</ artifactId >
</ exclusion >
</ exclusions >
</ dependency >
如果不需要准确验证date-time
格式,则可以排除ETHLO时间依赖关系。然后, date-time
格式将使用java.time.OffsetDateTime
确定date-time
是否有效。
< dependency >
< groupId >com.networknt</ groupId >
< artifactId >json-schema-validator</ artifactId >
< exclusions >
< exclusion >
< groupId >com.ethlo.time</ groupId >
< artifactId >itu</ artifactId >
</ exclusion >
</ exclusions >
</ dependency >
这个图书馆非常活跃,有很多贡献者。团队成员迅速处理新功能和错误修复。因为它是同一github组织中Light-4J框架的必要依赖性,所以它将与框架一起进化和维护。
图书馆支持Java 8及以上。如果要从源代码构建,则需要本地安装JDK 8。要支持JDK的多个版本,您可以使用SDKMAN
此包装可在Maven Central上找到。
< dependency >
< groupId >com.networknt</ groupId >
< artifactId >json-schema-validator</ artifactId >
< version >1.5.3</ version >
</ dependency >
dependencies {
implementation ( group : 'com.networknt' , name : 'json-schema-validator' , version : '1.5.3' );
}
以下示例说明了如何针对模式验证输入。它包括以下步骤。
schemaMappers
将$id
映射到检索URI。schemaLoaders
使用检索URI配置模式的加载方式。例如, Map<String, String> schemas
该模式包含检索URI作为String
数据的映射,可以通过使用builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas(schemas))
。这也接受Function<String, String> schemaRetrievalFunction
。 // This creates a schema factory that will use Draft 2020-12 as the default if $schema is not specified
// in the schema data. If $schema is specified in the schema data then that schema dialect will be used
// instead and this version is ignored.
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory . getInstance ( VersionFlag . V202012 , builder ->
// This creates a mapping from $id which starts with https://www.example.org/ to the retrieval URI classpath:schema/
builder . schemaMappers ( schemaMappers -> schemaMappers . mapPrefix ( "https://www.example.org/" , "classpath:schema/" ))
);
SchemaValidatorsConfig . Builder builder = SchemaValidatorsConfig . builder ();
// By default the JDK regular expression implementation which is not ECMA 262 compliant is used
// Note that setting this requires including optional dependencies
// builder.regularExpressionFactory(GraalJSRegularExpressionFactory.getInstance());
// builder.regularExpressionFactory(JoniRegularExpressionFactory.getInstance());
SchemaValidatorsConfig config = builder . build ();
// Due to the mapping the schema will be retrieved from the classpath at classpath:schema/example-main.json.
// If the schema data does not specify an $id the absolute IRI of the schema location will be used as the $id.
JsonSchema schema = jsonSchemaFactory . getSchema ( SchemaLocation . of ( "https://www.example.org/example-main.json" ), config );
String input = "{ r n "
+ " " main " : { r n "
+ " " common " : { r n "
+ " " field " : " invalidfield " r n "
+ " } r n "
+ " } r n "
+ "}" ;
Set < ValidationMessage > assertions = schema . validate ( input , InputFormat . JSON , executionContext -> {
// By default since Draft 2019-09 the format keyword only generates annotations and not assertions
executionContext . getExecutionConfig (). setFormatAssertionsEnabled ( true );
});
下面的示例说明了如何针对元智能验证模式。
这实际上与针对模式的验证输入相同,除非在这种情况下,输入是模式,使用的架构是元式式的。
请注意,草案4,草案6,草稿7,2019-09和2020-12草案的元框架与图书馆捆绑在一起,默认情况下将使用这些类Path的资源。
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory . getInstance ( VersionFlag . V202012 );
SchemaValidatorsConfig . Builder builder = SchemaValidatorsConfig . builder ();
// By default the JDK regular expression implementation which is not ECMA 262 compliant is used
// Note that setting this requires including optional dependencies
// builder.regularExpressionFactory(GraalJSRegularExpressionFactory.getInstance());
// builder.regularExpressionFactory(JoniRegularExpressionFactory.getInstance());
SchemaValidatorsConfig config = builder . build ();
// Due to the mapping the meta-schema will be retrieved from the classpath at classpath:draft/2020-12/schema.
JsonSchema schema = jsonSchemaFactory . getSchema ( SchemaLocation . of ( SchemaId . V202012 ), config );
String input = "{ r n "
+ " " type " : " object " , r n "
+ " " properties " : { r n "
+ " " key " : { r n "
+ " " title " : " My key " , r n "
+ " " type " : " invalidtype " r n "
+ " } r n "
+ " } r n "
+ "}" ;
Set < ValidationMessage > assertions = schema . validate ( input , InputFormat . JSON , executionContext -> {
// By default since Draft 2019-09 the format keyword only generates annotations and not assertions
executionContext . getExecutionConfig (). setFormatAssertionsEnabled ( true );
});
库生成以下类型的结果。
类型 | 描述 |
---|---|
断言 | 由关键字在特定输入数据实例上生成的验证错误。这通常在ValidationMessage 或OutputUnit 中进行描述。请注意,自2019 - 09年草案以来, format 关键字不再默认地生成断言,而是仅生成注释,除非使用配置选项或使用使用适当词汇的元智能进行另外配置。 |
注释 | 由关键字为特定输入数据实例生成的其他信息。这通常在OutputUnit 中描述。默认情况下关闭注释和报告。关键字(例如unevaluatedProperties 或未unevaluatedItems 要求的注释始终是为了评估目的,并且不能被禁用,但除非配置为此,否则不会被报告。 |
以下信息用于描述两种类型的结果。
类型 | 描述 |
---|---|
评估路径 | 这是从根部到达的键,评估可以到达用于评估实例的模式。这包括$ref 和$dynamicRef 。例如。 /properties/bar/$ref/properties/bar-prop |
模式位置 | 这是模式的规范IRI以及用于评估实例的模式的JSON指针片段。例如。 https://json-schema.org/schemas/example#/$defs/bar/properties/bar-prop |
实例位置 | 这是正在评估的实例数据的JSON指针片段。例如。 /bar/bar-prop |
断言包含以下其他信息
类型 | 描述 |
---|---|
信息 | 验证错误消息。 |
代码 | 错误代码。 |
消息密钥 | 用于生成本地化消息的消息键。 |
争论 | 用于生成消息的参数。 |
类型 | 生成消息的关键字。 |
财产 | 例如,导致验证错误的属性名称为required 关键字。请注意,这不是实例位置的一部分,因为这指向实例节点。 |
模式节点 | JsonNode 指向架构位置。这是导致输入数据失败的模式数据。可以通过使用使用LocationJsonNodeFactoryFactory 并使用JsonNodes.tokenLocationOf(schemaNode) JsonNodeReader 配置JsonSchemaFactory 来获取位置信息。 |
实例节点 | JsonNode 指向实例位置。这是验证失败的输入数据。可以通过使用使用LocationJsonNodeFactoryFactory 和使用JsonNodes.tokenLocationOf(instanceNode) 的JsonNodeReader 将JsonSchemaFactory 配置JSonschemaFactory来获取位置信息。 |
错误 | 错误。 |
细节 | 可以通过自定义关键字验证器实现设置的其他详细信息。库不使用。 |
注释包含以下其他信息
类型 | 描述 |
---|---|
价值 | 产生的注释值 |
可以将库配置为在JsonNode
实例中存储行和列信息,以获取实例和架构节点。这将对性能产生不利影响,默认情况下不配置。
这是通过配置JsonNodeReader
来完成的,该jsonnodereader使用JsonSchemaFactory
上的LocationJsonNodeFactoryFactory
JSONNODEFACTORYFACTORY。然后可以使用JsonNodes.tokenLocationOf(jsonNode)
检索JsonLocation
信息。
String schemaData = "{ r n "
+ " " $id " : " https://schema/myschema " , r n "
+ " " properties " : { r n "
+ " " startDate " : { r n "
+ " " format " : " date " , r n "
+ " " minLength " : 6 r n "
+ " } r n "
+ " } r n "
+ "}" ;
String inputData = "{ r n "
+ " " startDate " : " 1 " r n "
+ "}" ;
JsonSchemaFactory factory = JsonSchemaFactory . getInstance ( VersionFlag . V202012 ,
builder -> builder . jsonNodeReader ( JsonNodeReader . builder (). locationAware (). build ()));
SchemaValidatorsConfig config = SchemaValidatorsConfig . builder (). build ();
JsonSchema schema = factory . getSchema ( schemaData , InputFormat . JSON , config );
Set < ValidationMessage > messages = schema . validate ( inputData , InputFormat . JSON , executionContext -> {
executionContext . getExecutionConfig (). setFormatAssertionsEnabled ( true );
});
List < ValidationMessage > list = messages . stream (). collect ( Collectors . toList ());
ValidationMessage format = list . get ( 0 );
JsonLocation formatInstanceNodeTokenLocation = JsonNodes . tokenLocationOf ( format . getInstanceNode ());
JsonLocation formatSchemaNodeTokenLocation = JsonNodes . tokenLocationOf ( format . getSchemaNode ());
ValidationMessage minLength = list . get ( 1 );
JsonLocation minLengthInstanceNodeTokenLocation = JsonNodes . tokenLocationOf ( minLength . getInstanceNode ());
JsonLocation minLengthSchemaNodeTokenLocation = JsonNodes . tokenLocationOf ( minLength . getSchemaNode ());
assertEquals ( "format" , format . getType ());
assertEquals ( "date" , format . getSchemaNode (). asText ());
assertEquals ( 5 , formatSchemaNodeTokenLocation . getLineNr ());
assertEquals ( 17 , formatSchemaNodeTokenLocation . getColumnNr ());
assertEquals ( "1" , format . getInstanceNode (). asText ());
assertEquals ( 2 , formatInstanceNodeTokenLocation . getLineNr ());
assertEquals ( 16 , formatInstanceNodeTokenLocation . getColumnNr ());
assertEquals ( "minLength" , minLength . getType ());
assertEquals ( "6" , minLength . getSchemaNode (). asText ());
assertEquals ( 6 , minLengthSchemaNodeTokenLocation . getLineNr ());
assertEquals ( 20 , minLengthSchemaNodeTokenLocation . getColumnNr ());
assertEquals ( "1" , minLength . getInstanceNode (). asText ());
assertEquals ( 2 , minLengthInstanceNodeTokenLocation . getLineNr ());
assertEquals ( 16 , minLengthInstanceNodeTokenLocation . getColumnNr ());
assertEquals ( 16 , minLengthInstanceNodeTokenLocation . getColumnNr ());
该库实现了在规范中定义的标志,列表和层次输出格式,用于用于JSON Schema验证和注释的机器可读输出。
列表和分层输出格式特别有助于了解系统如何达到特定结果。
输出格式 | 描述 |
---|---|
默认 | 生成断言列表。 |
布尔 | 如果验证成功,则返回为true 。请注意,默认情况下,该输出格式默认打开了失败快速选项。 |
旗帜 | 如果验证成功,则返回具有valid OutputFlag 对象,具有true 。请注意,默认情况下,该输出格式默认打开了失败快速选项。 |
列表 | 返回带有details 的OutputUnit 对象,其中包含主张和注释的OutputUnit 对象列表。请注意,默认情况下未收集注释,必须启用它,因为它将影响性能。 |
分层 | 返回带有OutputUnit 对象层次结构的OutputUnit 对象,以进行主张和注释。请注意,默认情况下未收集注释,必须启用它,因为它将影响性能。 |
下面的示例显示了如何通过注释收集和报告打开并格式断言打开的层次输出格式。
JsonSchemaFactory factory = JsonSchemaFactory . getInstance ( VersionFlag . V202012 );
SchemaValidatorsConfig config = SchemaValidatorsConfig (). builder (). formatAssertionsEnabled ( true ). build ();
JsonSchema schema = factory . getSchema ( SchemaLocation . of ( "https://json-schema.org/schemas/example" ), config );
OutputUnit outputUnit = schema . validate ( inputData , InputFormat . JSON , OutputFormat . HIERARCHICAL , executionContext -> {
executionContext . getExecutionConfig (). setAnnotationCollectionEnabled ( true );
executionContext . getExecutionConfig (). setAnnotationCollectionFilter ( keyword -> true );
});
以下是分层格式的样本输出。
{
"valid" : false ,
"evaluationPath" : " " ,
"schemaLocation" : " https://json-schema.org/schemas/example# " ,
"instanceLocation" : " " ,
"droppedAnnotations" : {
"properties" : [ " foo " , " bar " ],
"title" : " root "
},
"details" : [ {
"valid" : false ,
"evaluationPath" : " /properties/foo/allOf/0 " ,
"schemaLocation" : " https://json-schema.org/schemas/example#/properties/foo/allOf/0 " ,
"instanceLocation" : " /foo " ,
"errors" : {
"required" : " required property 'unspecified-prop' not found "
}
}, {
"valid" : false ,
"evaluationPath" : " /properties/foo/allOf/1 " ,
"schemaLocation" : " https://json-schema.org/schemas/example#/properties/foo/allOf/1 " ,
"instanceLocation" : " /foo " ,
"droppedAnnotations" : {
"properties" : [ " foo-prop " ],
"title" : " foo-title " ,
"additionalProperties" : [ " foo-prop " , " other-prop " ]
},
"details" : [ {
"valid" : false ,
"evaluationPath" : " /properties/foo/allOf/1/properties/foo-prop " ,
"schemaLocation" : " https://json-schema.org/schemas/example#/properties/foo/allOf/1/properties/foo-prop " ,
"instanceLocation" : " /foo/foo-prop " ,
"errors" : {
"const" : " must be a constant value 1 "
},
"droppedAnnotations" : {
"title" : " foo-prop-title "
}
} ]
}, {
"valid" : false ,
"evaluationPath" : " /properties/bar/$ref " ,
"schemaLocation" : " https://json-schema.org/schemas/example#/$defs/bar " ,
"instanceLocation" : " /bar " ,
"droppedAnnotations" : {
"properties" : [ " bar-prop " ],
"title" : " bar-title "
},
"details" : [ {
"valid" : false ,
"evaluationPath" : " /properties/bar/$ref/properties/bar-prop " ,
"schemaLocation" : " https://json-schema.org/schemas/example#/$defs/bar/properties/bar-prop " ,
"instanceLocation" : " /bar/bar-prop " ,
"errors" : {
"minimum" : " must have a minimum value of 10 "
},
"droppedAnnotations" : {
"title" : " bar-prop-title "
}
} ]
} ]
}
姓名 | 描述 | 默认值 |
---|---|---|
annotationCollectionEnabled | 控制是否在处理过程中收集注释。请注意,收集注释会对绩效产生不利影响。 | false |
annotationCollectionFilter | 用于控制哪种关键字用于收集和报告注释的谓词。这需要annotationCollectionEnabled 是true 。 | keyword -> false |
locale | 用于在ValidationMessage 中生成消息的语言环境。请注意,此值是从SchemaValidatorsConfig 复制的,以供每个执行。 | Locale.getDefault() |
failFast | 生成断言时是否立即返回失败。请注意,此值是从SchemaValidatorsConfig 复制的,但每次执行都将自动设置为布尔和标志输出格式的true 。 | false |
formatAssertionsEnabled | 默认值是生成从草案4到草案7的格式主张,而仅产生2019 - 09年草案的注释。设置为true 或false 将覆盖默认行为。 | null |
debugEnabled | 控制是否启用了调试日志记录以在处理时记录节点信息。请注意,这将产生许多会影响性能的日志。 | false |
姓名 | 描述 | 默认值 |
---|---|---|
applyDefaultsStrategy | 遇到失踪或无效节点时行走时应用默认值的策略。 | ApplyDefaultsStrategy.EMPTY_APPLY_DEFAULTS_STRATEGY |
cacheRefs | 从REFS加载的模式是否会被缓存并重复使用以进行后续运行。将此设置为false 会影响性能,但如果使用多个嵌套的涂抹器,例如anyOf , oneOf 和allOf 则可能是防止高速内存使用的必要条件。 | true |
discriminatorKeywordEnabled | 根据OpenAPI 3处理discriminator 关键字。 | false |
errorMessageKeyword | 用于架构中自定义错误消息的关键字。如果未设置此功能,则将禁用。通常将其设置为errorMessage 或message 。 | null |
executionContextCustomizer | 这可用于自定义JsonSchema 生成的每个验证运行生成的ExecutionContext 。 | null |
failFast | 生成断言时是否立即返回失败。 | false |
formatAssertionsEnabled | 默认值是生成从草案4到草案7的格式主张,而仅产生2019 - 09年草案的注释。设置为true 或false 将覆盖默认行为。 | null |
javaSemantics | Java语义是否用于type 关键字。 | false |
locale | 用于在ValidationMessage 中生成消息的语言环境。 | Locale.getDefault() |
losslessNarrowing | 是否将无损狭窄用于type 关键字。 | false |
messageSource | 这用于检索特定的特定消息。 | DefaultMessageSource.getInstance() |
nullableKeywordEnabled | 是否根据OpenAPI 3.0处理nullable 关键字。这会影响enum 和type 关键字。 | false |
pathType | 用于报告实例位置和评估路径的路径类型。设置为PathType.JSON_PATH 使用JSON路径。 | PathType.JSON_POINTER |
preloadJsonSchema | 在处理任何输入之前,该模式是否会预加载。这将使用内存,但是验证的执行将更快。 | true |
preloadJsonSchemaRefMaxNestingDepth | 预加载参考时,评估路径的最大深度。 | 40 |
readOnly | 是否仅读取模式。这会影响readOnly 关键字。 | null |
regularExpressionFactory | 用于创建正则表达式的工厂,例如JoniRegularExpressionFactory 或GraalJSRegularExpressionFactory 。这需要手动添加依赖项,否则将抛出ClassNotFoundException 。 | JDKRegularExpressionFactory.getInstance() |
schemaIdValidator | 这用于自定义$id 值的验证方式。请注意,默认实现允许在未指定基本IRI的情况下进行非空片段,并且还允许根架中的非吸毒IRI $id 值。 | JsonSchemaIdValidator.DEFAULT |
strict | 这是设置关键字在其验证中是否严格的。这所能取决于单个验证者。 | |
typeLoose | 是否以松散的方式解释类型。如果设置为true,则可以将单个值解释为尺寸1数组。字符串也可以解释为数字,整数或布尔值。 | false |
writeOnly | 模式是否仅写。这会影响writeOnly 关键字。 | null |
当库从模式工厂创建模式时,它将为评估路径上的每个位置创建一个独特的验证器实例。这意味着,如果有不同的$ref
,则参考相同的模式位置,则为每个评估路径创建不同的验证器实例。
创建架构时,库默认将自动预加载所有所需的验证器并解析引用。可以使用SchemaValidatorsConfig
中的preloadJsonSchema
选项禁用这一点。在这一点上,如果无法解决参考,将不例外。如果有循环的参考文献,则只会预加载第一个周期。如果您希望确保可以解决远程引用,则需要在JsonSchema
上调用initializeValidators
方法,如果有一些无法解决的引用,该方法将引发异常。
JsonSchemaFactory
的实例和由其创建的JsonSchema
实例被设计为线程安全,只要其配置未经修改,应被缓存和重复使用。不重复使用JsonSchema
意味着需要通过创建的验证器实例重复解析架构数据并解决了引用。解决引用时,创建的验证器将被缓存。对于具有深度嵌套引用的模式,验证器所需的内存可能很高,在这种情况下,可能需要使用SchemaValidatorsConfig
中的cacheRefs
选项来禁用缓存。禁用这将意味着需要重新创建参考文献的验证者,这将影响性能。
收集注释将对验证绩效产生不利影响。
较早的草案规格包含的关键字较少,可能会影响性能。例如,使用unevaluatedProperties
或unevaluatedItems
关键字的使用将触发相关验证器中的注释集合,例如properties
或items
验证器。
这并不意味着将架构与以后的草稿规范使用会自动引起性能影响。例如, properties
验证器将执行检查以确定是否需要收集注释,并检查Meta-Schema是否包含unevaluatedProperties
Properties关键字以及是否存在unevaluatedProperties
关键字是否存在相邻评估路径。
图书馆假设正在加载的模式被信任。该安全模型假定该模式与类Path上的应用程序捆绑在一起的用例。
问题 | 描述 | 减轻 |
---|---|---|
架构加载 | 默认情况下,图书馆将在需要时从类路径和Internet加载模式。 | 可以配置DisallowSchemaLoader ,以不允许架构检索。另外,可以将AllowSchemaLoader 配置为限制允许的检索虹膜。 |
模式缓存 | 加载模式时,默认情况下库的预加载和缓存引用。虽然在预加载模式时会有最大的嵌套深度,但仍然可以构造一个模式,该模式具有扇形,该模式消耗了从服务器中消耗大量内存的风扇。 | 将cacheRefs 选项设置为SchemaValidatorsConfig 中的false。 |
正则表达式 | 该库未验证给定的正则表达式是否可以剥夺拒绝服务(重做)。 | 可以将AllowRegularExpressionFactory 配置为对允许的正则表达式执行验证。 |
验证错误 | 该库默认尝试返回所有验证错误。使用诸如allOf 类的涂药器与大量模式的使用可能会导致大量验证错误占据内存。 | 在SchemaValidatorsConfig 中设置failFast Option在遇到第一个错误时立即返回。也可以使用OutputFormat.BOOLEAN 或OutputFormat.FLAG 。 |
Light-Rest-4J,Light-GraphQL-4J和Light-Hybrid-4J使用此库根据规格来验证请求和响应。如果您使用的是Spring Boot(例如Spring Boot)的其他框架,则可以根据OpenAPI 3.0规范使用OpenApivalIdator,这是通用OpenAPI 3.0验证器。
如果您有一个使用此库的项目,请在下面提交PR以添加您的项目。
感谢以下为该项目做出贡献的人。如果您正在使用此库,请考虑为其中一位贡献者的赞助商。
@stevehu
@prashanth-chaitanya
@fdutton
@valfirst
@balloonwen
@jiachen1120
@ddobrin
@eskabetxe
@ehrmann
@prashanthjos
@subhajitdas298
@fwiesner
@Rhwood
@Jawaff
@nitin1891
有关所有贡献者
如果您是贡献者,请加入GitHub赞助商,然后通过PR将链接切换到您的赞助商仪表板。