-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprotoc-gen-validate.yaml
More file actions
425 lines (287 loc) · 16.2 KB
/
Copy pathprotoc-gen-validate.yaml
File metadata and controls
425 lines (287 loc) · 16.2 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
asyncapi: 2.0.0
info:
title: Example using ProtoBuff and https://github.com/bufbuild/protoc-gen-validate/blob/main/validate/validate.proto
version: '1.0.0'
channels:
mychannel:
publish:
message:
$ref: '#/components/messages/testMessage'
components:
messages:
testMessage:
schemaFormat: 'application/vnd.google.protobuf;version=3'
payload: |
syntax = "proto3";
package events;
import "google/type/date.proto";
import "google/type/datetime.proto";
import "google/type/decimal.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";
import "validate/validate.proto";
enum State {
INACTIVE = 0;
PENDING = 1;
ACTIVE = 2;
}
message Person {
uint64 id = 1 [(validate.rules).uint64.gt = 999];
string email = 2 [(validate.rules).string.email = true];
string name = 3 [(validate.rules).string = {
pattern: "^[A-Za-z]+( [A-Za-z]+)*$",
max_bytes: 256,
}];
Location home = 4 [(validate.rules).message.required = true];
message Location {
double lat = 1 [(validate.rules).double = {gte: -90, lte: 90}];
double lng = 2 [(validate.rules).double = {gte: -180, lte: 180}];
}
}
message NumericExamples {
// x must equal 1.23 exactly
float na = 1 [(validate.rules).float.const = 1.23];
// x must be less than 10
int32 nb = 2 [(validate.rules).int32.lt = 10];
// x must be greater than or equal to 20
uint64 nc = 3 [(validate.rules).uint64.gte = 20];
// x must be in the range [30, 40)
fixed32 nd = 4 [(validate.rules).fixed32 = {gte:30, lt: 40}];
// x must be outside the range [30, 40)
double ne = 5 [(validate.rules).double = {lt:30, gte:40}];
// x must be outside the range [30, 40)
double nf = 6 [(validate.rules).double = {lt:30, gte:40}];
// x must be either 1, 2, or 3
uint32 ng = 7 [(validate.rules).uint32 = {in: [1,2,3]}];
// x cannot be 0 nor 0.99
float nh = 8 [(validate.rules).float = {not_in: [0, 0.99]}];
// x must but greater or equal 200 and being optional
uint32 ni = 9 [(validate.rules).uint32 = {ignore_empty: true, gte: 200}];
// x must be set to true
bool nk = 10 [(validate.rules).bool.const = true];
// x cannot be set to true
bool nl = 111 [(validate.rules).bool.const = false];
}
message StringExamples {
// x must be set to "foo"
string sa = 12 [(validate.rules).string.const = "foo"];
// x must be exactly 5 characters long
string sb = 13 [(validate.rules).string.len = 5];
// x must be at least 3 characters long
string sc = 14 [(validate.rules).string.min_len = 3];
// x must be between 5 and 10 characters, inclusive
string sd = 15 [(validate.rules).string = {min_len: 5, max_len: 10}];
// x must be at most 15 bytes long
string se = 16 [(validate.rules).string.max_bytes = 15];
// x must be between 128 and 1024 bytes long
string sf = 17 [(validate.rules).string = {min_bytes: 128, max_bytes: 1024}];
// x must be a non-empty, case-insensitive hexadecimal string
string sg = 18 [(validate.rules).string.pattern = "(?i)^[0-9a-f]+$"];
// x must begin with "foo"
string sh = 19 [(validate.rules).string.prefix = "foo"];
// x must end with "bar"
string si = 20 [(validate.rules).string.suffix = "bar"];
// x must contain "baz" anywhere inside it
string sj = 21 [(validate.rules).string.contains = "baz"];
// x cannot contain "baz" anywhere inside it
string sk = 22 [(validate.rules).string.not_contains = "baz"];
// x must begin with "fizz" and end with "buzz"
string sl = 23 [(validate.rules).string = {prefix: "fizz", suffix: "buzz"}];
// x must end with ".proto" and be less than 64 characters
string sm = 24 [(validate.rules).string = {suffix: ".proto", max_len:64}];
// x must be either "foo", "bar", or "baz"
string sn = 25 [(validate.rules).string = {in: ["foo", "bar", "baz"]}];
// x cannot be "fizz" nor "buzz"
string so = 26 [(validate.rules).string = {not_in: ["fizz", "buzz"]}];
// x must be a valid email address (via RFC 5322)
string sp = 27 [(validate.rules).string.email = true];
// x must be a valid address (IP or Hostname).
string sq = 28 [(validate.rules).string.address = true];
// x must be a valid hostname (via RFC 1034)
string sr = 29 [(validate.rules).string.hostname = true];
// x must be a valid IP address (either v4 or v6)
string ss = 30 [(validate.rules).string.ip = true];
// x must be a valid IPv4 address
// eg: "192.168.0.1"
string ip4 = 31 [(validate.rules).string.ipv4 = true];
// x must be a valid IPv6 address
// eg: "fe80::3"
string ip6 = 32 [(validate.rules).string.ipv6 = true];
// x must be a valid absolute URI (via RFC 3986)
string uri = 33 [(validate.rules).string.uri = true];
// x must be a valid URI reference (either absolute or relative)
string absuri = 34 [(validate.rules).string.uri_ref = true];
// x must be a valid UUID (via RFC 4122)
string uuid = 35 [(validate.rules).string.uuid = true];
// x must conform to a well known regex for HTTP header names (via RFC 7230)
string httpheadname = 36 [(validate.rules).string.well_known_regex = HTTP_HEADER_NAME];
// x must conform to a well known regex for HTTP header values (via RFC 7230)
string httpheadvalue = 37 [(validate.rules).string.well_known_regex = HTTP_HEADER_VALUE];
// To complex for protobufjs parser
// x must conform to a well known regex for headers, disallowing \r\n\0 characters.
// string httpheadvaluestrict = 38 [(validate.rules).string {well_known_regex: HTTP_HEADER_VALUE, strict: false}];
}
message ByteExamples {
// x must be set to "foo" ("\x66\x6f\x6f")
bytes ba = 39 [(validate.rules).bytes.const = "foo"];
// x must be set to "\xf0\x90\x28\xbc"
bytes bb = 40 [(validate.rules).bytes.const = "\xf0\x90\x28\xbc"];
// x must be exactly 3 bytes
bytes bc = 41 [(validate.rules).bytes.len = 3];
// x must be at least 3 bytes long
bytes bd = 42 [(validate.rules).bytes.min_len = 3];
// x must be between 5 and 10 bytes, inclusive
bytes be = 43 [(validate.rules).bytes = {min_len: 5, max_len: 10}];
// x must be a non-empty, ASCII byte sequence
bytes bf = 44 [(validate.rules).bytes.pattern = "^[\x00-\x7F]+$"];
// x must begin with "\x99"
bytes bg = 45 [(validate.rules).bytes.prefix = "\x99"];
// x must end with "buz\x7a"
bytes bh = 46 [(validate.rules).bytes.suffix = "buz\x7a"];
// x must contain "baz" anywhere inside it
bytes bi = 47 [(validate.rules).bytes.contains = "baz"];
// x must be either "foo", "bar", or "baz"
bytes bj = 48 [(validate.rules).bytes = {in: ["foo", "bar", "baz"]}];
// x cannot be "fizz" nor "buzz"
bytes bk = 49 [(validate.rules).bytes = {not_in: ["fizz", "buzz"]}];
bytes bl = 50 [(validate.rules).bytes = {ignore_empty: true, in: ["foo", "bar", "baz"]}];
// x must be a valid IP address (either v4 or v6) in byte format
bytes bm = 51 [(validate.rules).bytes.ip = true];
// x must be a valid IPv4 address in byte format
// eg: "\xC0\xA8\x00\x01"
bytes bn = 52 [(validate.rules).bytes.ipv4 = true];
// x must be a valid IPv6 address in byte format
// eg: "\x20\x01\x0D\xB8\x85\xA3\x00\x00\x00\x00\x8A\x2E\x03\x70\x73\x34"
bytes bo = 53 [(validate.rules).bytes.ipv6 = true];
}
// Dont have an idea how to represent those in JsonSchema
message ObjectExamples {
// x must be set to ACTIVE (2)
//State oa = 54 [(validate.rules).enum.const = 2];
// x can only be INACTIVE, PENDING, or ACTIVE
//State ob = 55 [(validate.rules).enum.defined_only = true];
// x must be either INACTIVE (0) or ACTIVE (2)
//State oc = 56 [(validate.rules).enum = {in: [0,2]}];
// x cannot be PENDING (1)
//State od = 57 [(validate.rules).enum = {not_in: [1]}];
// The fields on Person x will not be validated.
//Person oe = 58 [(validate.rules).message.skip = true];
// x cannot be unset
//Person of = 59 [(validate.rules).message.required = true];
// x cannot be unset, but the validations on x will not be performed
//Person og = 60 [(validate.rules).message = {required: true, skip: true}];
}
message RepeatedExamples {
// x must contain at least 3 elements
repeated int32 ra = 61 [(validate.rules).repeated.min_items = 3];
// x must contain between 5 and 10 Persons, inclusive
repeated Person rb = 62 [(validate.rules).repeated = {min_items: 5, max_items: 10}];
// x must contain exactly 7 elements
repeated double rc = 63 [(validate.rules).repeated = {min_items: 7, max_items: 7}];
// x must contain unique int64 values
repeated int64 rd = 64 [(validate.rules).repeated.unique = true];
// x must contain positive float values
repeated float re = 65 [(validate.rules).repeated.items.float.gt = 0];
// x must contain Persons but don't validate them
repeated Person rf = 66 [(validate.rules).repeated.items.message.skip = true];
// To complex for protobufjs parser
// repeated int64 rg = 67 [(validate.rules).repeated = {ignore_empty: true, items: {int64: {gt: 200}}}];
// x must contain positive float values
repeated float rk = 71 [(validate.rules).repeated.items.float.gt = 0];
// x must contain Persons but don't validate them
repeated Person rl = 72 [(validate.rules).repeated.items.message.skip = true];
}
// Dont have an idea how to represent those in JsonSchema
message MapExamples {
// x must contain at least 3 KV pairs
// map<string, uint64> rh = 68 [(validate.rules).map.min_pairs = 3];
// x must contain between 5 and 10 KV pairs
//map<string, string> ri = 69 [(validate.rules).map = {min_pairs: 5, max_pairs: 10}];
// x must contain exactly 7 KV pairs
// map<string, Person> rj = 70 [(validate.rules).map = {min_pairs: 7, max_pairs: 7}];
// x's keys must all be negative
//map<sint32, string> rm = 73 [(validate.rules).map.keys.sint32.lt = 0];
// x must contain strings of at least 3 characters
//map<string, string> rn = 74 [(validate.rules).map.values.string.min_len = 3];
// x must contain Persons but doesn't validate them
//map<string, Person> ro = 75 [(validate.rules).map.values.message.skip = true];
//map<string, string> rp = 76 [(validate.rules).map = {ignore_empty: true, values: {string: {min_len: 3}}}];
}
message GoogleExamples {
// if it is set, x must be greater than 3
google.protobuf.Int32Value ga = 77 [(validate.rules).int32.gt = 3];
// x cannot be unset
google.protobuf.Duration gb = 78 [(validate.rules).duration.required = true];
// To complex for protobufjs parser
// x must equal 1.5s exactly
// google.protobuf.Duration gc = 79 [(validate.rules).duration.const = {
// seconds: 1,
// nanos: 500000000
// }];
// x must be less than 10s
google.protobuf.Duration gd = 80 [(validate.rules).duration.lt.seconds = 10];
// x must be greater than or equal to 20ns
google.protobuf.Duration ge = 81 [(validate.rules).duration.gte.nanos = 20];
// x must be in the range [0s, 1s)
// To complex for protobufjs parser
// google.protobuf.Duration gf = 82 [(validate.rules).duration = {
// gte: {},
// lt: {seconds: 1}
// }];
// x must be outside the range [0s, 1s)
// google.protobuf.Duration gg = 83 [(validate.rules).duration = {
// lt: {},
// gte: {seconds: 1}
// }];
// x must be either 0s or 1s
// google.protobuf.Duration gh = 84 [(validate.rules).duration = {in: [
// {},
// {seconds: 1}
// ]}];
// x cannot be 20s nor 500ns
// google.protobuf.Duration gi = 85 [(validate.rules).duration = {not_in: [
// {seconds: 20},
// {nanos: 500}
// ]}];
// x cannot be unset
google.protobuf.Timestamp gj = 86 [(validate.rules).timestamp.required = true];
// x must equal 2009/11/10T23:00:00.500Z exactly
// google.protobuf.Timestamp gk = 87 [(validate.rules).timestamp.const = {
// seconds: 63393490800,
// nanos: 500000000
// }];
// x must be less than the Unix Epoch
google.protobuf.Timestamp gl = 88 [(validate.rules).timestamp.lt.seconds = 0];
// x must be greater than or equal to 2009/11/10T23:00:00Z
google.protobuf.Timestamp gm = 89 [(validate.rules).timestamp.gte.seconds = 63393490800];
// x must be in the range [epoch, 2009/11/10T23:00:00Z)
// google.protobuf.Timestamp gn = 90 [(validate.rules).timestamp = {
// gte: {},
// lt: {seconds: 63393490800}
// }];
// x must be outside the range [epoch, 2009/11/10T23:00:00Z)
// google.protobuf.Timestamp go = 91 [(validate.rules).timestamp = {
// lt: {},
// gte: {seconds: 63393490800}
// }];
// x must be less than the current timestamp
google.protobuf.Timestamp gp = 92 [(validate.rules).timestamp.lt_now = true];
// x must be within ±1s of the current time
google.protobuf.Timestamp gq = 93 [(validate.rules).timestamp.within.seconds = 1];
// x must be within the range (now, now+1h)
// google.protobuf.Timestamp gr = 94 [(validate.rules).timestamp = {
// gt_now: true,
// within: {seconds: 3600}
// }];
}
// Root
message RootNode {
NumericExamples n = 1;
StringExamples s = 2;
ByteExamples b = 3;
ObjectExamples o = 4;
RepeatedExamples r = 5;
MapExamples m = 6;
GoogleExamples g = 7;
}