dry_schema_rails 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -2
- data/README.md +10 -2
- data/lib/dry_schema_rails/base.rb +93 -5
- data/lib/dry_schema_rails/version.rb +4 -2
- data/lib/dry_schema_rails.rb +5 -2
- metadata +54 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a27da4a4f6b25633a1566eedfe71f563e7e5d3d513d4cecf653ed3f221c6cea
|
4
|
+
data.tar.gz: 9acc96fd285a1ccc34054671eab4cffbb209a4934d1c499d2f125bd6bc068b24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9c4cdb88c0d8c05a1174467f27cef5cc56bf90e4360ed59408ffacd55400c21bfca41af11041861e39c1aa34219ad406efe0c381c612b87f530b11f86119a13
|
7
|
+
data.tar.gz: 40776ad215c203af4260c37763fccf69d35bd201ce1dcfc892bfab1c678988cddd6a91d888850f9abd2fc55d6a8724e14d3bb75120eff505f9caa1bb75e50d9d
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,22 @@ All notable changes to [dry_schema_rails](https://github.com/golifox/dry_schema_
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## [0.1.4] - 2024-02-22
|
9
|
+
### Add
|
10
|
+
- Rubocop
|
11
|
+
- MFA
|
12
|
+
### Update
|
13
|
+
- dry-schema gem version to
|
14
|
+
|
15
|
+
## [0.1.3] - 2023-10-12
|
16
|
+
### Add
|
17
|
+
- Class comments
|
18
|
+
- Class method comments & return values linting
|
19
|
+
|
20
|
+
## [0.1.2] - 2023-10-12
|
21
|
+
### Fixed
|
22
|
+
- Compatibility errors
|
23
|
+
|
8
24
|
## [0.1.1] - 2023-10-12
|
9
25
|
### Fixed
|
10
26
|
- Maintain errors
|
@@ -25,5 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
25
41
|
### Notes
|
26
42
|
- This version serves as the foundational architecture for future features.
|
27
43
|
|
28
|
-
[0.1.
|
29
|
-
[0.1.
|
44
|
+
[0.1.4]: https://github.com/golifox/dry_schema_rails/compare/v0.1.3...v0.1.4
|
45
|
+
[0.1.3]: https://github.com/golifox/dry_schema_rails/compare/v0.1.2...v0.1.3
|
46
|
+
[0.1.2]: https://github.com/golifox/dry_schema_rails/compare/v0.1.1...v0.1.2
|
47
|
+
[0.1.1]: https://github.com/golifox/dry_schema_rails/compare/v0.1.0...v0.1.1
|
48
|
+
[0.1.0]: https://github.com/golifox/dry_schema_rails/releases/tag/v0.1.0
|
data/README.md
CHANGED
@@ -31,7 +31,7 @@ bundle install
|
|
31
31
|
Define your schema classes with necessary validation rules in a block passed to `schema`.
|
32
32
|
|
33
33
|
```ruby
|
34
|
-
class UserSchema < DrySchemaRails::
|
34
|
+
class UserSchema < DrySchemaRails::Base
|
35
35
|
schema do
|
36
36
|
required(:username).filled(:string)
|
37
37
|
required(:email).filled(:string, format?: /@/)
|
@@ -59,6 +59,14 @@ bundle install
|
|
59
59
|
module User
|
60
60
|
class CreateValidator < DrySchemaRails::Base
|
61
61
|
params User::CreateSchema.params
|
62
|
+
|
63
|
+
rule(:username) do
|
64
|
+
key.failure('must be unique') if User.exists?(username: value)
|
65
|
+
end
|
66
|
+
|
67
|
+
rule(:email) do
|
68
|
+
key.failure('must be in internation format') unless value.end_with?('.com')
|
69
|
+
end
|
62
70
|
end
|
63
71
|
end
|
64
72
|
```
|
@@ -67,7 +75,7 @@ end
|
|
67
75
|
```ruby
|
68
76
|
class UsersController < ApiController
|
69
77
|
...
|
70
|
-
schema(:create, &
|
78
|
+
schema(:create, &UserSchema.schema)
|
71
79
|
|
72
80
|
# This checks maybe in base controller for reusability
|
73
81
|
if safe_params&.failure?
|
@@ -1,26 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'dry-schema'
|
2
4
|
|
3
5
|
module DrySchemaRails
|
6
|
+
# Base class serves as a foundational class designed to encapsulate
|
7
|
+
# common functionalities associated with schema definitions. It provides a
|
8
|
+
# blueprint for creating new schema classes efficiently and consistently.
|
9
|
+
#
|
10
|
+
# The core mechanism of this class hinges on the `class_attribute` called
|
11
|
+
# `_schema_proc`, which stores the schema's definition as a proc.
|
12
|
+
# This proc gets executed in the context of `Dry::Schema` to define the schema.
|
13
|
+
#
|
14
|
+
# Example Usage:
|
15
|
+
#
|
16
|
+
# To create a new schema, simply inherit from the Base class and provide
|
17
|
+
# the schema definition inside a block passed to the `schema` class method.
|
18
|
+
#
|
19
|
+
# Example:
|
20
|
+
# BaseSchema = Class.new(DrySchemaRails::Base)
|
21
|
+
# UserSchema = Class.new(BaseSchema) do
|
22
|
+
# schema do
|
23
|
+
# required(:username).filled(:string)
|
24
|
+
# required(:email).filled(:string, format?: /@/)
|
25
|
+
# required(:age).filled(:integer, gt?: 18)
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# user_params = { username: "John", email: "john@example.com", age: 25 }
|
30
|
+
# result = UserSchema.params.call(user_params)
|
31
|
+
# OR
|
32
|
+
# result = UserSchema.call(user_params)
|
33
|
+
#
|
34
|
+
# puts result.success? # => true
|
35
|
+
# puts result.errors # => {}
|
36
|
+
#
|
37
|
+
# This approach allows for the creation of multiple schema classes in a concise
|
38
|
+
# and consistent manner, all inheriting common functionality from the base
|
39
|
+
# Base class.
|
4
40
|
class Base
|
5
41
|
class << self
|
6
42
|
def _schema_proc
|
7
43
|
@_schema_proc ||= -> {}
|
8
44
|
end
|
9
45
|
|
10
|
-
|
11
|
-
@_schema_proc = proc
|
12
|
-
end
|
46
|
+
attr_writer :_schema_proc
|
13
47
|
|
48
|
+
# @return [Dry::Schema::Result] the schema matching result
|
14
49
|
def call(*args)
|
15
50
|
params.call(*args)
|
16
51
|
end
|
17
52
|
|
53
|
+
# Using to create custom validators easy.
|
54
|
+
#
|
55
|
+
# Example:
|
56
|
+
#
|
57
|
+
# UserSchema = Class.new(DrySchemaRails) do
|
58
|
+
# schema do
|
59
|
+
# required(:name).filled(:string)
|
60
|
+
# required(:email).filled(:string)
|
61
|
+
# end
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# class UserValidator < Dry::Validation::Contract
|
65
|
+
# params UserSchema.params
|
66
|
+
# # OR
|
67
|
+
# params UserSchema.contract
|
68
|
+
#
|
69
|
+
# rule(:name) do
|
70
|
+
# key.failure('name is too short') if value.length < 3
|
71
|
+
# end
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
# validation = UserSchema.params
|
75
|
+
#
|
76
|
+
# @return [Dry::Schema::Params] the schema definition
|
18
77
|
def params
|
19
78
|
Dry::Schema.Params(&_schema_proc)
|
20
79
|
end
|
21
80
|
|
22
|
-
alias
|
81
|
+
alias contract params
|
23
82
|
|
83
|
+
# Using in rails controllers.
|
84
|
+
#
|
85
|
+
# Example:
|
86
|
+
#
|
87
|
+
# UserSchema = Class.new(BaseSchema) do
|
88
|
+
# schema do
|
89
|
+
# required(:name).filled(:string)
|
90
|
+
# optional(:email).filled(:string)
|
91
|
+
# end
|
92
|
+
# end
|
93
|
+
#
|
94
|
+
# class ApplicationController < ActionController::Base
|
95
|
+
# before_action do
|
96
|
+
# if safe_params&.failure?
|
97
|
+
# render json: { errors: safe_params.errors.to_h }, status: :bad_request
|
98
|
+
# end
|
99
|
+
# end
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# class UsersController < ApplicationController
|
103
|
+
# schema(:create, &UserSchema.schema)
|
104
|
+
#
|
105
|
+
# def create
|
106
|
+
# @user = User.create(safe_params.output)
|
107
|
+
# render json: @user, status: :created
|
108
|
+
# end
|
109
|
+
# end
|
110
|
+
#
|
111
|
+
# @return [Proc] the schema definition
|
24
112
|
def schema(&block)
|
25
113
|
if block_given?
|
26
114
|
self._schema_proc = block
|
@@ -30,4 +118,4 @@ module DrySchemaRails
|
|
30
118
|
end
|
31
119
|
end
|
32
120
|
end
|
33
|
-
end
|
121
|
+
end
|
data/lib/dry_schema_rails.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry_schema_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Rybolovlev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-schema
|
@@ -16,30 +16,72 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.13'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rspec
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
45
|
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '3.
|
47
|
+
version: '3.0'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
52
|
- - "~>"
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '3.
|
54
|
+
version: '3.0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.60'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.60'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-performance
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.10'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.10'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rspec
|
43
85
|
requirement: !ruby/object:Gem::Requirement
|
44
86
|
requirements:
|
45
87
|
- - "~>"
|
@@ -64,10 +106,11 @@ files:
|
|
64
106
|
- lib/dry_schema_rails.rb
|
65
107
|
- lib/dry_schema_rails/base.rb
|
66
108
|
- lib/dry_schema_rails/version.rb
|
67
|
-
homepage:
|
109
|
+
homepage: https://github.com/golifox/dry_schema_rails
|
68
110
|
licenses:
|
69
111
|
- MIT
|
70
|
-
metadata:
|
112
|
+
metadata:
|
113
|
+
rubygems_mfa_required: 'true'
|
71
114
|
post_install_message:
|
72
115
|
rdoc_options: []
|
73
116
|
require_paths:
|
@@ -76,14 +119,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
119
|
requirements:
|
77
120
|
- - ">="
|
78
121
|
- !ruby/object:Gem::Version
|
79
|
-
version: 2.
|
122
|
+
version: 2.7.4
|
80
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
124
|
requirements:
|
82
125
|
- - ">="
|
83
126
|
- !ruby/object:Gem::Version
|
84
127
|
version: '0'
|
85
128
|
requirements: []
|
86
|
-
rubygems_version: 3.1.
|
129
|
+
rubygems_version: 3.1.6
|
87
130
|
signing_key:
|
88
131
|
specification_version: 4
|
89
132
|
summary: Simple DSL for reusing dry-schema in Rails.
|