sorbet-schema 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/.ruby-version +1 -0
  3. data/.standard.yml +6 -0
  4. data/.tool-versions +1 -0
  5. data/CHANGELOG.md +24 -0
  6. data/CODE_OF_CONDUCT.md +84 -0
  7. data/Gemfile +25 -0
  8. data/Gemfile.lock +147 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +43 -0
  11. data/Rakefile +22 -0
  12. data/lib/sorbet-schema/hash_transformer.rb +22 -0
  13. data/lib/sorbet-schema/struct_ext.rb +37 -0
  14. data/lib/sorbet-schema/version.rb +5 -0
  15. data/lib/sorbet-schema.rb +27 -0
  16. data/lib/typed/coercion/coercer.rb +18 -0
  17. data/lib/typed/coercion/coercion_error.rb +7 -0
  18. data/lib/typed/coercion/coercion_not_supported_error.rb +7 -0
  19. data/lib/typed/coercion/float_coercer.rb +21 -0
  20. data/lib/typed/coercion/integer_coercer.rb +21 -0
  21. data/lib/typed/coercion/string_coercer.rb +19 -0
  22. data/lib/typed/coercion/struct_coercer.rb +26 -0
  23. data/lib/typed/coercion.rb +26 -0
  24. data/lib/typed/deserialize_error.rb +6 -0
  25. data/lib/typed/field.rb +28 -0
  26. data/lib/typed/hash_serializer.rb +21 -0
  27. data/lib/typed/json_serializer.rb +28 -0
  28. data/lib/typed/parse_error.rb +12 -0
  29. data/lib/typed/schema.rb +10 -0
  30. data/lib/typed/serializer.rb +61 -0
  31. data/lib/typed/validations/field_type_validator.rb +24 -0
  32. data/lib/typed/validations/field_validator.rb +15 -0
  33. data/lib/typed/validations/multiple_validation_error.rb +16 -0
  34. data/lib/typed/validations/required_field_error.rb +14 -0
  35. data/lib/typed/validations/type_mismatch_error.rb +14 -0
  36. data/lib/typed/validations/validated_value.rb +12 -0
  37. data/lib/typed/validations/validation_error.rb +8 -0
  38. data/lib/typed/validations/validation_results.rb +29 -0
  39. data/lib/typed/validations.rb +8 -0
  40. data/sorbet/config +5 -0
  41. data/sorbet/rbi/annotations/rainbow.rbi +269 -0
  42. data/sorbet/rbi/gems/.gitattributes +1 -0
  43. data/sorbet/rbi/gems/ansi@1.5.0.rbi +687 -0
  44. data/sorbet/rbi/gems/ast@2.4.2.rbi +584 -0
  45. data/sorbet/rbi/gems/builder@3.2.4.rbi +504 -0
  46. data/sorbet/rbi/gems/erubi@1.12.0.rbi +145 -0
  47. data/sorbet/rbi/gems/io-console@0.7.2.rbi +8 -0
  48. data/sorbet/rbi/gems/json@2.7.1.rbi +1553 -0
  49. data/sorbet/rbi/gems/language_server-protocol@3.17.0.3.rbi +14237 -0
  50. data/sorbet/rbi/gems/lint_roller@1.1.0.rbi +239 -0
  51. data/sorbet/rbi/gems/minitest-focus@1.4.0.rbi +91 -0
  52. data/sorbet/rbi/gems/minitest-reporters@1.6.1.rbi +1010 -0
  53. data/sorbet/rbi/gems/minitest@5.22.2.rbi +2233 -0
  54. data/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
  55. data/sorbet/rbi/gems/parallel@1.24.0.rbi +280 -0
  56. data/sorbet/rbi/gems/parser@3.3.0.5.rbi +5472 -0
  57. data/sorbet/rbi/gems/prettier_print@1.2.1.rbi +951 -0
  58. data/sorbet/rbi/gems/prism@0.24.0.rbi +31040 -0
  59. data/sorbet/rbi/gems/psych@5.1.2.rbi +1731 -0
  60. data/sorbet/rbi/gems/racc@1.7.3.rbi +157 -0
  61. data/sorbet/rbi/gems/rainbow@3.1.1.rbi +402 -0
  62. data/sorbet/rbi/gems/rake@13.1.0.rbi +3027 -0
  63. data/sorbet/rbi/gems/rbi@0.1.9.rbi +3006 -0
  64. data/sorbet/rbi/gems/regexp_parser@2.9.0.rbi +3771 -0
  65. data/sorbet/rbi/gems/reline@0.4.3.rbi +8 -0
  66. data/sorbet/rbi/gems/rexml@3.2.6.rbi +4781 -0
  67. data/sorbet/rbi/gems/rubocop-ast@1.31.1.rbi +7014 -0
  68. data/sorbet/rbi/gems/rubocop-performance@1.20.2.rbi +8 -0
  69. data/sorbet/rbi/gems/rubocop-sorbet@0.7.7.rbi +8 -0
  70. data/sorbet/rbi/gems/rubocop@1.61.0.rbi +57499 -0
  71. data/sorbet/rbi/gems/ruby-progressbar@1.13.0.rbi +1317 -0
  72. data/sorbet/rbi/gems/sorbet-result@1.1.0.rbi +519 -0
  73. data/sorbet/rbi/gems/sorbet-struct-comparable@1.3.0.rbi +34 -0
  74. data/sorbet/rbi/gems/spoom@1.2.4.rbi +3777 -0
  75. data/sorbet/rbi/gems/standard-custom@1.0.2.rbi +8 -0
  76. data/sorbet/rbi/gems/standard-performance@1.3.1.rbi +80 -0
  77. data/sorbet/rbi/gems/standard-sorbet@0.0.2.rbi +52 -0
  78. data/sorbet/rbi/gems/standard@1.34.0.rbi +850 -0
  79. data/sorbet/rbi/gems/stringio@3.1.0.rbi +8 -0
  80. data/sorbet/rbi/gems/syntax_tree@6.2.0.rbi +23133 -0
  81. data/sorbet/rbi/gems/tapioca@0.12.0.rbi +3499 -0
  82. data/sorbet/rbi/gems/thor@1.3.1.rbi +4351 -0
  83. data/sorbet/rbi/gems/unicode-display_width@2.5.0.rbi +65 -0
  84. data/sorbet/rbi/gems/yard-sorbet@0.8.1.rbi +428 -0
  85. data/sorbet/rbi/gems/yard@0.9.36.rbi +18220 -0
  86. data/sorbet/rbi/gems/zeitwerk@2.6.13.rbi +1003 -0
  87. data/sorbet/tapioca/config.yml +4 -0
  88. data/sorbet/tapioca/require.rb +12 -0
  89. metadata +191 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ffdfd75d86c1aae5d768f10ff0771233cf477e433a5f8fb8c97a426ac6082f33
