protovalidate 0.1.0.beta2-x86_64-linux-musl → 0.1.0.beta3-x86_64-linux-musl
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 +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +17 -0
- data/lib/protovalidate/3.3/protovalidate_native.so +0 -0
- data/lib/protovalidate/3.4/protovalidate_native.so +0 -0
- data/lib/protovalidate/4.0/protovalidate_native.so +0 -0
- data/lib/protovalidate/validator.rb +78 -17
- data/lib/protovalidate/version.rb +1 -1
- data/lib/protovalidate.rb +19 -0
- data/sig/protovalidate/native.rbs +7 -0
- data/sig/protovalidate.rbs +22 -3
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e031f847eb9af657c7b9889fd7d193bc06b5bfb229f6f87acaa6d0a043da734c
|
|
4
|
+
data.tar.gz: 1cae86079996f8eac7c9e997bf8a488c8ad060df02290225a7a017f3d1f9e1d8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 77af95b01c8e02e98a753351bc170fdee47f087341fa6be5f8c5c06c14e753bfee86c4bcaa31f55a0c108d749ce5a58f3b75b76ded25bc9e94bb79cff7c62ff5
|
|
7
|
+
data.tar.gz: 139ecc9b4a46773ff00dc02fc9b831f9d0e267be8a5a0f88a0899a674ac992add1b32c6ec9dc9ab9660f01298f57dfa644411a0d320b46fe79f70374d86da3c3
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.0.beta3 (2026-09-22)
|
|
4
|
+
|
|
5
|
+
- `Protovalidate.register` and `Validator#register` compile the rules of message types ahead of their first validation, so the compilation, which blocks concurrent validations, can run at boot instead of inside a request. `register_all` does the same for every loaded message class.
|
|
6
|
+
- Validating an already seen message type no longer takes a lock or allocates its type name on the Ruby side.
|
|
7
|
+
|
|
3
8
|
## 0.1.0.beta2 (2026-09-21)
|
|
4
9
|
|
|
5
10
|
- Precompiled gems for `x86_64-linux-musl`, `aarch64-linux-musl`, `arm-linux-gnu` and `x64-mingw-ucrt`.
|
data/README.md
CHANGED
|
@@ -77,6 +77,23 @@ All inherit from `Protovalidate::Error` except `ArgumentError`.
|
|
|
77
77
|
|
|
78
78
|
A `Validator` compiles the rules of each message type once and caches them, so reuse one instance. Validating from several threads at once is supported and releases the GVL while protovalidate-cc runs. Ractors are not supported.
|
|
79
79
|
|
|
80
|
+
The rules of a message type are compiled the first time a message of that type is validated, and protovalidate-cc holds an exclusive lock while it compiles, so every concurrent validation on the same validator waits until the compilation finishes. To keep that off the request path, register the message types your application validates at boot, for example from a Rails initializer or before the server forks workers:
|
|
81
|
+
|
|
82
|
+
```ruby
|
|
83
|
+
Protovalidate.register(Example::User, Example::Order)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`Protovalidate.register_all` registers every message class loaded so far instead of a hand-maintained list, so call it once all generated code has been loaded. In a Rails application, `config.after_initialize` callbacks run in the `finisher_hook` initializer, after the `eager_load!` initializer, so with `config.eager_load` enabled every generated class under an autoload path is already loaded by then. Generated code outside the autoload paths, such as under `lib/`, must be required before the callback runs.
|
|
87
|
+
|
|
88
|
+
```ruby
|
|
89
|
+
# config/initializers/protovalidate.rb
|
|
90
|
+
Rails.application.config.after_initialize do
|
|
91
|
+
Protovalidate.register_all if Rails.application.config.eager_load
|
|
92
|
+
end
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Types that were not registered still compile on first use, so registration is an optimization rather than a requirement. Both methods raise `Protovalidate::CompilationError` for a rule that does not compile, so a bad rule fails at boot rather than on the first request that reaches it. `register_all` skips message types the engine cannot see, which happens when google-protobuf ships a newer copy of a file compiled into the extension, such as `google/protobuf/descriptor.proto`; those types cannot be validated either way. Once a type is registered, validating it takes no lock on the Ruby side, and the shared validator returned by `Protovalidate.validator` is created once and then read without locking.
|
|
96
|
+
|
|
80
97
|
## Development
|
|
81
98
|
|
|
82
99
|
```bash
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -9,17 +9,57 @@ module Protovalidate
|
|
|
9
9
|
# type once and caches them, so reuse one instance rather than creating one
|
|
10
10
|
# per validation. Instances may be shared between threads; Ractors are not
|
|
11
11
|
# supported.
|
|
12
|
+
#
|
|
13
|
+
# The rules of a message type are compiled the first time it is validated,
|
|
14
|
+
# and concurrent validations wait while that happens. {#register} moves the
|
|
15
|
+
# compilation to a time of your choosing, such as application boot.
|
|
12
16
|
class Validator
|
|
17
|
+
# Every generated message class is a direct subclass of this private
|
|
18
|
+
# google-protobuf class, so its subclasses are the loaded message classes.
|
|
19
|
+
MESSAGE_BASE = Google::Protobuf.const_get(:AbstractMessage)
|
|
20
|
+
private_constant :MESSAGE_BASE
|
|
21
|
+
|
|
13
22
|
# @param descriptor_pool [Google::Protobuf::DescriptorPool] pool the
|
|
14
23
|
# validated messages' schema files and their imports are looked up in
|
|
15
24
|
def initialize(descriptor_pool: Google::Protobuf::DescriptorPool.generated_pool)
|
|
16
25
|
@descriptor_pool = descriptor_pool
|
|
17
26
|
@engine = Native::Engine.new
|
|
18
27
|
@registered_files = Set.new
|
|
19
|
-
|
|
28
|
+
types = {} #: Hash[message_class, String]
|
|
29
|
+
@types = types.compare_by_identity.freeze
|
|
20
30
|
@registration = Mutex.new
|
|
21
31
|
end
|
|
22
32
|
|
|
33
|
+
# Compiles the rules of message classes ahead of their first validation.
|
|
34
|
+
#
|
|
35
|
+
# Classes not registered here are still compiled on first use, so this is
|
|
36
|
+
# an optimization, not a requirement.
|
|
37
|
+
#
|
|
38
|
+
# @param message_classes [Array<Class>] message classes generated by google-protobuf
|
|
39
|
+
# @return [void]
|
|
40
|
+
# @raise [CompilationError] when the rules of a message type cannot be compiled
|
|
41
|
+
# @raise [ArgumentError] when an import of a message's file is missing from the pool
|
|
42
|
+
# @raise [TypeError] when a class is not a Protobuf message class
|
|
43
|
+
def register(*message_classes)
|
|
44
|
+
message_classes.each { |klass| type_name_of(klass) }
|
|
45
|
+
nil
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Registers every message class loaded so far whose type is defined in
|
|
49
|
+
# this validator's descriptor pool. Call it once all generated code has
|
|
50
|
+
# been loaded, such as after eager loading.
|
|
51
|
+
#
|
|
52
|
+
# Types the engine cannot see are skipped: google-protobuf may ship a
|
|
53
|
+
# newer copy of a file compiled into the extension, such as
|
|
54
|
+
# descriptor.proto, and the types it added cannot be validated anyway.
|
|
55
|
+
#
|
|
56
|
+
# @return [void]
|
|
57
|
+
# @raise (see #register)
|
|
58
|
+
def register_all
|
|
59
|
+
loaded_message_classes.each { |klass| register_class(klass, skip_unknown: true) }
|
|
60
|
+
nil
|
|
61
|
+
end
|
|
62
|
+
|
|
23
63
|
# Validates a message, raising when it is invalid.
|
|
24
64
|
#
|
|
25
65
|
# @param message [Google::Protobuf::MessageExts]
|
|
@@ -46,9 +86,8 @@ module Protovalidate
|
|
|
46
86
|
# @raise [EvaluationError] when a rule fails during evaluation
|
|
47
87
|
# @raise [TypeError] when `message` is not a Protobuf message
|
|
48
88
|
def collect_violations(message, fail_fast: false)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
serialized = @engine.validate(descriptor.name, message.class.encode(message), fail_fast)
|
|
89
|
+
type_name = type_name_of(message.class)
|
|
90
|
+
serialized = @engine.validate(type_name, message.class.encode(message), fail_fast)
|
|
52
91
|
return [] if serialized.nil?
|
|
53
92
|
|
|
54
93
|
Buf::Validate::Violations.decode(serialized).violations.map do |proto|
|
|
@@ -56,24 +95,46 @@ module Protovalidate
|
|
|
56
95
|
end
|
|
57
96
|
end
|
|
58
97
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
klass.descriptor
|
|
98
|
+
# The full name of a message class's type, registering and compiling it on
|
|
99
|
+
# first sight. Registered classes resolve with one lookup and no lock.
|
|
100
|
+
private def type_name_of(klass)
|
|
101
|
+
@types[klass] || register_class(klass)
|
|
66
102
|
end
|
|
67
103
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
104
|
+
# @types is replaced with a frozen copy rather than mutated, so a reader
|
|
105
|
+
# outside the lock never observes a Hash being written to.
|
|
106
|
+
private def register_class(klass, skip_unknown: false)
|
|
107
|
+
descriptor = descriptor_of(klass)
|
|
71
108
|
@registration.synchronize do
|
|
72
|
-
|
|
109
|
+
@types[klass] || begin
|
|
110
|
+
register_file(descriptor.file_descriptor)
|
|
111
|
+
name = descriptor.name.freeze
|
|
112
|
+
next if skip_unknown && !@engine.message_type?(name)
|
|
113
|
+
|
|
114
|
+
@engine.compile(name)
|
|
115
|
+
types = @types.dup
|
|
116
|
+
types[klass] = name
|
|
117
|
+
@types = types.freeze
|
|
118
|
+
name
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Classes generated from other pools are skipped, since their files are
|
|
124
|
+
# not resolvable against this one.
|
|
125
|
+
private def loaded_message_classes
|
|
126
|
+
MESSAGE_BASE.subclasses.select do |klass|
|
|
127
|
+
descriptor = klass.descriptor
|
|
128
|
+
@descriptor_pool.lookup(descriptor.name).equal?(descriptor)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
73
131
|
|
|
74
|
-
|
|
75
|
-
|
|
132
|
+
private def descriptor_of(klass)
|
|
133
|
+
unless klass.is_a?(Google::Protobuf::MessageExts::ClassMethods)
|
|
134
|
+
raise TypeError, "expected a Google::Protobuf message class, got #{klass.inspect}"
|
|
76
135
|
end
|
|
136
|
+
|
|
137
|
+
klass.descriptor
|
|
77
138
|
end
|
|
78
139
|
|
|
79
140
|
# The engine resolves imports eagerly, so they are added before the importer.
|
data/lib/protovalidate.rb
CHANGED
|
@@ -32,6 +32,25 @@ module Protovalidate
|
|
|
32
32
|
validator.validate(message, fail_fast:)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
+
# Compiles the rules of message classes ahead of their first validation with
|
|
36
|
+
# the shared validator, for example at application boot.
|
|
37
|
+
#
|
|
38
|
+
# @param (see Validator#register)
|
|
39
|
+
# @return [void]
|
|
40
|
+
# @raise (see Validator#register)
|
|
41
|
+
def self.register(*message_classes)
|
|
42
|
+
validator.register(*message_classes)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Registers every loaded message class of the generated pool with the shared
|
|
46
|
+
# validator. Call it once all generated code has been loaded.
|
|
47
|
+
#
|
|
48
|
+
# @return [void]
|
|
49
|
+
# @raise (see Validator#register)
|
|
50
|
+
def self.register_all
|
|
51
|
+
validator.register_all
|
|
52
|
+
end
|
|
53
|
+
|
|
35
54
|
# Collects the violations of a message with the shared validator.
|
|
36
55
|
#
|
|
37
56
|
# @param (see Validator#collect_violations)
|
|
@@ -6,6 +6,13 @@ module Protovalidate
|
|
|
6
6
|
# Adds a serialized google.protobuf.FileDescriptorProto to the pool.
|
|
7
7
|
def add_file: (String file) -> void
|
|
8
8
|
|
|
9
|
+
# Whether the engine's pool knows the named message type.
|
|
10
|
+
def message_type?: (String type_name) -> bool
|
|
11
|
+
|
|
12
|
+
# Compiles the rules of the named type and of every type reachable from
|
|
13
|
+
# it, ahead of the first validate.
|
|
14
|
+
def compile: (String type_name) -> void
|
|
15
|
+
|
|
9
16
|
# Validates a serialized message of the named type; returns serialized
|
|
10
17
|
# buf.validate.Violations, or nil when the message is valid.
|
|
11
18
|
def validate: (String type_name, String payload, bool fail_fast) -> String?
|
data/sig/protovalidate.rbs
CHANGED
|
@@ -11,11 +11,18 @@ module Protovalidate
|
|
|
11
11
|
# A Protobuf message instance produced by google-protobuf.
|
|
12
12
|
type message = Google::Protobuf::MessageExts
|
|
13
13
|
|
|
14
|
+
# A Protobuf message class generated by google-protobuf.
|
|
15
|
+
type message_class = Google::Protobuf::MessageExts::ClassMethods
|
|
16
|
+
|
|
14
17
|
# The validator shared by the module-level functions.
|
|
15
18
|
def self.validator: () -> Validator
|
|
16
19
|
|
|
17
20
|
def self.validate: (message message, ?fail_fast: bool) -> void
|
|
18
21
|
|
|
22
|
+
def self.register: (*message_class message_classes) -> void
|
|
23
|
+
|
|
24
|
+
def self.register_all: () -> void
|
|
25
|
+
|
|
19
26
|
def self.collect_violations: (message message, ?fail_fast: bool) -> Array[Violation]
|
|
20
27
|
|
|
21
28
|
class Error < StandardError
|
|
@@ -36,21 +43,33 @@ module Protovalidate
|
|
|
36
43
|
end
|
|
37
44
|
|
|
38
45
|
class Validator
|
|
46
|
+
# google-protobuf's private base class of generated messages, reached with const_get.
|
|
47
|
+
MESSAGE_BASE: untyped
|
|
48
|
+
|
|
39
49
|
@descriptor_pool: Google::Protobuf::DescriptorPool
|
|
40
50
|
@engine: Native::Engine
|
|
41
51
|
@registered_files: Set[String]
|
|
42
|
-
|
|
52
|
+
# Message class to its full type name, replaced as a whole on registration.
|
|
53
|
+
@types: Hash[message_class, String]
|
|
43
54
|
@registration: Mutex
|
|
44
55
|
|
|
45
56
|
def initialize: (?descriptor_pool: Google::Protobuf::DescriptorPool) -> void
|
|
46
57
|
|
|
58
|
+
def register: (*message_class message_classes) -> void
|
|
59
|
+
|
|
60
|
+
def register_all: () -> void
|
|
61
|
+
|
|
47
62
|
def validate: (message message, ?fail_fast: bool) -> void
|
|
48
63
|
|
|
49
64
|
def collect_violations: (message message, ?fail_fast: bool) -> Array[Violation]
|
|
50
65
|
|
|
51
|
-
private def
|
|
66
|
+
private def loaded_message_classes: () -> Array[message_class]
|
|
67
|
+
|
|
68
|
+
private def type_name_of: (message_class klass) -> String
|
|
69
|
+
|
|
70
|
+
private def register_class: (message_class klass, ?skip_unknown: bool) -> String?
|
|
52
71
|
|
|
53
|
-
private def
|
|
72
|
+
private def descriptor_of: (message_class klass) -> Google::Protobuf::Descriptor
|
|
54
73
|
|
|
55
74
|
private def register_file: (Google::Protobuf::FileDescriptor file) -> void
|
|
56
75
|
end
|