game_validator 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +64 -3
- data/game_validator.gemspec +1 -1
- data/lib/game_validator/validator.rb +1 -0
- data/lib/game_validator/validator/full_validator_coercer.rb +38 -0
- data/lib/game_validator/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 919eea6127e5739639bfd7034cf90ac44f4686dcc98fe8bd05d5cec91214458e
|
4
|
+
data.tar.gz: 2169e75bd4797488853984472902d076da7c032eee445ba89ad02a5db98f7fac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9002a19d864067465f51a27cf344087d75f94b22704e2b6bccb07d38d3b9c2d56470bddac6a13a90874b7f8c34feb8e88a2afdf5b318294e0b6a7e6c1ff7438
|
7
|
+
data.tar.gz: 41e821d0ab7d559142a95dce2afd1032a4926a846e0be6d338f65abbc74804310668e922176940244eebbef5d096a994f5ea80e1832a7aa825fbf16b16307ab7
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# GameValidator
|
2
2
|
|
3
|
-
|
3
|
+
This gem is a wrapper on 'dry-validation' to validate the entire input in two or more passes.
|
4
|
+
|
5
|
+
The first pass will verify only that the user is valid and the player action is valid. If and only if the first pass is successful, a second pass will be made to verify the input against a validator specific to the user's admin status, and to the player_action selected.
|
4
6
|
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
@@ -22,7 +23,65 @@ Or install it yourself as:
|
|
22
23
|
|
23
24
|
## Usage
|
24
25
|
|
25
|
-
|
26
|
+
The base class of this gem is GameValidator::Validator. It requires two dependencies. The first, with key <b>validate_player_action_and_user</b>, is a built in class GameValidator::Validator::Base, while the second, <b>full_validator_for</b>, should be created by the application builder.
|
27
|
+
|
28
|
+
validate_player_action_and_user, where the legal player actions are 'run' and 'hide', and the player whose turn it is has an id of 1, is most easily initialized with the below code
|
29
|
+
```
|
30
|
+
GameValidator::Validator::Base::new(legal_options: ['run', 'hide'], next_player_id: 1)
|
31
|
+
```
|
32
|
+
|
33
|
+
To create the various action specific validators, wrap Dry::Validation::Contract in a manner such as this
|
34
|
+
```
|
35
|
+
class RunValidator
|
36
|
+
extend Dry::Initializer
|
37
|
+
option :validate, default: ->{Validator::new}
|
38
|
+
|
39
|
+
class Validator < Dry::Validation::Contract
|
40
|
+
schema do
|
41
|
+
required(:player_action).filled(:string, eql?: 'run')
|
42
|
+
required(:where).filled(:string, included_in?: ['here', 'there'])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Executor
|
47
|
+
extend Dry::Initializer
|
48
|
+
option :where
|
49
|
+
def call(change_orders:, **args)
|
50
|
+
change_orders.push(Node::new(where: where))
|
51
|
+
change_orders
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class Node
|
56
|
+
extend Dry::Initializer
|
57
|
+
option :where
|
58
|
+
def accept(visitor)
|
59
|
+
visitor.handle_run_node(where)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def call(input)
|
64
|
+
result = validate(input)
|
65
|
+
result.success? ? Executor::new(where: result[:where]) : result
|
66
|
+
end
|
67
|
+
end
|
68
|
+
```
|
69
|
+
You then need to arrange all of the possible validators in a hash, with either a String or Hash key the value being the value of player_action, or a two element array as the key, the first element being the string value of player_action, and whether the validator is for an admin or non admin as the second element. If the same validator should apply whether or not the user is an admin or not, the same element should be added twice, with the admin portion being both true and false
|
70
|
+
|
71
|
+
```
|
72
|
+
full_validator = {
|
73
|
+
'run' => run_validator,
|
74
|
+
['hide', true] => hide_admin_validator,
|
75
|
+
['hide', false] => hide_non_admin_validator # using a hash with two elements as the key in the event I need different validators for admin and non-admin
|
76
|
+
}
|
77
|
+
```
|
78
|
+
|
79
|
+
In this fashion, you create the full validator as follows
|
80
|
+
```
|
81
|
+
GameValidator::Validator::new(
|
82
|
+
validate_player_action_and_user: GameValidator::Validator::Base::new(legal_options: ['run', 'hide'], next_player_id: 1),
|
83
|
+
full_validator_for: full_validator)
|
84
|
+
```
|
26
85
|
|
27
86
|
## Development
|
28
87
|
|
@@ -37,3 +96,5 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
|
|
37
96
|
## License
|
38
97
|
|
39
98
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
99
|
+
extend Dry::Initializer
|
100
|
+
option :where
|
data/game_validator.gemspec
CHANGED
@@ -2,7 +2,7 @@ lib = File.expand_path("lib", __dir__)
|
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
3
|
require "game_validator/version"
|
4
4
|
|
5
|
-
Gem::Specification.new 'game_validator', '0.
|
5
|
+
Gem::Specification.new 'game_validator', '0.3.0' do |spec|
|
6
6
|
spec.name = 'game_validator'
|
7
7
|
spec.version = GameValidator::VERSION
|
8
8
|
spec.authors = ['Colin Horner']
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen string_literal: true
|
2
|
+
|
3
|
+
require 'dry-types'
|
4
|
+
|
5
|
+
module GameValidator
|
6
|
+
class Validator
|
7
|
+
class FullValidatorCoercer
|
8
|
+
def call(validator_hash)
|
9
|
+
Types::Hash.call(validator_hash) # throw exception if not given a hash
|
10
|
+
result = {}
|
11
|
+
validator_hash.each do |key, value|
|
12
|
+
if key.is_a?(Array)
|
13
|
+
if key.length != 2
|
14
|
+
raise Dry::Types::ConstraintError::new("Array keys must have two elements given #{key}", key)
|
15
|
+
end
|
16
|
+
if !key.first.is_a?(String) && !key.first.is_a?(Symbol)
|
17
|
+
raise Dry::Types::ConstraintError::new("first part of array key must be a String or Symbol, given #{key}", key)
|
18
|
+
end
|
19
|
+
if !key.last.eql?(true) && !key.last.eql?(false)
|
20
|
+
raise Dry::Types::ConstraintError::new("last part of array key must be a boolean, given #{key}", key)
|
21
|
+
end
|
22
|
+
result[["#{key.first}".freeze, key.last]] = Types.Interface(:call).call(value)
|
23
|
+
elsif key.is_a?(String) || key.is_a?(Symbol)
|
24
|
+
if key.length.eql?(0)
|
25
|
+
raise Dry::Types::ConstraintError::new('Empty String or Symbol key not permitted', key)
|
26
|
+
end
|
27
|
+
result[["#{key}".freeze, false]] = Types.Interface(:call).call(value)
|
28
|
+
result[["#{key}".freeze, true]] = Types.Interface(:call).call(value)
|
29
|
+
else
|
30
|
+
raise Dry::Types::ConstraintError::new('key must be a String, Symbol, or an Array', key)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
result
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: game_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Colin Horner
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-01-
|
11
|
+
date: 2021-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -143,6 +143,7 @@ files:
|
|
143
143
|
- lib/game_validator.rb
|
144
144
|
- lib/game_validator/validator.rb
|
145
145
|
- lib/game_validator/validator/base.rb
|
146
|
+
- lib/game_validator/validator/full_validator_coercer.rb
|
146
147
|
- lib/game_validator/validator/result.rb
|
147
148
|
- lib/game_validator/validator/result/failure.rb
|
148
149
|
- lib/game_validator/validator/result/failure/builder.rb
|