Commit ff29603
Refactor validator creation to support various configurations (#283)
Fixes #51
This makes a series of changes to the way that a validator is created.
Doing so enables more flexible ways to configure a validator and also
provides a bit more safety when attempting to create a validator with
invalid state. The impetus behind this change was to allow for the
ability to specify a list of seed descriptors when building a validator
and for the optional ability to disable lazy-loading of any descriptors
not provided.
Previously, a validator could be created either with a default
constructor (`new Validator()`) or via a `Config` object (`new
Validator(config)`). However, the `Config` object had no way to provide
a list of descriptors, it only had a `disableLazy` field. Simply adding
a list of descriptors to the `Config` presented a few problems though:
* Since these were two unrelated fields in the config, users had a
serious footgun to play with because it was possible to set
`disableLazy` to true and not specify any descriptors. Doing so would
return an error at validation time with a validation message about an
unknown evaluator.
* Even if the above was tolerable, building validation rules from
descriptors necessitates catching a `CompilationException`. This meant
that if the seed descriptors were added to the config, all users
creating a validator would now have to catch a checked exception even if
they weren't using the seed descriptors.
The solution in this PR converts validator creation from using
constructors into a builder pattern with the following changes:
* `disableLazy` and the new `descriptors` list have been moved from the
config and are now parameters at instantiation time.
* There are two options for building once the builder is created:
* `build()`
* `buildWithDescriptors(List<Descriptor>, boolean disableLazy) throws
CompilationException, InvalidStateException`
The overload that accepts seed descriptors is the only one that throws a
`CompilationException`. If users choose to use `build` with no
arguments, no checked exception needs to be handled.
Note that the above also allows us to provide a helpful check at
instantiation time if a user sets `disableLazy` to true but doesn't
provide any descriptors. In this case, an `IllegalStateException` is
thrown. It is still possible to provide an _incorrect_ list of
descriptors, but that is impossible to catch at instantiation time (but
hopefully is now a bit harder to do).
The new API now adds a `ValidatorFactory` class with static methods for
creating a validator. In addition, the type returned is now an interface
(still named `Validator`) which provides us with a bit more flexibility
both for testing and for making non-breaking changes in the future.
Examples of creating a validator with no seed descriptors:
```java
// Create a Validator with a default config
Validator validator = ValidatorFactory.newBuilder().build();
// Create a Validator with a custom config
Config cfg = Config.newBuilder().setFailFast(true).build();
Validator validator = ValidatoryFactory.newBuilder().withConfig(cfg).build();
```
Creating a validator with seed descriptors is similar. You would still
call `newBuilder`, but at build time, you call `buildWithDescriptors`
and pass the descriptors and set `disableLazy` accordingly. Using this
approach requires catching exceptions at build time:
```java
// Create a validator with seed descriptors
MyMessage msg = MyMessage.newBuilder().build();
List<Descriptor> seedDescriptors = new ArrayList<Descriptor>();
seedDescriptors.add(msg.getDescriptorForType());
try {
Validator validator = ValidatorFactory.newBuilder().buildWithDescriptors(seedDescriptors, false);
// Note you can also create a validator with a config
// Validator validator = ValidatorFactory.newBuilder().withConfig(cfg).buildWithDescriptors(seedDescriptors, false);
// Also note that the buildWithDescriptors function is what throws the exception so the validator
// builder can be created ahead of time with no need for catching an exception.
} catch (CompilationException ce) {
// Exception pre-warming the cache with the seed descriptors
} catch (IllegalStateException ise) {
// This is thrown if seedDescriptors is empty and disableLazy is true
}
```
Note: **THIS IS A BREAKING CHANGE**, but the hope is that this makes it
easier and more intuitive for creating a validator while providing
future-proof flexibility. To migrate, users should make the following
changes:
**With no config**
```diff
import build.buf.protovalidate.Validator;
+ import build.buf.protovalidate.ValidatorFactory;
- Validator validator = new Validator();
+ Validator validator = ValidatorFactory.newBuilder().build();
```
**With config**
```diff
import build.buf.protovalidate.Validator;
+ import build.buf.protovalidate.ValidatorFactory;
Config cfg = Config.newBuilder().setFailFast(true).build();
- Validator validator = new Validator(cfg);
+ Validator validator = ValidatorFactory.newBuilder().withConfig(cfg).build();
```
In addition, the `loadMessages` and `loadDescriptors` methods from the
validator have been removed. Users should instead use the
`newBuilder(List<Descriptor> descriptors, boolean disableLazy)` approach
to load descriptors ahead of time.
---------
Co-authored-by: Philip K. Warren <[email protected]>1 parent 45271ab commit ff29603
File tree
12 files changed
+524
-123
lines changed- conformance/src
- main/java/build/buf/protovalidate/conformance
- test/java/build/buf/protovalidate
- src
- main/java/build/buf/protovalidate
- test
- java/build/buf/protovalidate
- resources/proto/validationtest
12 files changed
+524
-123
lines changedLines changed: 8 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
69 | 71 | | |
70 | 72 | | |
71 | 73 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
28 | 27 | | |
29 | 28 | | |
30 | 29 | | |
31 | 30 | | |
32 | 31 | | |
33 | 32 | | |
34 | | - | |
35 | 33 | | |
36 | 34 | | |
37 | 35 | | |
38 | 36 | | |
39 | | - | |
40 | 37 | | |
41 | 38 | | |
42 | 39 | | |
| |||
60 | 57 | | |
61 | 58 | | |
62 | 59 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | 60 | | |
73 | 61 | | |
74 | 62 | | |
| |||
99 | 87 | | |
100 | 88 | | |
101 | 89 | | |
102 | | - | |
103 | 90 | | |
104 | 91 | | |
105 | 92 | | |
| |||
117 | 104 | | |
118 | 105 | | |
119 | 106 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | 107 | | |
132 | 108 | | |
133 | 109 | | |
| |||
187 | 163 | | |
188 | 164 | | |
189 | 165 | | |
190 | | - | |
| 166 | + | |
191 | 167 | | |
192 | 168 | | |
193 | 169 | | |
Lines changed: 21 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | | - | |
| 62 | + | |
63 | 63 | | |
64 | | - | |
| 64 | + | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
68 | 86 | | |
69 | 87 | | |
70 | 88 | | |
| |||
73 | 91 | | |
74 | 92 | | |
75 | 93 | | |
76 | | - | |
| 94 | + | |
77 | 95 | | |
78 | 96 | | |
79 | 97 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | 20 | | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | 21 | | |
| 22 | + | |
| 23 | + | |
57 | 24 | | |
58 | 25 | | |
59 | 26 | | |
| |||
67 | 34 | | |
68 | 35 | | |
69 | 36 | | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
| 37 | + | |
113 | 38 | | |
Lines changed: 102 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
0 commit comments