nxt_schema 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +86 -0
- data/LICENSE.txt +21 -0
- data/README.md +376 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/nxt_schema/callable.rb +74 -0
- data/lib/nxt_schema/callable_or_value.rb +72 -0
- data/lib/nxt_schema/dsl.rb +38 -0
- data/lib/nxt_schema/error_messages/en.yaml +15 -0
- data/lib/nxt_schema/error_messages.rb +40 -0
- data/lib/nxt_schema/errors/error.rb +7 -0
- data/lib/nxt_schema/errors/invalid_options_error.rb +5 -0
- data/lib/nxt_schema/errors/schema_not_applied_error.rb +5 -0
- data/lib/nxt_schema/errors.rb +4 -0
- data/lib/nxt_schema/node/base.rb +318 -0
- data/lib/nxt_schema/node/collection.rb +73 -0
- data/lib/nxt_schema/node/constructor.rb +9 -0
- data/lib/nxt_schema/node/default_value_evaluator.rb +20 -0
- data/lib/nxt_schema/node/error.rb +13 -0
- data/lib/nxt_schema/node/has_subnodes.rb +97 -0
- data/lib/nxt_schema/node/leaf.rb +43 -0
- data/lib/nxt_schema/node/maybe_evaluator.rb +23 -0
- data/lib/nxt_schema/node/schema.rb +147 -0
- data/lib/nxt_schema/node/template_store.rb +15 -0
- data/lib/nxt_schema/node/type_resolver.rb +24 -0
- data/lib/nxt_schema/node/validate_with_proxy.rb +41 -0
- data/lib/nxt_schema/node.rb +5 -0
- data/lib/nxt_schema/registry.rb +85 -0
- data/lib/nxt_schema/types.rb +10 -0
- data/lib/nxt_schema/undefined.rb +7 -0
- data/lib/nxt_schema/validators/attribute.rb +34 -0
- data/lib/nxt_schema/validators/equality.rb +33 -0
- data/lib/nxt_schema/validators/excluded.rb +23 -0
- data/lib/nxt_schema/validators/excludes.rb +23 -0
- data/lib/nxt_schema/validators/greater_than.rb +23 -0
- data/lib/nxt_schema/validators/greater_than_or_equal.rb +23 -0
- data/lib/nxt_schema/validators/included.rb +23 -0
- data/lib/nxt_schema/validators/includes.rb +23 -0
- data/lib/nxt_schema/validators/less_than.rb +23 -0
- data/lib/nxt_schema/validators/less_than_or_equal.rb +23 -0
- data/lib/nxt_schema/validators/optional_node.rb +26 -0
- data/lib/nxt_schema/validators/pattern.rb +24 -0
- data/lib/nxt_schema/validators/query.rb +28 -0
- data/lib/nxt_schema/validators/registry.rb +11 -0
- data/lib/nxt_schema/validators/validator.rb +17 -0
- data/lib/nxt_schema/version.rb +3 -0
- data/lib/nxt_schema.rb +69 -0
- data/nxt_schema.gemspec +46 -0
- metadata +211 -0
@@ -0,0 +1,28 @@
|
|
1
|
+
module NxtSchema
|
2
|
+
module Validators
|
3
|
+
class Query < Validator
|
4
|
+
def initialize(method)
|
5
|
+
@method = method
|
6
|
+
end
|
7
|
+
|
8
|
+
register_as :query
|
9
|
+
attr_reader :method
|
10
|
+
|
11
|
+
# Query a boolean method on you value => node(:test, :String).validate(:query, :good_enough?)
|
12
|
+
# => Would be valid if value.good_enough? is truthy
|
13
|
+
|
14
|
+
def build
|
15
|
+
lambda do |node, value|
|
16
|
+
raise ArgumentError, "#{value} does not respond to query: #{method}" unless value.respond_to?(method)
|
17
|
+
|
18
|
+
if value.send(method)
|
19
|
+
true
|
20
|
+
else
|
21
|
+
message = translate_error(node.locale, value: value, actual: value.send(method), query: method)
|
22
|
+
node.add_error(message)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module NxtSchema
|
2
|
+
module Validators
|
3
|
+
class Validator
|
4
|
+
def self.register_as(*keys)
|
5
|
+
keys.each do |key|
|
6
|
+
NxtSchema::Validators::Registry::VALIDATORS.register(key, self)
|
7
|
+
end
|
8
|
+
|
9
|
+
define_method('key') { @key ||= keys.first }
|
10
|
+
end
|
11
|
+
|
12
|
+
def translate_error(locale, **options)
|
13
|
+
NxtSchema::ErrorMessages.resolve(locale, key, **options)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/nxt_schema.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require "nxt_schema/version"
|
2
|
+
require "pry"
|
3
|
+
require "active_support/all"
|
4
|
+
require 'dry-types'
|
5
|
+
require 'nxt_registry'
|
6
|
+
require 'yaml'
|
7
|
+
|
8
|
+
require "nxt_schema/types"
|
9
|
+
require "nxt_schema/undefined"
|
10
|
+
require "nxt_schema/registry"
|
11
|
+
require "nxt_schema/callable"
|
12
|
+
require "nxt_schema/callable_or_value"
|
13
|
+
require "nxt_schema/validators/registry"
|
14
|
+
require "nxt_schema/node/validate_with_proxy"
|
15
|
+
|
16
|
+
require "nxt_schema/errors"
|
17
|
+
require "nxt_schema/errors/error"
|
18
|
+
require "nxt_schema/errors/schema_not_applied_error"
|
19
|
+
require "nxt_schema/errors/invalid_options_error"
|
20
|
+
|
21
|
+
require "nxt_schema/error_messages"
|
22
|
+
require "nxt_schema/validators/validator"
|
23
|
+
require "nxt_schema/validators/attribute"
|
24
|
+
require "nxt_schema/validators/equality"
|
25
|
+
require "nxt_schema/validators/optional_node"
|
26
|
+
require "nxt_schema/validators/greater_than"
|
27
|
+
require "nxt_schema/validators/greater_than_or_equal"
|
28
|
+
require "nxt_schema/validators/less_than"
|
29
|
+
require "nxt_schema/validators/less_than_or_equal"
|
30
|
+
require "nxt_schema/validators/pattern"
|
31
|
+
require "nxt_schema/validators/included"
|
32
|
+
require "nxt_schema/validators/includes"
|
33
|
+
require "nxt_schema/validators/excluded"
|
34
|
+
require "nxt_schema/validators/excludes"
|
35
|
+
require "nxt_schema/validators/query"
|
36
|
+
|
37
|
+
require "nxt_schema/node"
|
38
|
+
require "nxt_schema/node/type_resolver"
|
39
|
+
require "nxt_schema/node/maybe_evaluator"
|
40
|
+
require "nxt_schema/node/default_value_evaluator"
|
41
|
+
require "nxt_schema/node/base"
|
42
|
+
require "nxt_schema/node/error"
|
43
|
+
require "nxt_schema/node/has_subnodes"
|
44
|
+
require "nxt_schema/node/template_store"
|
45
|
+
require "nxt_schema/node/schema"
|
46
|
+
require "nxt_schema/node/collection"
|
47
|
+
require "nxt_schema/node/leaf"
|
48
|
+
require "nxt_schema/dsl"
|
49
|
+
|
50
|
+
module NxtSchema
|
51
|
+
def register_validator(validator, *keys)
|
52
|
+
keys.each do |key|
|
53
|
+
NxtSchema::Validators::Registry::VALIDATORS.register(key, validator)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def register_type(key, type)
|
58
|
+
NxtSchema::Types.const_set(key.to_s, type)
|
59
|
+
end
|
60
|
+
|
61
|
+
def register_error_messages(*paths)
|
62
|
+
ErrorMessages.load(paths)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Load default messages
|
66
|
+
ErrorMessages.load
|
67
|
+
|
68
|
+
module_function :register_validator, :register_type, :register_error_messages
|
69
|
+
end
|
data/nxt_schema.gemspec
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "nxt_schema/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nxt_schema"
|
8
|
+
spec.version = NxtSchema::VERSION
|
9
|
+
spec.authors = ["Andreas Robecke"]
|
10
|
+
spec.email = ["a.robecke@getsafe.de"]
|
11
|
+
|
12
|
+
spec.summary = %q{Write a short summary, because RubyGems requires one.}
|
13
|
+
spec.description = %q{Write a longer description or delete this line.}
|
14
|
+
spec.homepage = "https://www.getsafe.de"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
spec.metadata["source_code_uri"] = "https://www.getsafe.de"
|
24
|
+
else
|
25
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
26
|
+
"public gem pushes."
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specify which files should be added to the gem when it is released.
|
30
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
31
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
32
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
|
+
end
|
34
|
+
spec.bindir = "exe"
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
+
spec.require_paths = ["lib"]
|
37
|
+
|
38
|
+
spec.add_dependency "activesupport"
|
39
|
+
spec.add_dependency "dry-types"
|
40
|
+
spec.add_dependency "nxt_registry"
|
41
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
42
|
+
spec.add_development_dependency "rake", "~> 12.3.3"
|
43
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
44
|
+
spec.add_development_dependency "pry"
|
45
|
+
spec.add_development_dependency "method_profiler"
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,211 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nxt_schema
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andreas Robecke
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-types
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: nxt_registry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.17'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.17'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 12.3.3
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 12.3.3
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: method_profiler
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: Write a longer description or delete this line.
|
126
|
+
email:
|
127
|
+
- a.robecke@getsafe.de
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- Gemfile
|
136
|
+
- Gemfile.lock
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- lib/nxt_schema.rb
|
143
|
+
- lib/nxt_schema/callable.rb
|
144
|
+
- lib/nxt_schema/callable_or_value.rb
|
145
|
+
- lib/nxt_schema/dsl.rb
|
146
|
+
- lib/nxt_schema/error_messages.rb
|
147
|
+
- lib/nxt_schema/error_messages/en.yaml
|
148
|
+
- lib/nxt_schema/errors.rb
|
149
|
+
- lib/nxt_schema/errors/error.rb
|
150
|
+
- lib/nxt_schema/errors/invalid_options_error.rb
|
151
|
+
- lib/nxt_schema/errors/schema_not_applied_error.rb
|
152
|
+
- lib/nxt_schema/node.rb
|
153
|
+
- lib/nxt_schema/node/base.rb
|
154
|
+
- lib/nxt_schema/node/collection.rb
|
155
|
+
- lib/nxt_schema/node/constructor.rb
|
156
|
+
- lib/nxt_schema/node/default_value_evaluator.rb
|
157
|
+
- lib/nxt_schema/node/error.rb
|
158
|
+
- lib/nxt_schema/node/has_subnodes.rb
|
159
|
+
- lib/nxt_schema/node/leaf.rb
|
160
|
+
- lib/nxt_schema/node/maybe_evaluator.rb
|
161
|
+
- lib/nxt_schema/node/schema.rb
|
162
|
+
- lib/nxt_schema/node/template_store.rb
|
163
|
+
- lib/nxt_schema/node/type_resolver.rb
|
164
|
+
- lib/nxt_schema/node/validate_with_proxy.rb
|
165
|
+
- lib/nxt_schema/registry.rb
|
166
|
+
- lib/nxt_schema/types.rb
|
167
|
+
- lib/nxt_schema/undefined.rb
|
168
|
+
- lib/nxt_schema/validators/attribute.rb
|
169
|
+
- lib/nxt_schema/validators/equality.rb
|
170
|
+
- lib/nxt_schema/validators/excluded.rb
|
171
|
+
- lib/nxt_schema/validators/excludes.rb
|
172
|
+
- lib/nxt_schema/validators/greater_than.rb
|
173
|
+
- lib/nxt_schema/validators/greater_than_or_equal.rb
|
174
|
+
- lib/nxt_schema/validators/included.rb
|
175
|
+
- lib/nxt_schema/validators/includes.rb
|
176
|
+
- lib/nxt_schema/validators/less_than.rb
|
177
|
+
- lib/nxt_schema/validators/less_than_or_equal.rb
|
178
|
+
- lib/nxt_schema/validators/optional_node.rb
|
179
|
+
- lib/nxt_schema/validators/pattern.rb
|
180
|
+
- lib/nxt_schema/validators/query.rb
|
181
|
+
- lib/nxt_schema/validators/registry.rb
|
182
|
+
- lib/nxt_schema/validators/validator.rb
|
183
|
+
- lib/nxt_schema/version.rb
|
184
|
+
- nxt_schema.gemspec
|
185
|
+
homepage: https://www.getsafe.de
|
186
|
+
licenses:
|
187
|
+
- MIT
|
188
|
+
metadata:
|
189
|
+
allowed_push_host: https://rubygems.org
|
190
|
+
homepage_uri: https://www.getsafe.de
|
191
|
+
source_code_uri: https://www.getsafe.de
|
192
|
+
post_install_message:
|
193
|
+
rdoc_options: []
|
194
|
+
require_paths:
|
195
|
+
- lib
|
196
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
197
|
+
requirements:
|
198
|
+
- - ">="
|
199
|
+
- !ruby/object:Gem::Version
|
200
|
+
version: '0'
|
201
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
202
|
+
requirements:
|
203
|
+
- - ">="
|
204
|
+
- !ruby/object:Gem::Version
|
205
|
+
version: '0'
|
206
|
+
requirements: []
|
207
|
+
rubygems_version: 3.0.3
|
208
|
+
signing_key:
|
209
|
+
specification_version: 4
|
210
|
+
summary: Write a short summary, because RubyGems requires one.
|
211
|
+
test_files: []
|