featurevisor-schemas 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +1139 -0
- data/lib/featurevisor/schemas/derived.rb +292 -0
- data/lib/featurevisor/schemas/engine.rb +623 -0
- data/lib/featurevisor/schemas/handle.rb +425 -0
- data/lib/featurevisor/schemas/store.rb +299 -0
- data/lib/featurevisor/schemas/support.rb +446 -0
- data/lib/featurevisor/schemas/values.rb +137 -0
- data/lib/featurevisor/schemas.rb +242 -0
- data/lib/featurevisor-schemas.rb +3 -0
- metadata +67 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "featurevisor"
|
|
4
|
+
|
|
5
|
+
module Featurevisor
|
|
6
|
+
# Validates data against the schemas a Featurevisor datafile carries, with the
|
|
7
|
+
# same rules, messages and conditions as @featurevisor/schemas in JavaScript.
|
|
8
|
+
#
|
|
9
|
+
# schemas = Featurevisor::Schemas.create(f)
|
|
10
|
+
# result = schemas.validate("signUp", params)
|
|
11
|
+
# render json: result.flatten, status: :unprocessable_entity unless result.valid?
|
|
12
|
+
module Schemas
|
|
13
|
+
# One problem with a value, with what an application needs to word it.
|
|
14
|
+
Issue = Struct.new(:message, :path, :code, :params, :schema, :mutation) do
|
|
15
|
+
# The path as a field name with dots, such as "items.0.name".
|
|
16
|
+
def field
|
|
17
|
+
Paths.format_path(path)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def with(message: self.message, mutation: self.mutation)
|
|
21
|
+
Issue.new(message, path, code, params, schema, mutation)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def to_s
|
|
25
|
+
"#{path.empty? ? "" : "#{field}: "}#{message} (#{code})"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Issues' messages grouped by field, in the shape of Zod's flattenError.
|
|
30
|
+
Flattened = Struct.new(:form_errors, :field_errors)
|
|
31
|
+
|
|
32
|
+
FLATTEN = lambda do |issues|
|
|
33
|
+
form = []
|
|
34
|
+
fields = Hash.new { |hash, key| hash[key] = [] }
|
|
35
|
+
issues.each { |issue| issue.path.empty? ? form << issue.message : fields[issue.field] << issue.message }
|
|
36
|
+
Flattened.new(form, Hash[fields])
|
|
37
|
+
end
|
|
38
|
+
private_constant :FLATTEN
|
|
39
|
+
|
|
40
|
+
# What validating a value found: the value as given, and its issues, empty when it passes.
|
|
41
|
+
Result = Struct.new(:value, :issues) do
|
|
42
|
+
def valid?
|
|
43
|
+
issues.empty?
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# The issues by field, for showing them next to inputs or in an error response.
|
|
47
|
+
def flatten
|
|
48
|
+
FLATTEN.call(issues)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Raised by parse when a value fails, with the issues found.
|
|
53
|
+
class ValidationError < StandardError
|
|
54
|
+
attr_reader :schema, :issues
|
|
55
|
+
|
|
56
|
+
def initialize(schema, issues)
|
|
57
|
+
@schema = schema
|
|
58
|
+
@issues = issues
|
|
59
|
+
count = issues.length == 1 ? "1 issue" : "#{issues.length} issues"
|
|
60
|
+
super("#{schema}: #{count}. #{issues.first&.message}".strip)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def flatten
|
|
64
|
+
FLATTEN.call(issues)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# What a message function and a translator get besides the issue: the context validation uses.
|
|
69
|
+
class MessageInfo
|
|
70
|
+
def initialize(make)
|
|
71
|
+
@make = make
|
|
72
|
+
@mutex = Mutex.new
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The instance's context, with any given to the validation merged over it, merged only when read.
|
|
76
|
+
def context
|
|
77
|
+
@mutex.synchronize { @context ||= @make.call }
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# What a check in code gets besides the value at its path: the whole input and the context.
|
|
82
|
+
CheckInfo = Struct.new(:input, :context) do
|
|
83
|
+
# The value at a path in the input, such as "password" or "items.0.name", or nil.
|
|
84
|
+
def read(path)
|
|
85
|
+
Paths.parse(path).reduce(input) do |current, step|
|
|
86
|
+
case step.kind
|
|
87
|
+
when :key then current.is_a?(Hash) ? current[step.key] : (return nil)
|
|
88
|
+
when :index then current.is_a?(Array) ? current[step.index] : (return nil)
|
|
89
|
+
else return nil
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# A check in code that rules cannot express, such as whether a username is
|
|
96
|
+
# taken. check gets the value at its path and the CheckInfo, and passes with
|
|
97
|
+
# true. It runs after the rules, and only where they found no issue at or
|
|
98
|
+
# inside its path, nor in a field it reads. A check that raises reports refineError.
|
|
99
|
+
#
|
|
100
|
+
# Refinement.new(key: "passwordsMatch", path: "confirm", reads: ["password"],
|
|
101
|
+
# message: "Passwords must match",
|
|
102
|
+
# check: ->(value, info) { value == info.read("password") })
|
|
103
|
+
Refinement = Struct.new(:key, :check, :path, :reads, :message, keyword_init: true) do
|
|
104
|
+
def initialize(key:, check:, path: "", reads: [], message: nil)
|
|
105
|
+
super
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Choices for a field that come from the application, such as cities from a
|
|
110
|
+
# service: search suggests them as a person types, and lookup checks a value
|
|
111
|
+
# when validating, nil or false for none and a hash for what it says about the
|
|
112
|
+
# choice, which reports enum when it finds nothing.
|
|
113
|
+
OptionSource = Struct.new(:key, :path, :search, :lookup, :reads, :message, keyword_init: true) do
|
|
114
|
+
def initialize(key:, path:, search: nil, lookup: nil, reads: [], message: nil)
|
|
115
|
+
super
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# The HTML validation attributes a field's rules imply, nil where the rules set none.
|
|
120
|
+
Constraints = Struct.new(:required, :min_length, :max_length, :min, :max, :step, :multiple, keyword_init: true) do
|
|
121
|
+
def initialize(required: false, min_length: nil, max_length: nil, min: nil, max: nil, step: nil, multiple: false)
|
|
122
|
+
super
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# The constraints as HTML attributes, only those set.
|
|
126
|
+
def attributes
|
|
127
|
+
{ required: required || nil, minlength: min_length, maxlength: max_length, min: min, max: max, step: step,
|
|
128
|
+
multiple: multiple || nil }.compact
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# A choice a field allows, as its value with what the schema says about it, such as its title.
|
|
133
|
+
SchemaOption = Struct.new(:value, :fields) do
|
|
134
|
+
def self.build(value, entry)
|
|
135
|
+
fields = (entry || {}).transform_keys(&:to_s)
|
|
136
|
+
fields["value"] = value
|
|
137
|
+
new(value, fields.freeze)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def title
|
|
141
|
+
fields["title"].is_a?(String) ? fields["title"] : nil
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def [](name)
|
|
145
|
+
fields[name.to_s]
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Validates against the schemas of a Featurevisor instance's datafile. It holds
|
|
150
|
+
# no copy of them: every handle reads the current datafile when it is used, so
|
|
151
|
+
# a new datafile takes effect at the next validation.
|
|
152
|
+
class Instance
|
|
153
|
+
def initialize(f, messages: nil, translate: nil)
|
|
154
|
+
@store = Store.new(f)
|
|
155
|
+
@messages = (messages || {}).transform_keys(&:to_s)
|
|
156
|
+
@translate = translate
|
|
157
|
+
@handles = {}
|
|
158
|
+
@listeners = {}
|
|
159
|
+
@mutex = Mutex.new
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# A schema's handle, the same one for the same key, or one with a form's
|
|
163
|
+
# messages by field path, refinements and option sources.
|
|
164
|
+
def get(key, messages: nil, refine: nil, option_sources: nil)
|
|
165
|
+
if messages || refine || option_sources
|
|
166
|
+
return Handle.new(self, key, { messages: messages, refine: refine, option_sources: option_sources })
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Kept only for a key the datafile has, so asking for keys it lacks keeps nothing.
|
|
170
|
+
@mutex.synchronize do
|
|
171
|
+
next @handles[key] if @handles.key?(key)
|
|
172
|
+
|
|
173
|
+
handle = Handle.new(self, key)
|
|
174
|
+
@handles[key] = handle if @store.entry(key)
|
|
175
|
+
handle
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def validate(key, value, context: nil)
|
|
180
|
+
get(key).validate(value, context: context)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def valid?(key, value, context: nil)
|
|
184
|
+
get(key).valid?(value, context: context)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# The value, or a ValidationError with the issues found.
|
|
188
|
+
def parse(key, value, context: nil)
|
|
189
|
+
get(key).parse(value, context: context)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# The keys of the schemas the datafile carries, sorted.
|
|
193
|
+
def keys
|
|
194
|
+
@store.keys
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# Whether the datafile carries a schema.
|
|
198
|
+
def key?(key)
|
|
199
|
+
!@store.entry(key).nil?
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# Stops every subscription and removes the module from the Featurevisor instance.
|
|
203
|
+
def close
|
|
204
|
+
stops = @mutex.synchronize { @listeners.values.tap { @listeners.clear } }
|
|
205
|
+
stops.each(&:call)
|
|
206
|
+
@store.close
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
private
|
|
210
|
+
|
|
211
|
+
attr_reader :store, :messages, :translate
|
|
212
|
+
|
|
213
|
+
def listen(&callback)
|
|
214
|
+
stop = @store.f.on("datafile_set", ->(_details) { callback.call })
|
|
215
|
+
id = Object.new
|
|
216
|
+
@mutex.synchronize { @listeners[id] = stop }
|
|
217
|
+
lambda do
|
|
218
|
+
current = @mutex.synchronize { @listeners.delete(id) }
|
|
219
|
+
current&.call
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# Schemas for the instance create_featurevisor returns. Pass a child's context to validate instead of a child.
|
|
225
|
+
def self.create(f, messages: nil, translate: nil)
|
|
226
|
+
Instance.new(f, messages: messages, translate: translate)
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
require_relative "schemas/values"
|
|
232
|
+
require_relative "schemas/support"
|
|
233
|
+
require_relative "schemas/engine"
|
|
234
|
+
require_relative "schemas/store"
|
|
235
|
+
require_relative "schemas/derived"
|
|
236
|
+
require_relative "schemas/handle"
|
|
237
|
+
|
|
238
|
+
module Featurevisor
|
|
239
|
+
module Schemas
|
|
240
|
+
private_constant :Derived, :Engine, :Messages, :Paths, :Patterns, :Store, :Values, :Views
|
|
241
|
+
end
|
|
242
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: featurevisor-schemas
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 4.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Fahad Heylaal
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: featurevisor
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - '='
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 4.1.0
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - '='
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 4.1.0
|
|
26
|
+
description: Validate data against the schemas a Featurevisor datafile carries
|
|
27
|
+
executables: []
|
|
28
|
+
extensions: []
|
|
29
|
+
extra_rdoc_files: []
|
|
30
|
+
files:
|
|
31
|
+
- LICENSE
|
|
32
|
+
- README.md
|
|
33
|
+
- lib/featurevisor-schemas.rb
|
|
34
|
+
- lib/featurevisor/schemas.rb
|
|
35
|
+
- lib/featurevisor/schemas/derived.rb
|
|
36
|
+
- lib/featurevisor/schemas/engine.rb
|
|
37
|
+
- lib/featurevisor/schemas/handle.rb
|
|
38
|
+
- lib/featurevisor/schemas/store.rb
|
|
39
|
+
- lib/featurevisor/schemas/support.rb
|
|
40
|
+
- lib/featurevisor/schemas/values.rb
|
|
41
|
+
homepage: https://featurevisor.com/docs/schemas/
|
|
42
|
+
licenses:
|
|
43
|
+
- MIT
|
|
44
|
+
metadata:
|
|
45
|
+
source_code_uri: https://github.com/featurevisor/featurevisor-ruby
|
|
46
|
+
documentation_uri: https://featurevisor.com/docs/sdks/ruby/#schemas
|
|
47
|
+
bug_tracker_uri: https://github.com/featurevisor/featurevisor-ruby/issues
|
|
48
|
+
allowed_push_host: https://rubygems.org
|
|
49
|
+
rubygems_mfa_required: 'true'
|
|
50
|
+
rdoc_options: []
|
|
51
|
+
require_paths:
|
|
52
|
+
- lib
|
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: 3.0.0
|
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '0'
|
|
63
|
+
requirements: []
|
|
64
|
+
rubygems_version: 3.6.9
|
|
65
|
+
specification_version: 4
|
|
66
|
+
summary: Schemas for Featurevisor
|
|
67
|
+
test_files: []
|