https://github.com/stefankoppier/openapi-validator
Library for validating OpenAPI specifications
https://github.com/stefankoppier/openapi-validator
testing validation
Last synced: 6 months ago
JSON representation
Library for validating OpenAPI specifications
- Host: GitHub
- URL: https://github.com/stefankoppier/openapi-validator
- Owner: stefankoppier
- License: apache-2.0
- Created: 2023-05-27T05:41:25.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-11-01T06:30:02.000Z (over 2 years ago)
- Last Synced: 2023-11-01T07:26:09.883Z (over 2 years ago)
- Topics: testing, validation
- Language: Kotlin
- Homepage: https://stefankoppier.github.io/openapi-validator/
- Size: 1.28 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://plugins.gradle.org/plugin/io.github.stefankoppier.openapi.validator)
[](https://mvnrepository.com/artifact/io.github.stefankoppier.openapi.validator)
[](https://sonarcloud.io/summary/new_code?id=stefankoppier_openapi-validator)
[](https://sonarcloud.io/summary/new_code?id=stefankoppier_openapi-validator)
# OpenAPI Validator
The OpenAPI Validator allows you to validate your OpenAPI specification by writing a descriptive ruleset in Kotlin!
There is an extensive set of built-in rules and is extensible, as it allows you to add custom rules easily.
Visit [the project documentation](https://stefankoppier.github.io/openapi-validator/) for a complete overview of the
functionality.
## Features
### Rules
All properties of the OpenAPI specification have a corresponding rule. For example, if you want to validate that the
path `/pet/findByStatus` has a get operation with the required query parameter `status` you can write
```kotlin
openAPI {
paths {
required()
path(named = "/pet/findByStatus") {
get {
parameters {
parameter(named = "status") {
required()
`in` { exactly("query") }
}
}
}
}
}
}
```
### Quantifier Rules
There is support for quntification on the relevant types. For example
```kotlin
openAPI {
paths {
all {
startsWith("prefix")
}
}
}
```
states that all paths must start with the string `prefix`.
### Preconditions
There is support for preconditions. The general precondition `given` validates a given rule only if
the predicate is true. For example
```kotlin
openAPI {
info {
given({ it != null && it.length > 20 }) {
description { lowercase() }
}
}
}
```
states that the field `description` of the `info` field must lowercase when it is not null, and it's length is
greater than 20.
There is also the `since` precondition, which states that something should hold only if the given date is in
the past. For example
```kotlin
openAPI {
info {
since(LocalDate.of(2024, 1, 1)) {
title { lowercase() }
}
}
}
```
states that the field `title` of the `info` field must be in lowercase after January 1ˢᵗ of 2024.
## Usage
The OpenAPI Validator allows for two ways to execute the validation: via Gradle or via JUnit.
### Gradle Integration
Apply the plugin
```kotlin
plugins {
id("io.github.stefankoppier.openapi.validator") version "x.y.z"
}
```
and then configure the document to be verified using the given rules using
```kotlin
openAPIValidate {
document.set(uri("petstore.yaml"))
rules.set(
openAPI {
info {
title { exactly("OpenAPI Petstore") }
}
}
)
}
```
### JUnit Integration
Add the package to your dependencies.
Then extend your test class with `@ExtendWith(OpenAPIValidationExtension::class)` to add loading of a specification.
The specification can be loaded using the annotation `@OpenAPITest(relativeUrl = "src/test/resources/petstore.yaml")` to
specify the location of the specification. For example
```kotlin
@ExtendWith(OpenAPIValidationExtension::class)
@OpenAPITest(relativeUrl = "src/test/resources/petstore.yaml")
class OpenAPIValidationExtensionTest {
@Test
fun `my petstore test`() {
assertDocumentIsValidFor {
openAPI("My specification") {
info {
title { exactly("OpenAPI Petstore") }
}
}
}
}
}
```