Skip to content

Commit 8f288f5

Browse files
committed
Add ignore enum examples
1 parent cd64fa3 commit 8f288f5

File tree

1 file changed

+117
-1
lines changed

1 file changed

+117
-1
lines changed

proto/protovalidate/buf/validate/validate.proto

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,129 @@ message FieldConstraints {
210210
// FieldConstraints.required for definitions of "populated" and "nullable".
211211
enum Ignore {
212212
// Validation is only skipped if it's an unpopulated nullable fields.
213+
//
214+
// ```proto
215+
// syntax="proto3";
216+
//
217+
// message Request {
218+
// // The uri rule applies to any value, including the empty string.
219+
// string foo = 1 [
220+
// (buf.validate.field).string.uri = true
221+
// ];
222+
//
223+
// // The uri rule only applies if the field is set, including if it's
224+
// // set to the empty string.
225+
// optional string bar = 2 [
226+
// (buf.validate.field).string.uri = true
227+
// ];
228+
//
229+
// // The min_items rule always applies, even if the list is empty.
230+
// repeated string baz = 3 [
231+
// (buf.validate.field).repeated.min_items = 3
232+
// ];
233+
//
234+
// // The custom CEL rule applies only if the field is set, including if
235+
// // it's the "zero" value of that message.
236+
// SomeMessage quux = 4 [
237+
// (buf.validate.field).cel = {/* ... */}
238+
// ];
239+
// }
240+
// ```
213241
IGNORE_UNSPECIFIED = 0;
214-
// Validation is skipped if the field is unpopulated.
242+
243+
// Validation is skipped if the field is unpopulated. This rule is redundant
244+
// if the field is already nullable. This value is equivalent behavior to the
245+
// deprecated ignore_empty rule.
246+
//
247+
// ```proto
248+
// syntax="proto3
249+
//
250+
// message Request {
251+
// // The uri rule applies only if the value is not the empty string.
252+
// string foo = 1 [
253+
// (buf.validate.field).string.uri = true,
254+
// (buf.validate.field).ignore = IGNORE_EMPTY
255+
// ];
256+
//
257+
// // IGNORE_EMPTY is equivalent to IGNORE_UNSPECIFIED in this case: the
258+
// // uri rule only applies if the field is set, including if it's set
259+
// // to the empty string.
260+
// optional string bar = 2 [
261+
// (buf.validate.field).string.uri = true,
262+
// (buf.validate.field).ignore = IGNORE_EMPTY
263+
// ];
264+
//
265+
// // The min_items rule only applies if the list has at least one item.
266+
// repeated string baz = 3 [
267+
// (buf.validate.field).repeated.min_items = 3,
268+
// (buf.validate.field).ignore = IGNORE_EMPTY
269+
// ];
270+
//
271+
// // IGNORE_EMPTY is equivalent to IGNORE_UNSPECIFIED in this case: the
272+
// // custom CEL rule applies only if the field is set, including if it's
273+
// // the "zero" value of that message.
274+
// SomeMessage quux = 4 [
275+
// (buf.validate.field).cel = {/* ... */},
276+
// (buf.validate.field).ignore = IGNORE_EMPTY
277+
// ];
278+
// }
279+
// ```
215280
IGNORE_EMPTY = 1;
281+
216282
// Validation is skipped if the field is unpopulated or if it is a nullable
217283
// field populated with its default value. This is typically the zero or
218284
// empty value, but proto2 scalars support custom defaults. For messages, the
219285
// default is a non-null message with all its fields unpopulated.
286+
//
287+
// ```proto
288+
// syntax="proto3
289+
//
290+
// message Request {
291+
// // IGNORE_DEFAULT is equivalent to IGNORE_EMPTY in this case; the uri
292+
// // rule applies only if the value is not the empty string.
293+
// string foo = 1 [
294+
// (buf.validate.field).string.uri = true,
295+
// (buf.validate.field).ignore = IGNORE_DEFAULT
296+
// ];
297+
//
298+
// // The uri rule only applies if the field is set to a value other than
299+
// // the empty string.
300+
// optional string bar = 2 [
301+
// (buf.validate.field).string.uri = true,
302+
// (buf.validate.field).ignore = IGNORE_DEFAULT
303+
// ];
304+
//
305+
// // IGNORE_DEFAULT is equivalent to IGNORE_EMPTY in this case; the
306+
// // min_items rule only applies if the list has at least one item.
307+
// repeated string baz = 3 [
308+
// (buf.validate.field).repeated.min_items = 3,
309+
// (buf.validate.field).ignore = IGNORE_DEFAULT
310+
// ];
311+
//
312+
// // The custom CEL rule only applies if the field is set to a value other
313+
// // than an empty message (i.e., fields are unpopulated).
314+
// SomeMessage quux = 4 [
315+
// (buf.validate.field).cel = {/* ... */},
316+
// (buf.validate.field).ignore = IGNORE_DEFAULT
317+
// ];
318+
// }
319+
// ```
320+
//
321+
// This rule is affected by proto2 custom default values:
322+
//
323+
// ```proto
324+
// syntax="proto2";
325+
//
326+
// message Request {
327+
// // The gt rule only applies if the field is set and it's value is not
328+
// the default (i.e., not -42). The rule even applies if the field is set
329+
// to zero since the default value differs.
330+
// optional int32 value = 1 [
331+
// default = -42,
332+
// (buf.validate.field).int32.gt = 0,
333+
// (buf.validate.field).ignore = IGNORE_DEFAULT
334+
// ];
335+
// }
220336
IGNORE_DEFAULT = 2;
221337
}
222338

0 commit comments

Comments
 (0)