dry_schema_rails 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e0ab00d425f197732b212e89786fc907a79777a587553179f9bfb1f73fd0993
4
- data.tar.gz: 9febc5e668f7baef5e14d586aa1defcc911bf5db669a27406127a3a59dba5d0b
3
+ metadata.gz: 6c3560caeddcea00621aed557566de2f4095e9ad0ff0c7b4581467cad3c0f813
4
+ data.tar.gz: 8ba8692aadec09ba7d50604f31ab94a5ea5e90c66b55edad8ddf6a9469cb1250
5
5
  SHA512:
6
- metadata.gz: 55cf34abdd81b67cd17bee24148f6ad759e94534b72da8987f08459446066bd1066f16d99067600e5b0de25bb853dbda6a52a1168d2c4a258873a8fa62b35df1
7
- data.tar.gz: b86a495286b57a8b88ae35a1c355cf36f0fa62e3b431429493e827242feb29240500c933bbb72ce4d6a08677214c86699c0f4b8b690d508efdd2a08db32d1a36
6
+ metadata.gz: bc5375cf1db629467b34f91c79d00a0e099ab43b021042600e4ba9228f98878145c9dde969111b580bb9cc7ce80c895357347162c00eedac8eabe19514bcfbf7
7
+ data.tar.gz: 8b93626e2e87a47a14527fdf9a78ca162034ce560ee63f49e71b9cd5c00da37309a386f68b385d1d244487c30b8af510591ed801ef3ec4bfe6172343705d964d
data/CHANGELOG.md CHANGED
@@ -5,6 +5,15 @@ 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.3] - 2023-10-12
9
+ ### Add
10
+ - Class comments
11
+ - Class method comments & return values linting
12
+
13
+ ## [0.1.2] - 2023-10-12
14
+ ### Fixed
15
+ - Compatibility errors
16
+
8
17
  ## [0.1.1] - 2023-10-12
9
18
  ### Fixed
10
19
  - Maintain errors
@@ -25,5 +34,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
25
34
  ### Notes
26
35
  - This version serves as the foundational architecture for future features.
27
36
 
28
- [0.1.1]: https://github.com/your_username/dry_schema_rails/compare/v0.1.0...v0.1.1
29
- [0.1.0]: https://github.com/your_username/dry_schema_rails/releases/tag/v0.1.0
37
+ [0.1.2]: https://github.com/golifox/dry_schema_rails/compare/v0.1.0...v0.1.2
38
+ [0.1.1]: https://github.com/golifox/dry_schema_rails/compare/v0.1.0...v0.1.1
39
+ [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::ApplicationSchema
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, &User::CreateSchema.schema)
78
+ schema(:create, &UserSchema.schema)
71
79
 
72
80
  # This checks maybe in base controller for reusability
73
81
  if safe_params&.failure?
@@ -1,6 +1,40 @@
1
1
  require 'dry-schema'
2
2
 
3
3
  module DrySchemaRails
4
+ # Base class serves as a foundational class designed to encapsulate
5
+ # common functionalities associated with schema definitions. It provides a
6
+ # blueprint for creating new schema classes efficiently and consistently.
7
+ #
8
+ # The core mechanism of this class hinges on the `class_attribute` called
9
+ # `_schema_proc`, which stores the schema's definition as a proc.
10
+ # This proc gets executed in the context of `Dry::Schema` to define the schema.
11
+ #
12
+ # Example Usage:
13
+ #
14
+ # To create a new schema, simply inherit from the Base class and provide
15
+ # the schema definition inside a block passed to the `schema` class method.
16
+ #
17
+ # Example:
18
+ # BaseSchema = Class.new(DrySchemaRails::Base)
19
+ # UserSchema = Class.new(BaseSchema) do
20
+ # schema do
21
+ # required(:username).filled(:string)
22
+ # required(:email).filled(:string, format?: /@/)
23
+ # required(:age).filled(:integer, gt?: 18)
24
+ # end
25
+ # end
26
+ #
27
+ # user_params = { username: "John", email: "john@example.com", age: 25 }
28
+ # result = UserSchema.params.call(user_params)
29
+ # OR
30
+ # result = UserSchema.call(user_params)
31
+ #
32
+ # puts result.success? # => true
33
+ # puts result.errors # => {}
34
+ #
35
+ # This approach allows for the creation of multiple schema classes in a concise
36
+ # and consistent manner, all inheriting common functionality from the base
37
+ # Base class.
4
38
  class Base
5
39
  class << self
6
40
  def _schema_proc
@@ -11,16 +45,70 @@ module DrySchemaRails
11
45
  @_schema_proc = proc
12
46
  end
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
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
@@ -1,3 +1,3 @@
1
1
  module DrySchemaRails
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry_schema_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Rybolovlev