dry_schema_rails 0.1.0 → 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: 6a4f36b5c56fee74bae96b4dc58ad417771401e43a3ad09ea82a1af3ede0796b
4
- data.tar.gz: 789431a29531e8bb001f4f5c59cf8edfa7f2a8d303a5ca8db2b5cace60a3dd85
3
+ metadata.gz: 6c3560caeddcea00621aed557566de2f4095e9ad0ff0c7b4581467cad3c0f813
4
+ data.tar.gz: 8ba8692aadec09ba7d50604f31ab94a5ea5e90c66b55edad8ddf6a9469cb1250
5
5
  SHA512:
6
- metadata.gz: 4944f5d17da6df9265ebb55fbc03a9452ed74ef07eb05d95266e88f0247605c6846855aa74db3bb577653ca00d3991afdbe38e542b03404dd6af8ac169aa6acc
7
- data.tar.gz: 0ec54b772f7c76c3940aa36020d7cb89ab351734128f0c34e8c4538425114e838e32676ac43129bccab33195a2e40a4a7036de7ea49b2b55418bbd55ca225993
6
+ metadata.gz: bc5375cf1db629467b34f91c79d00a0e099ab43b021042600e4ba9228f98878145c9dde969111b580bb9cc7ce80c895357347162c00eedac8eabe19514bcfbf7
7
+ data.tar.gz: 8b93626e2e87a47a14527fdf9a78ca162034ce560ee63f49e71b9cd5c00da37309a386f68b385d1d244487c30b8af510591ed801ef3ec4bfe6172343705d964d
data/CHANGELOG.md CHANGED
@@ -1,29 +1,28 @@
1
1
  # Changelog
2
2
 
3
- All notable changes to "dry_schema_rails" will be documented in this file.
3
+ All notable changes to [dry_schema_rails](https://github.com/golifox/dry_schema_rails) will be documented in this file.
4
4
 
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
- ## [Unreleased]
8
+ ## [0.1.3] - 2023-10-12
9
+ ### Add
10
+ - Class comments
11
+ - Class method comments & return values linting
9
12
 
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.
13
+ ## [0.1.2] - 2023-10-12
14
+ ### Fixed
15
+ - Compatibility errors
18
16
 
17
+ ## [0.1.1] - 2023-10-12
19
18
  ### Fixed
20
- - Bug that did Y under circumstance Z.
19
+ - Maintain errors
21
20
 
22
21
  ### Changed
23
- - Refactored module X for better readability and performance.
22
+ - class_method to instance class variables
24
23
 
25
24
  ### Removed
26
- - Deprecated methods in module Y.
25
+ - Gem active-support
27
26
 
28
27
  ## [0.1.0] - 2023-10-10
29
28
 
@@ -35,6 +34,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
35
34
  ### Notes
36
35
  - This version serves as the foundational architecture for future features.
37
36
 
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
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,21 +1,114 @@
1
1
  require 'dry-schema'
2
- require 'active_support/core_ext/class/attribute'
3
2
 
4
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.
5
38
  class Base
6
- class_attribute :_schema_proc, default: -> {}
7
-
8
39
  class << self
40
+ def _schema_proc
41
+ @_schema_proc ||= -> {}
42
+ end
43
+
44
+ def _schema_proc=(proc)
45
+ @_schema_proc = proc
46
+ end
47
+
48
+ # @return [Dry::Schema::Result] the schema matching result
9
49
  def call(*args)
10
50
  params.call(*args)
11
51
  end
12
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
13
77
  def params
14
78
  Dry::Schema.Params(&_schema_proc)
15
79
  end
16
80
 
17
81
  alias :contract :params
18
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
19
112
  def schema(&block)
20
113
  if block_given?
21
114
  self._schema_proc = block
@@ -25,4 +118,4 @@ module DrySchemaRails
25
118
  end
26
119
  end
27
120
  end
28
- end
121
+ end
@@ -1,3 +1,3 @@
1
1
  module DrySchemaRails
2
- VERSION = "0.1.0"
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.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Rybolovlev
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
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
27
  - !ruby/object:Gem::Dependency
42
28
  name: rspec
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -97,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
83
  - !ruby/object:Gem::Version
98
84
  version: '0'
99
85
  requirements: []
100
- rubygems_version: 3.4.19
86
+ rubygems_version: 3.1.4
101
87
  signing_key:
102
88
  specification_version: 4
103
89
  summary: Simple DSL for reusing dry-schema in Rails.