-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5abfcd9
Showing
103 changed files
with
5,381 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Ruby | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
pull_request: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
name: Ruby ${{ matrix.ruby }} | ||
strategy: | ||
matrix: | ||
ruby: | ||
- '3.1.2' | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: ${{ matrix.ruby }} | ||
bundler-cache: true | ||
- name: Run the default task | ||
run: bundle exec rake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
|
||
# rspec failure tracking | ||
.rspec_status | ||
|
||
Gemfile.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--format documentation | ||
--color | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# For available configuration options, see: | ||
# https://github.com/testdouble/standard | ||
ruby_version: 2.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog], | ||
and this project adheres to [Semantic Versioning]. | ||
|
||
## [Unreleased] | ||
|
||
## [0.1.0] - 2023-09-27 | ||
|
||
### Added | ||
|
||
- Initial implementation. ([@skryukov]) | ||
|
||
[@skryukov]: https://github.com/skryukov | ||
|
||
[Unreleased]: https://github.com/skryukov/skooma/compare/v0.1.0...HEAD | ||
[0.1.0]: https://github.com/skryukov/skooma/commits/v0.1.0 | ||
|
||
[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/ | ||
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
# Specify your gem's dependencies in skooma.gemspec | ||
gemspec | ||
|
||
gem "rake", "~> 13.0" | ||
|
||
gem "rspec", "~> 3.0" | ||
|
||
gem "standard", "~> 1.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2023 Svyatoslav Kryukov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# Skooma – Sugar for your APIs | ||
|
||
[![Gem Version](https://badge.fury.io/rb/skooma.svg)](https://rubygems.org/gems/skooma) | ||
[![Ruby](https://github.com/skryukov/skooma/actions/workflows/main.yml/badge.svg)](https://github.com/skryukov/skooma/actions/workflows/main.yml) | ||
|
||
Skooma is a Ruby library for validating API implementations against OpenAPI documents. | ||
|
||
Features: | ||
- Supports OpenAPI 3.1.0 | ||
- Supports OpenAPI document validation | ||
- Supports request/response validations against OpenAPI document | ||
|
||
<a href="https://evilmartians.com/?utm_source=skooma&utm_campaign=project_page"> | ||
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"> | ||
</a> | ||
|
||
## Installation | ||
|
||
Install the gem and add to the application's Gemfile by executing: | ||
|
||
$ bundle add skooma | ||
|
||
If bundler is not being used to manage dependencies, install the gem by executing: | ||
|
||
$ gem install skooma | ||
|
||
## Usage | ||
|
||
### Configuration | ||
|
||
```ruby | ||
# spec/rails_helper.rb | ||
|
||
RSpec.configure do |config| | ||
# ... | ||
Skooma.create_registry | ||
path_to_openapi = Rails.root.join("docs", "openapi.yml") | ||
config.include Skooma::RSpec[path_to_openapi], type: :request | ||
end | ||
``` | ||
|
||
### Validate OpenAPI document | ||
|
||
```ruby | ||
# spec/openapi_spec.rb | ||
|
||
require "rails_helper" | ||
|
||
describe "OpenAPI document", type: :request do | ||
subject(:schema) { skooma_openapi_schema } | ||
|
||
it { is_expected.to be_valid_document } | ||
end | ||
``` | ||
|
||
### Validate request | ||
|
||
```ruby | ||
# spec/requests/feed_spec.rb | ||
|
||
require "rails_helper" | ||
|
||
describe "/animals/:animal_id/feed" do | ||
let(:animal) { create(:animal, :unicorn) } | ||
|
||
describe "POST" do | ||
subject { post "/animals/#{animal.id}/feed", body:, as: :json } | ||
|
||
let(:body) { {food: "apple", quantity: 3} } | ||
|
||
it { is_expected.to conform_schema(200) } | ||
|
||
context "with wrong food type" do | ||
let(:body) { {food: "wood", quantity: 1} } | ||
|
||
it { is_expected.to conform_schema(422) } | ||
end | ||
end | ||
end | ||
|
||
# Validation Result: | ||
# | ||
# {"valid"=>false, | ||
# "instanceLocation"=>"", | ||
# "keywordLocation"=>"", | ||
# "absoluteKeywordLocation"=>"urn:uuid:1b4b39eb-9b93-4cc1-b6ac-32a25d9bff50#", | ||
# "errors"=> | ||
# [{"instanceLocation"=>"", | ||
# "keywordLocation"=> | ||
# "/paths/~1animals~1{animalId}~1feed/post/responses/200"/ | ||
# "/content/application~1json/schema/required", | ||
# "error"=> | ||
# "The object is missing required properties"/ | ||
# " [\"animalId\", \"food\", \"amount\"]"}]} | ||
``` | ||
|
||
## Alternatives | ||
|
||
- [openapi_first](https://github.com/ahx/openapi_first) | ||
- [committee](https://github.com/interagent/committee) | ||
|
||
## Feature plans | ||
|
||
- Full support for external `$ref`s | ||
- Full OpenAPI 3.1.0 support: | ||
- `discriminator` keyword | ||
- respect `style` and `explode` keywords | ||
- xml | ||
- Callbacks and webhooks validations | ||
- Example validations | ||
- Ability to plug in custom X-*** keyword classes | ||
|
||
## Development | ||
|
||
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. | ||
|
||
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). | ||
|
||
## Contributing | ||
|
||
Bug reports and pull requests are welcome on GitHub at https://github.com/skryukov/skooma. | ||
|
||
## License | ||
|
||
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
require "standard/rake" | ||
|
||
task default: %i[spec standard] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "skooma" | ||
|
||
# You can add fixtures and/or initialization code here to make experimenting | ||
# with your gem easier. You can also use a different console, if you like. | ||
|
||
require "irb" | ||
IRB.start(__FILE__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "skooma" | ||
|
||
# Example: | ||
|
||
Skooma.create_registry | ||
|
||
OPENAPI_SCHEMA = { | ||
"openapi": "3.1.0", | ||
"info": { "title": "api", "version": "1.0.0" }, | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"responses": { | ||
"200": { | ||
"description": "OK", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "object", | ||
"properties": { | ||
"foo": { "type": "string" }, | ||
"bar": { "type": "integer" } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"/foo": { | ||
"get": { | ||
"responses": { | ||
"200": { | ||
"description": "OK", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/Foo" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"schemas": { | ||
"Foo": { | ||
"type": "object", | ||
"properties": { | ||
"foo": { "type": "string" }, | ||
"bar": { "type": "integer" } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
REQUEST_SCHEMA = { | ||
method: "get", | ||
path: "/", | ||
query: "?foo=bar", | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
response: { | ||
status: 200, | ||
headers: { | ||
"Content-Type": 'application/json' | ||
}, | ||
body: { | ||
foo: 'foo', | ||
bar: 1 | ||
} | ||
} | ||
} | ||
|
||
REQUEST_SCHEMA2 = { | ||
method: "get", | ||
path: "/foo", | ||
query: "?foo=bar", | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
response: { | ||
status: 200, | ||
headers: { | ||
"Content-Type": 'application/json' | ||
}, | ||
body: { | ||
foo: 'foo', | ||
bar: 1 | ||
} | ||
} | ||
} | ||
|
||
OPENAPI = Skooma::OpenAPISchema.new(OPENAPI_SCHEMA) | ||
|
||
# OPENAPI.validate.output(:detailed) | ||
# result = OPENAPI.evaluate(REQUEST_SCHEMA) | ||
# result = OPENAPI.evaluate(REQUEST_SCHEMA2) | ||
# result.output(:detailed) | ||
|
||
require "irb" | ||
IRB.start(__FILE__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle install | ||
|
||
# Do any other automated setup that you need to do here |
Oops, something went wrong.