https://github.com/toshiemon18/schemar
Design and generate OpenAPI schema with Ruby models
https://github.com/toshiemon18/schemar
openapi openapi3 webapi
Last synced: 10 months ago
JSON representation
Design and generate OpenAPI schema with Ruby models
- Host: GitHub
- URL: https://github.com/toshiemon18/schemar
- Owner: toshiemon18
- License: mit
- Created: 2025-04-20T17:07:05.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-04-20T17:56:48.000Z (10 months ago)
- Last Synced: 2025-04-20T18:29:25.145Z (10 months ago)
- Topics: openapi, openapi3, webapi
- Language: Ruby
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Schemar
A Ruby library for automatically generating OpenAPI JSON schemas from Ruby classes.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'schemar'
```
And then execute:
```bash
bundle install
```
## Usage
### Basic Usage
1. Create a model class and extend `SchemaDefinable`:
```ruby
class User
extend Schemar::SchemaDefinable
# Define basic type attributes
attribute :id, type: Integer, required: true, description: "User ID"
attribute :name, type: String, required: true, description: "User name"
attribute :is_active, type: Boolean, required: true, description: "Account status"
attribute :birth_date, type: Date, description: "Date of birth"
attribute :created_at, type: DateTime, required: true, description: "Creation timestamp"
# Define array type attributes
attribute :tags, type: Array, item_type: String, description: "User tags"
# Define nested objects
attribute :address, type: Address, description: "User address"
end
```
2. Generate the schema:
```ruby
generator = Schemar::SchemaGenerator.new([User])
generator.write_json("openapi.json", title: "User API", version: "1.0.0")
```
### Supported Types
The following types are supported:
- `String`
- `Integer`
- `Float`
- `BigDecimal`
- `Boolean` (only available within classes that extend SchemaDefinable)
- `Date`
- `Time`
- `DateTime`
- `Array` (specify element type with item_type)
- `Hash`
### Enum Definition
```ruby
class Post
extend Schemar::SchemaDefinable
attribute :status, type: String, required: true, description: "Post status"
enum :status, {
draft: "draft",
published: "published",
archived: "archived"
}
end
```
## Development
After checking out the repo, run:
```bash
bin/setup
```
To run tests:
```bash
bundle exec rspec
```
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the Schemar project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).