-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprotovalidate-simple.yaml
More file actions
328 lines (217 loc) · 12.7 KB
/
Copy pathprotovalidate-simple.yaml
File metadata and controls
328 lines (217 loc) · 12.7 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
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 "buf/validate/validate.proto";
enum State {
INACTIVE = 0;
PENDING = 1;
ACTIVE = 2;
}
message Person {
uint64 id = 1 [(buf.validate.field).uint64.gt = 999];
string email = 2 [(buf.validate.field).string.email = true];
string name = 3 [(buf.validate.field).string = {
pattern: "^[A-Za-z]+( [A-Za-z]+)*$",
max_bytes: 256,
}];
Location home = 4 [(buf.validate.field).required = true];
message Location {
double lat = 1 [(buf.validate.field).double = {gte: -90, lte: 90}];
double lng = 2 [(buf.validate.field).double = {gte: -180, lte: 180}];
}
}
message NumericExamples {
// x must equal 1.23 exactly
float na = 1 [(buf.validate.field).float.const = 1.23];
// x must be less than 10
int32 nb = 2 [(buf.validate.field).int32.lt = 10];
// x must be greater than or equal to 20
uint64 nc = 3 [(buf.validate.field).uint64.gte = 20];
// x must be in the range [30, 40)
fixed32 nd = 4 [(buf.validate.field).fixed32 = {gte:30, lt: 40}];
// x must be outside the range [30, 40)
double ne = 5 [(buf.validate.field).double = {lt:30, gte:40}];
// x must be outside the range [30, 40)
double nf = 6 [(buf.validate.field).double = {lt:30, gte:40}];
// x must be either 1, 2, or 3
uint32 ng = 7 [(buf.validate.field).uint32 = {in: [1,2,3]}];
// x cannot be 0 nor 0.99
float nh = 8 [(buf.validate.field).float = {not_in: [0, 0.99]}];
// x must but greater or equal 200 and being optional
uint32 ni = 9 [(buf.validate.field).uint32 = {ignore_empty: true, gte: 200}];
// x must be set to true
bool nk = 10 [(buf.validate.field).bool.const = true];
// x cannot be set to true
bool nl = 111 [(buf.validate.field).bool.const = false];
}
message StringExamples {
// x must be set to "foo"
string sa = 12 [(buf.validate.field).string.const = "foo"];
// x must be exactly 5 characters long
string sb = 13 [(buf.validate.field).string.len = 5];
// x must be at least 3 characters long
string sc = 14 [(buf.validate.field).string.min_len = 3];
// x must be between 5 and 10 characters, inclusive
string sd = 15 [(buf.validate.field).string = {min_len: 5, max_len: 10}];
// x must be at most 15 bytes long
string se = 16 [(buf.validate.field).string.max_bytes = 15];
// x must be between 128 and 1024 bytes long
string sf = 17 [(buf.validate.field).string = {min_bytes: 128, max_bytes: 1024}];
// x must be a non-empty, case-insensitive hexadecimal string
string sg = 18 [(buf.validate.field).string.pattern = "(?i)^[0-9a-f]+$"];
// x must begin with "foo"
string sh = 19 [(buf.validate.field).string.prefix = "foo"];
// x must end with "bar"
string si = 20 [(buf.validate.field).string.suffix = "bar"];
// x must contain "baz" anywhere inside it
string sj = 21 [(buf.validate.field).string.contains = "baz"];
// x cannot contain "baz" anywhere inside it
string sk = 22 [(buf.validate.field).string.not_contains = "baz"];
// x must begin with "fizz" and end with "buzz"
string sl = 23 [(buf.validate.field).string = {prefix: "fizz", suffix: "buzz"}];
// x must end with ".proto" and be less than 64 characters
string sm = 24 [(buf.validate.field).string = {suffix: ".proto", max_len:64}];
// x must be either "foo", "bar", or "baz"
string sn = 25 [(buf.validate.field).string = {in: ["foo", "bar", "baz"]}];
// x cannot be "fizz" nor "buzz"
string so = 26 [(buf.validate.field).string = {not_in: ["fizz", "buzz"]}];
// x must be a valid email address (via RFC 5322)
string sp = 27 [(buf.validate.field).string.email = true];
// x must be a valid address (IP or Hostname).
string sq = 28 [(buf.validate.field).string.address = true];
// x must be a valid hostname (via RFC 1034)
string sr = 29 [(buf.validate.field).string.hostname = true];
// x must be a valid IP address (either v4 or v6)
string ss = 30 [(buf.validate.field).string.ip = true];
// x must be a valid IPv4 address
// eg: "192.168.0.1"
string ip4 = 31 [(buf.validate.field).string.ipv4 = true];
// x must be a valid IPv6 address
// eg: "fe80::3"
string ip6 = 32 [(buf.validate.field).string.ipv6 = true];
// x must be a valid absolute URI (via RFC 3986)
string uri = 33 [(buf.validate.field).string.uri = true];
// x must be a valid URI reference (either absolute or relative)
string absuri = 34 [(buf.validate.field).string.uri_ref = true];
// x must be a valid UUID (via RFC 4122)
string uuid = 35 [(buf.validate.field).string.uuid = true];
// x must conform to a well known regex for HTTP header names (via RFC 7230)
string httpheadname = 36 [(buf.validate.field).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 [(buf.validate.field).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 [(buf.validate.field).string {well_known_regex: HTTP_HEADER_VALUE, strict: false}];
}
message ByteExamples {
// x must be set to "foo" ("\x66\x6f\x6f")
bytes ba = 39 [(buf.validate.field).bytes.const = "foo"];
// x must be set to "\xf0\x90\x28\xbc"
bytes bb = 40 [(buf.validate.field).bytes.const = "\xf0\x90\x28\xbc"];
// x must be exactly 3 bytes
bytes bc = 41 [(buf.validate.field).bytes.len = 3];
// x must be at least 3 bytes long
bytes bd = 42 [(buf.validate.field).bytes.min_len = 3];
// x must be between 5 and 10 bytes, inclusive
bytes be = 43 [(buf.validate.field).bytes = {min_len: 5, max_len: 10}];
// x must be a non-empty, ASCII byte sequence
bytes bf = 44 [(buf.validate.field).bytes.pattern = "^[\x00-\x7F]+$"];
// x must begin with "\x99"
bytes bg = 45 [(buf.validate.field).bytes.prefix = "\x99"];
// x must end with "buz\x7a"
bytes bh = 46 [(buf.validate.field).bytes.suffix = "buz\x7a"];
// x must contain "baz" anywhere inside it
bytes bi = 47 [(buf.validate.field).bytes.contains = "baz"];
// x must be either "foo", "bar", or "baz"
bytes bj = 48 [(buf.validate.field).bytes = {in: ["foo", "bar", "baz"]}];
// x cannot be "fizz" nor "buzz"
bytes bk = 49 [(buf.validate.field).bytes = {not_in: ["fizz", "buzz"]}];
bytes bl = 50 [(buf.validate.field).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 [(buf.validate.field).bytes.ip = true];
// x must be a valid IPv4 address in byte format
// eg: "\xC0\xA8\x00\x01"
bytes bn = 52 [(buf.validate.field).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 [(buf.validate.field).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 [(buf.validate.field).enum.const = 2];
// x can only be INACTIVE, PENDING, or ACTIVE
//State ob = 55 [(buf.validate.field).enum.defined_only = true];
// x must be either INACTIVE (0) or ACTIVE (2)
//State oc = 56 [(buf.validate.field).enum = {in: [0,2]}];
// x cannot be PENDING (1)
//State od = 57 [(buf.validate.field).enum = {not_in: [1]}];
// x cannot be unset
//Person of = 59 [(buf.validate.field).required = true];
// x cannot be unset, but the validations on x will not be performed
//Person og = 60 [(buf.validate.field).message = {required: true, skip: true}];
}
message RepeatedExamples {
// x must contain at least 3 elements
repeated int32 ra = 61 [(buf.validate.field).repeated.min_items = 3];
// x must contain between 5 and 10 Persons, inclusive
repeated Person rb = 62 [(buf.validate.field).repeated = {min_items: 5, max_items: 10}];
// x must contain exactly 7 elements
repeated double rc = 63 [(buf.validate.field).repeated = {min_items: 7, max_items: 7}];
// x must contain unique int64 values
repeated int64 rd = 64 [(buf.validate.field).repeated.unique = true];
// x must contain positive float values
repeated float re = 65 [(buf.validate.field).repeated.items.float.gt = 0];
// To complex for protobufjs parser
// repeated int64 rg = 67 [(buf.validate.field).repeated = {ignore_empty: true, items: {int64: {gt: 200}}}];
// x must contain positive float values
repeated float rk = 71 [(buf.validate.field).repeated.items.float.gt = 0];
}
message GoogleExamples {
// if it is set, x must be greater than 3
google.protobuf.Int32Value ga = 77 [(buf.validate.field).int32.gt = 3];
// x cannot be unset
google.protobuf.Duration gb = 78 [(buf.validate.field).required = true];
// x must be less than 10s
google.protobuf.Duration gd = 80 [(buf.validate.field).duration.lt.seconds = 10];
// x must be greater than or equal to 20ns
google.protobuf.Duration ge = 81 [(buf.validate.field).duration.gte.nanos = 20];
// x cannot be unset
google.protobuf.Timestamp gj = 86 [(buf.validate.field).required = true];
// x must be less than the Unix Epoch
google.protobuf.Timestamp gl = 88 [(buf.validate.field).timestamp.lt.seconds = 0];
// x must be greater than or equal to 2009/11/10T23:00:00Z
google.protobuf.Timestamp gm = 89 [(buf.validate.field).timestamp.gte.seconds = 63393490800];
// x must be less than the current timestamp
google.protobuf.Timestamp gp = 92 [(buf.validate.field).timestamp.lt_now = true];
// x must be within ±1s of the current time
google.protobuf.Timestamp gq = 93 [(buf.validate.field).timestamp.within.seconds = 1];
}
// Root
message RootNode {
NumericExamples n = 1;
StringExamples s = 2;
ByteExamples b = 3;
ObjectExamples o = 4;
RepeatedExamples r = 5;
GoogleExamples g = 7;
}