堆棧溢出| 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將鏈接切換到您的讚助商儀表板。