Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Sep 26, 2023
0 parents commit 5abfcd9
Show file tree
Hide file tree
Showing 103 changed files with 5,381 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
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
13 changes: 13 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
3 changes: 3 additions & 0 deletions .standard.yml
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
22 changes: 22 additions & 0 deletions CHANGELOG.md
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
12 changes: 12 additions & 0 deletions Gemfile
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"
21 changes: 21 additions & 0 deletions LICENSE.txt
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.
125 changes: 125 additions & 0 deletions README.md
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).
10 changes: 10 additions & 0 deletions Rakefile
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]
11 changes: 11 additions & 0 deletions bin/console
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__)
111 changes: 111 additions & 0 deletions bin/example
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__)
8 changes: 8 additions & 0 deletions bin/setup
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
Loading

0 comments on commit 5abfcd9

Please sign in to comment.