dry_schema_rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6a4f36b5c56fee74bae96b4dc58ad417771401e43a3ad09ea82a1af3ede0796b
4
+ data.tar.gz: 789431a29531e8bb001f4f5c59cf8edfa7f2a8d303a5ca8db2b5cace60a3dd85
5
+ SHA512:
6
+ metadata.gz: 4944f5d17da6df9265ebb55fbc03a9452ed74ef07eb05d95266e88f0247605c6846855aa74db3bb577653ca00d3991afdbe38e542b03404dd6af8ac169aa6acc
7
+ data.tar.gz: 0ec54b772f7c76c3940aa36020d7cb89ab351734128f0c34e8c4538425114e838e32676ac43129bccab33195a2e40a4a7036de7ea49b2b55418bbd55ca225993
data/CHANGELOG.md ADDED
@@ -0,0 +1,40 @@
1
+ # Changelog
2
+
3
+ All notable changes to "dry_schema_rails" will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Added
11
+ - Initial development and features yet to be released.
12
+
13
+ ## [0.2.0] - 2023-10-20
14
+
15
+ ### Added
16
+ - Feature X that does Y.
17
+ - Improved documentation for feature Z.
18
+
19
+ ### Fixed
20
+ - Bug that did Y under circumstance Z.
21
+
22
+ ### Changed
23
+ - Refactored module X for better readability and performance.
24
+
25
+ ### Removed
26
+ - Deprecated methods in module Y.
27
+
28
+ ## [0.1.0] - 2023-10-10
29
+
30
+ ### Added
31
+ - Initial release of the gem.
32
+ - Basic schema functionality.
33
+ - Basic documentation on usage and installation.
34
+
35
+ ### Notes
36
+ - This version serves as the foundational architecture for future features.
37
+
38
+ [Unreleased]: https://github.com/your_username/dry_schema_rails/compare/v0.2.0...HEAD
39
+ [0.2.0]: https://github.com/your_username/dry_schema_rails/compare/v0.1.0...v0.2.0
40
+ [0.1.0]: https://github.com/your_username/dry_schema_rails/releases/tag/v0.1.0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 David Rybolovlev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,164 @@
1
+ # dry_schema_rails [![Gem Version](https://badge.fury.io/rb/dry_schema_rails.svg)](https://badge.fury.io/rb/dry_schema_rails)
2
+
3
+ Simplify Your Schema Management in Rails.
4
+
5
+ ## Description
6
+
7
+ DrySchemaRails is a lightweight gem that leverages `dry-schema` for defining schemas in a DRY and consistent manner across your Rails application. It encapsulates common schema definition functionalities, promoting a clean and easy-to-follow schema definition practice.
8
+
9
+ ## Key Features
10
+ - **Concise Schema Definitions**: Write compact and clear schema definitions using block syntax.
11
+ - **Reusable Schemas**: Create and reuse schema definitions across different parts of your application.
12
+ - **Extendable**: Easily extend and customize your schema definitions as per your requirements.
13
+
14
+ ## Installation
15
+
16
+ Add DrySchemaRails to your Gemfile and bundle.
17
+
18
+ ```ruby
19
+ gem 'dry_schema_rails'
20
+ ```
21
+
22
+ Run
23
+ ```bash
24
+ bundle install
25
+ ```
26
+
27
+ ## Quick Usage Guide
28
+
29
+ 1. **Define Schema:**
30
+
31
+ Define your schema classes with necessary validation rules in a block passed to `schema`.
32
+
33
+ ```ruby
34
+ class UserSchema < DrySchemaRails::ApplicationSchema
35
+ schema do
36
+ required(:username).filled(:string)
37
+ required(:email).filled(:string, format?: /@/)
38
+ required(:age).filled(:integer, gt?: 18)
39
+ end
40
+ end
41
+ ```
42
+
43
+ 2. **Use Schema:**
44
+
45
+ Validate and sanitize parameters using defined schemas.
46
+
47
+ ```ruby
48
+ user_params = { username: "John", email: "john@example.com", age: 25 }
49
+ result = UserSchema.call(user_params)
50
+
51
+ puts result.success? # => true
52
+ puts result.errors # => {}
53
+ ```
54
+
55
+ ## In-Depth Usage
56
+
57
+ ### Custom Validators
58
+ ```ruby
59
+ module User
60
+ class CreateValidator < DrySchemaRails::Base
61
+ params User::CreateSchema.params
62
+ end
63
+ end
64
+ ```
65
+
66
+ ### In Controllers
67
+ ```ruby
68
+ class UsersController < ApiController
69
+ ...
70
+ schema(:create, &User::CreateSchema.schema)
71
+
72
+ # This checks maybe in base controller for reusability
73
+ if safe_params&.failure?
74
+ render json: { errors: safe_params.errors }, status: :unprocessable_entity
75
+ end
76
+
77
+ def create
78
+ @user = User.create!(safe_params.output)
79
+ render json: @user, status: :created
80
+ end
81
+ ...
82
+ end
83
+ ```
84
+
85
+ ## Example Application: DrySchemaRailsDemo
86
+
87
+ ### Idea
88
+
89
+ Create a Rails application `DrySchemaRailsDemo` demonstrating the usage of `DrySchemaRails` in various common Rails use-cases: model validation, parameter validation in controllers, form object validation, etc.
90
+
91
+ ### Key Features:
92
+
93
+ - **User Management:** Simple CRUD for managing users with validations using defined schemas.
94
+ - **API Endpoint:** Demonstrate parameter validation for API endpoints.
95
+ - **Form Object:** Use schemas to validate form objects.
96
+
97
+ ### Implementation
98
+
99
+ 1. **Model and Schema:**
100
+
101
+ Define `User` model and `UserSchema` for validating user attributes.
102
+
103
+ ```ruby
104
+ class User < ApplicationRecord
105
+ # your model code
106
+ end
107
+
108
+ class UserSchema < DrySchemaRails::Base
109
+ schema do
110
+ required(:username).filled(:string)
111
+ # additional validations...
112
+ end
113
+ end
114
+ ```
115
+
116
+ 2. **Controller Validation:**
117
+
118
+ Implement validations in controllers and API endpoints using schema.
119
+
120
+ ```ruby
121
+ class UsersController < ApplicationController
122
+ def create
123
+ validation = UserSchema.call(params.permit!.to_h)
124
+ if validation.success?
125
+ # handle success...
126
+ else
127
+ # handle failure...
128
+ end
129
+ end
130
+ end
131
+ ```
132
+
133
+ 3. **Form Object:**
134
+
135
+ Define form objects validating data using schema before processing.
136
+
137
+ ```ruby
138
+ class RegistrationForm
139
+ def initialize(params)
140
+ @params = params
141
+ end
142
+
143
+ def save
144
+ validation = UserSchema.call(@params)
145
+ if validation.success?
146
+ User.create(validation.output)
147
+ else
148
+ # handle errors...
149
+ end
150
+ end
151
+ end
152
+ ```
153
+
154
+ ## Tests
155
+
156
+ Write tests for models, controllers, and form objects ensuring schema validations are applied and working as expected.
157
+
158
+ ## License
159
+
160
+ See `LICENSE` file.
161
+
162
+ ---
163
+
164
+ For more complex understanding you might adjust specifics to cater to the unique needs and standards of your app or team. For further details on `dry-schema`, visit [Dry Schema](https://dry-rb.org/gems/dry-schema).
@@ -0,0 +1,28 @@
1
+ require 'dry-schema'
2
+ require 'active_support/core_ext/class/attribute'
3
+
4
+ module DrySchemaRails
5
+ class Base
6
+ class_attribute :_schema_proc, default: -> {}
7
+
8
+ class << self
9
+ def call(*args)
10
+ params.call(*args)
11
+ end
12
+
13
+ def params
14
+ Dry::Schema.Params(&_schema_proc)
15
+ end
16
+
17
+ alias :contract :params
18
+
19
+ def schema(&block)
20
+ if block_given?
21
+ self._schema_proc = block
22
+ else
23
+ _schema_proc
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module DrySchemaRails
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ module DrySchemaRails
2
+ end
3
+
4
+ require "dry_schema_rails/version"
5
+ require "dry_schema_rails/base"
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry_schema_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Rybolovlev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-schema
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ description: Simple DSL for reusing dry-schema in Rails.
70
+ email: i@golifox.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - CHANGELOG.md
76
+ - LICENSE
77
+ - README.md
78
+ - lib/dry_schema_rails.rb
79
+ - lib/dry_schema_rails/base.rb
80
+ - lib/dry_schema_rails/version.rb
81
+ homepage: http://github.com/golifox/dry_schema_rails
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.5.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.4.19
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Simple DSL for reusing dry-schema in Rails.
104
+ test_files: []