4
+ data.tar.gz: e800bb3ba0f34e67eec363581306299d57304ae5c1c3fae23db703888f2fd963
5
+ SHA512:
6
+ metadata.gz: 5974ed3fc6f0e7e9bfec18028a3c78ee66bd51fdd3329e6bf34980028ce7fb2254086f975cc58b6f5e143708f789170aeabc3ee095cdbc26dd2cf269dd471cd9
7
+ data.tar.gz: 196aa60d08c1f12552307427737377eba10d19b7945282bdac35b3916decf93035c3039f9ec628bbc040bd1fe430b189bbc9e90714688256397caff5352744ac
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.0
data/.standard.yml ADDED
@@ -0,0 +1,6 @@
1
+ parallel: true
2
+ ruby_version: 3.0
3
+ ignore:
4
+ - 'vendor/**/*'
5
+ plugins:
6
+ - standard-sorbet
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,24 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## 0.1.0 (2024-03-05)
8
+
9
+ ### Features
10
+
11
+ * Add `Integer` and `Float` coercions ([#26](https://github.com/maxveldink/sorbet-schema/issues/26)) ([537f0fc](https://github.com/maxveldink/sorbet-schema/commit/537f0fc4613e95e1e94ac7524488a19afb4018b7))
12
+ * add basic JSON serializer and supporting classes ([0c149d1](https://github.com/maxveldink/sorbet-schema/commit/0c149d1cb175630227ad2cd49fcbaf92a2ef22d3))
13
+ * Add basic type check without coercion ([#16](https://github.com/maxveldink/sorbet-schema/issues/16)) ([4d987e7](https://github.com/maxveldink/sorbet-schema/commit/4d987e736bea6e4650d6ed6bbf35208c63083322))
14
+ * Add Hash and Json conversion methods to `T::Struct` ([#20](https://github.com/maxveldink/sorbet-schema/issues/20)) ([6df87c2](https://github.com/maxveldink/sorbet-schema/commit/6df87c2bb8aa44363c3a02b0fe719725dbe97cb5))
15
+ * Create schema extension on `T::Struct` ([#18](https://github.com/maxveldink/sorbet-schema/issues/18)) ([1f335b7](https://github.com/maxveldink/sorbet-schema/commit/1f335b7746199034208df8b5718edae73b4158dd))
16
+ * initial repo scaffolding ([479f285](https://github.com/maxveldink/sorbet-schema/commit/479f285c08d952f1e6a9c767488657ba36c603b8))
17
+ * Introduce simple HashSerializer ([#19](https://github.com/maxveldink/sorbet-schema/issues/19)) ([80f20a9](https://github.com/maxveldink/sorbet-schema/commit/80f20a9e0164237ceb9743fa5fe062f5a03aba1f))
18
+
19
+
20
+ ### Miscellaneous Chores
21
+
22
+ * release 0.1.0 ([f365058](https://github.com/maxveldink/sorbet-schema/commit/f365058a769a59acb1fd8c505907980a2896c51c))
23
+
24
+ ## [Unreleased]
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at maxveldink@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in sorbet-schema.gemspec
6
+ gemspec
7
+
8
+ group :development do
9
+ gem "rake"
10
+ gem "standard"
11
+ gem "standard-performance"
12
+ gem "standard-sorbet"
13
+ gem "sorbet"
14
+ gem "tapioca", require: false
15
+ end
16
+
17
+ group :development, :test do
18
+ gem "minitest"
19
+ gem "minitest-focus"
20
+ gem "minitest-reporters"
21
+
22
+ gem "debug"
23
+
24
+ gem "sorbet-struct-comparable"
25
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,147 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sorbet-schema (0.1.0)
5
+ sorbet-result (~> 1.1)
6
+ sorbet-runtime (~> 0.5)
7
+ sorbet-struct-comparable (~> 1.3)
8
+ zeitwerk (~> 2.6)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ ansi (1.5.0)
14
+ ast (2.4.2)
15
+ builder (3.2.4)
16
+ debug (1.9.1)
17
+ irb (~> 1.10)
18
+ reline (>= 0.3.8)
19
+ erubi (1.12.0)
20
+ io-console (0.7.2)
21
+ irb (1.11.2)
22
+ rdoc
23
+ reline (>= 0.4.2)
24
+ json (2.7.1)
25
+ language_server-protocol (3.17.0.3)
26
+ lint_roller (1.1.0)
27
+ minitest (5.22.2)
28
+ minitest-focus (1.4.0)
29
+ minitest (>= 4, < 6)
30
+ minitest-reporters (1.6.1)
31
+ ansi
32
+ builder
33
+ minitest (>= 5.0)
34
+ ruby-progressbar
35
+ netrc (0.11.0)
36
+ parallel (1.24.0)
37
+ parser (3.3.0.5)
38
+ ast (~> 2.4.1)
39
+ racc
40
+ prettier_print (1.2.1)
41
+ prism (0.24.0)
42
+ psych (5.1.2)
43
+ stringio
44
+ racc (1.7.3)
45
+ rainbow (3.1.1)
46
+ rake (13.1.0)
47
+ rbi (0.1.9)
48
+ prism (>= 0.18.0, < 0.25)
49
+ sorbet-runtime (>= 0.5.9204)
50
+ rdoc (6.6.2)
51
+ psych (>= 4.0.0)
52
+ regexp_parser (2.9.0)
53
+ reline (0.4.3)
54
+ io-console (~> 0.5)
55
+ rexml (3.2.6)
56
+ rubocop (1.61.0)
57
+ json (~> 2.3)
58
+ language_server-protocol (>= 3.17.0)
59
+ parallel (~> 1.10)
60
+ parser (>= 3.3.0.2)
61
+ rainbow (>= 2.2.2, < 4.0)
62
+ regexp_parser (>= 1.8, < 3.0)
63
+ rexml (>= 3.2.5, < 4.0)
64
+ rubocop-ast (>= 1.30.0, < 2.0)
65
+ ruby-progressbar (~> 1.7)
66
+ unicode-display_width (>= 2.4.0, < 3.0)
67
+ rubocop-ast (1.31.1)
68
+ parser (>= 3.3.0.4)
69
+ rubocop-performance (1.20.2)
70
+ rubocop (>= 1.48.1, < 2.0)
71
+ rubocop-ast (>= 1.30.0, < 2.0)
72
+ rubocop-sorbet (0.7.7)
73
+ rubocop (>= 0.90.0)
74
+ ruby-progressbar (1.13.0)
75
+ sorbet (0.5.11284)
76
+ sorbet-static (= 0.5.11284)
77
+ sorbet-result (1.1.0)
78
+ sorbet-runtime (~> 0.5)
79
+ sorbet-runtime (0.5.11284)
80
+ sorbet-static (0.5.11284-universal-darwin)
81
+ sorbet-static (0.5.11284-x86_64-linux)
82
+ sorbet-static-and-runtime (0.5.11284)
83
+ sorbet (= 0.5.11284)
84
+ sorbet-runtime (= 0.5.11284)
85
+ sorbet-struct-comparable (1.3.0)
86
+ sorbet-runtime (>= 0.5)
87
+ spoom (1.2.4)
88
+ erubi (>= 1.10.0)
89
+ sorbet-static-and-runtime (>= 0.5.10187)
90
+ syntax_tree (>= 6.1.1)
91
+ thor (>= 0.19.2)
92
+ standard (1.34.0)
93
+ language_server-protocol (~> 3.17.0.2)
94
+ lint_roller (~> 1.0)
95
+ rubocop (~> 1.60)
96
+ standard-custom (~> 1.0.0)
97
+ standard-performance (~> 1.3)
98
+ standard-custom (1.0.2)
99
+ lint_roller (~> 1.0)
100
+ rubocop (~> 1.50)
101
+ standard-performance (1.3.1)
102
+ lint_roller (~> 1.1)
103
+ rubocop-performance (~> 1.20.2)
104
+ standard-sorbet (0.0.2)
105
+ lint_roller (~> 1.1)
106
+ rubocop-sorbet (~> 0.7.0)
107
+ stringio (3.1.0)
108
+ syntax_tree (6.2.0)
109
+ prettier_print (>= 1.2.0)
110
+ tapioca (0.12.0)
111
+ bundler (>= 2.2.25)
112
+ netrc (>= 0.11.0)
113
+ parallel (>= 1.21.0)
114
+ rbi (>= 0.1.4, < 0.2)
115
+ sorbet-static-and-runtime (>= 0.5.10820)
116
+ spoom (~> 1.2.0, >= 1.2.0)
117
+ thor (>= 1.2.0)
118
+ yard-sorbet
119
+ thor (1.3.1)
120
+ unicode-display_width (2.5.0)
121
+ yard (0.9.36)
122
+ yard-sorbet (0.8.1)
123
+ sorbet-runtime (>= 0.5)
124
+ yard (>= 0.9)
125
+ zeitwerk (2.6.13)
126
+
127
+ PLATFORMS
128
+ arm64-darwin-22
129
+ arm64-darwin-23
130
+ x86_64-linux
131
+
132
+ DEPENDENCIES
133
+ debug
134
+ minitest
135
+ minitest-focus
136
+ minitest-reporters
137
+ rake
138
+ sorbet
139
+ sorbet-schema!
140
+ sorbet-struct-comparable
141
+ standard
142
+ standard-performance
143
+ standard-sorbet
144
+ tapioca
145
+
146
+ BUNDLED WITH
147
+ 2.5.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Max VelDink
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Sorbet Schema
2
+
3
+
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add sorbet-schema
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install sorbet-schema
14
+
15
+ ## Usage
16
+
17
+ ### Getting Started
18
+
19
+ ### Chaining
20
+
21
+ ## Why use Options?
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run Rubocop and the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`.
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/maxveldink/sorbet-option. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/maxveldink/sorbet-option/blob/master/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in this project's codebase, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/maxveldink/sorbet-option/blob/master/CODE_OF_CONDUCT.md).
40
+
41
+ ## Sponsorships
42
+
43
+ I love creating in the open. If you find this or any other [maxveld.ink](https://maxveld.ink) content useful, please consider sponsoring me on [GitHub](https://github.com/sponsors/maxveldink).
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create do |t|
7
+ t.test_globs = ["test/**/*_test.rb"]
8
+ end
9
+
10
+ require "standard/rake"
11
+
12
+ desc "Run tapioca compilers"
13
+ task :tapioca do
14
+ sh "bin/tapioca gem"
15
+ end
16
+
17
+ desc "Run Sorbet typechecker"
18
+ task :sorbet do
19
+ sh "bundle exec srb tc"
20
+ end
21
+
22
+ task default: %i[standard:fix_unsafely sorbet test]
@@ -0,0 +1,22 @@
1
+ # typed: strict
2
+
3
+ # We don't want a dependency on ActiveSupport.
4
+ # This is a simplified version of ActiveSupport's Key Hash extension
5
+ # https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/hash/keys.rb
6
+ class HashTransformer
7
+ extend T::Sig
8
+
9
+ sig { params(hash: T::Hash[T.untyped, T.untyped]).returns(T::Hash[Symbol, T.untyped]) }
10
+ def deep_symbolize_keys(hash)
11
+ hash.each_with_object({}) do |(key, value), result|
12
+ result[key.to_sym] = value.is_a?(Hash) ? deep_symbolize_keys(value) : value
13
+ end
14
+ end
15
+
16
+ sig { params(hash: T::Hash[T.untyped, T.untyped]).returns(T::Hash[String, T.untyped]) }
17
+ def deep_stringify_keys(hash)
18
+ hash.each_with_object({}) do |(key, value), result|
19
+ result[key.to_s] = value.is_a?(Hash) ? deep_stringify_keys(value) : value
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ # typed: strict
2
+
3
+ module T
4
+ class Struct
5
+ extend T::Sig
6
+
7
+ sig { returns(Typed::Schema) }
8
+ def self.create_schema
9
+ Typed::Schema.new(
10
+ target: self,
11
+ fields: props.map do |name, properties|
12
+ Typed::Field.new(name: name, type: properties[:type], required: !properties[:fully_optional])
13
+ end
14
+ )
15
+ end
16
+
17
+ sig { params(hash: Typed::HashSerializer::InputHash).returns(Typed::Serializer::DeserializeResult) }
18
+ def self.from_hash(hash)
19
+ Typed::HashSerializer.new(schema: create_schema).deserialize(hash)
20
+ end
21
+
22
+ sig { params(json: String).returns(Typed::Serializer::DeserializeResult) }
23
+ def self.from_json(json)
24
+ Typed::JSONSerializer.new(schema: create_schema).deserialize(json)
25
+ end
26
+
27
+ sig { returns(Typed::HashSerializer::OutputHash) }
28
+ def to_hash
29
+ Typed::HashSerializer.new(schema: self.class.create_schema).serialize(self)
30
+ end
31
+
32
+ sig { returns(String) }
33
+ def to_json
34
+ Typed::JSONSerializer.new(schema: self.class.create_schema).serialize(self)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # typed: strict
2
+
3
+ module SorbetSchema
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,27 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "sorbet-runtime"
5
+ require "sorbet-result"
6
+ require "sorbet-struct-comparable"
7
+
8
+ # We can't use `Loader.for_gem` here as we've unconventionally named the root file.
9
+ require "zeitwerk"
10
+ loader = Zeitwerk::Loader.new
11
+ loader.push_dir(__dir__.to_s)
12
+ loader.ignore(__FILE__)
13
+ loader.ignore("#{__dir__}/sorbet-schema/*.rb")
14
+ loader.inflector.inflect(
15
+ "json_serializer" => "JSONSerializer"
16
+ )
17
+ loader.setup
18
+
19
+ # We don't want to place this in the `Typed` module.
20
+ # `sorbet-schema` is a directory that is not autoloaded
21
+ # but contains extensions, so we need to manually require it.
22
+ require_relative "sorbet-schema/hash_transformer"
23
+
24
+ # Sorbet-aware namespace to super-charge your projects
25
+ module Typed
26
+ Value = T.type_alias { T.untyped }
27
+ end
@@ -0,0 +1,18 @@
1
+ # typed: strict
2
+
3
+ module Typed
4
+ module Coercion
5
+ module Coercer
6
+ extend T::Sig
7
+ extend T::Generic
8
+
9
+ abstract!
10
+
11
+ Target = type_member(:out)
12
+
13
+ sig { abstract.params(field: Field, value: Value).returns(Result[Target, CoercionError]) }
14
+ def coerce(field:, value:)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ # typed: strict
2
+
3
+ module Typed
4
+ module Coercion
5
+ class CoercionError < StandardError; end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # typed: strict
2
+
3
+ module Typed
4
+ module Coercion
5
+ class CoercionNotSupportedError < CoercionError; end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ # typed: strict
2
+
3
+ module Typed
4
+ module Coercion
5
+ class FloatCoercer
6
+ extend T::Sig
7
+ extend T::Generic
8
+
9
+ extend Coercer
10
+
11
+ Target = type_template { {fixed: Float} }
12
+
13
+ sig { override.params(field: Field, value: Value).returns(Result[Target, CoercionError]) }
14
+ def self.coerce(field:, value:)
15
+ Success.new(Float(value))
16
+ rescue ArgumentError, TypeError
17
+ Failure.new(CoercionError.new("'#{value}' cannot be coerced into Float."))
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # typed: strict
2
+
3
+ module Typed
4
+ module Coercion
5
+ class IntegerCoercer
6
+ extend T::Sig
7
+ extend T::Generic
8
+
9
+ extend Coercer
10
+
11
+ Target = type_template { {fixed: Integer} }
12
+
13
+ sig { override.params(field: Field, value: Value).returns(Result[Target, CoercionError]) }
14
+ def self.coerce(field:, value:)
15
+ Success.new(Integer(value))
16
+ rescue ArgumentError, TypeError
17
+ Failure.new(CoercionError.new("'#{value}' cannot be coerced into Integer."))
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ # typed: strict
2
+
3
+ module Typed
4
+ module Coercion
5
+ class StringCoercer
6
+ extend T::Sig
7
+ extend T::Generic
8
+
9
+ extend Coercer
10
+
11
+ Target = type_template { {fixed: String} }
12
+
13
+ sig { override.params(field: Field, value: Value).returns(Result[Target, CoercionError]) }
14
+ def self.coerce(field:, value:)
15
+ Success.new(String(value))
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ # typed: strict
2
+
3
+ module Typed
4
+ module Coercion
5
+ class StructCoercer
6
+ extend T::Sig
7
+ extend T::Generic
8
+
9
+ extend Coercer
10
+
11
+ Target = type_template { {fixed: T::Struct} }
12
+
13
+ sig { override.params(field: Field, value: Value).returns(Result[Target, CoercionError]) }
14
+ def self.coerce(field:, value:)
15
+ type = field.type
16
+
17
+ return Failure.new(CoercionError.new("Field type must inherit from T::Struct for Struct coercion.")) unless type < T::Struct
18
+ return Failure.new(CoercionError.new("Value must be a Hash for Struct coercion.")) unless value.is_a?(Hash)
19
+
20
+ Success.new(type.from_hash!(HashTransformer.new.deep_stringify_keys(value)))
21
+ rescue ArgumentError => e
22
+ Failure.new(CoercionError.new(e.message))
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ # typed: strict
2
+
3
+ module Typed
4
+ module Coercion
5
+ extend T::Sig
6
+
7
+ # TODO: We can definitely improve how we select which coercer to use
8
+ # Related issues:
9
+ # * https://github.com/maxveldink/sorbet-schema/issues/9
10
+ # * https://github.com/maxveldink/sorbet-schema/issues/10
11
+ sig { type_parameters(:U).params(field: Field, value: Value).returns(Result[Value, CoercionError]) }
12
+ def self.coerce(field:, value:)
13
+ if field.type < T::Struct
14
+ StructCoercer.coerce(field: field, value: value)
15
+ elsif field.type == String
16
+ StringCoercer.coerce(field: field, value: value)
17
+ elsif field.type == Integer
18
+ IntegerCoercer.coerce(field: field, value: value)
19
+ elsif field.type == Float
20
+ FloatCoercer.coerce(field: field, value: value)
21
+ else
22
+ Failure.new(CoercionNotSupportedError.new)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,6 @@
1
+ # typed: strict
2
+
3
+ module Typed
4
+ class DeserializeError < StandardError
5
+ end
6
+ end