项目地址

https://github.com/envoyproxy/protoc-gen-validate

前置条件

需要安装protoc、protoc-gen-go、protoc-gen-validate, protoc和protoc-gen-go已经安装过了, 不懂的可以看之前的笔记, 传送门, 下面介绍一下protoc-gen-validate的安装和配置
1.先试用go get -d github.com/envoyproxy/protoc-gen-validate
2.然后我们需要cd到envoyproxy目录下, 我的是这样的, 然后我们在cd到protoc-gen-validate@v0.1.0目录下
image.png
3.执行make build命令, 这样我们就可以使用protoc-gen-validate插件了
3.可以通过protoc-gen-validate命令检查, 如果不报错, 说明安装成功了
image.png

快速体验

protocol文件的编写

  1. syntax = "proto3";
  2. import "validate.proto";
  3. option go_package=".;bark";
  4. service Bark {
  5. rpc SayHello (Person) returns (Person);
  6. }
  7. message Person {
  8. uint64 id = 1 [(validate.rules).uint64.gt = 999];
  9. string email = 2 [(validate.rules).string.email = true];
  10. string mobile = 3 [(validate.rules).string = {
  11. pattern: "^1\\d{10}$"}];
  12. }

在proto文件同级目录下创建一个名为validate.proto的文件, 贴入以下内容

  1. syntax = "proto2";
  2. package validate;
  3. option go_package = "github.com/envoyproxy/protoc-gen-validate/validate";
  4. import "google/protobuf/descriptor.proto";
  5. import "google/protobuf/duration.proto";
  6. import "google/protobuf/timestamp.proto";
  7. // Validation rules applied at the message level
  8. extend google.protobuf.MessageOptions {
  9. // Disabled nullifies any validation rules for this message, including any
  10. // message fields associated with it that do support validation.
  11. optional bool disabled = 1071;
  12. // Ignore skips generation of validation methods for this message.
  13. optional bool ignored = 1072;
  14. }
  15. // Validation rules applied at the oneof level
  16. extend google.protobuf.OneofOptions {
  17. // Required ensures that exactly one the field options in a oneof is set;
  18. // validation fails if no fields in the oneof are set.
  19. optional bool required = 1071;
  20. }
  21. // Validation rules applied at the field level
  22. extend google.protobuf.FieldOptions {
  23. // Rules specify the validations to be performed on this field. By default,
  24. // no validation is performed against a field.
  25. optional FieldRules rules = 1071;
  26. }
  27. // FieldRules encapsulates the rules for each type of field. Depending on the
  28. // field, the correct set should be used to ensure proper validations.
  29. message FieldRules {
  30. optional MessageRules message = 17;
  31. oneof type {
  32. // Scalar Field Types
  33. FloatRules float = 1;
  34. DoubleRules double = 2;
  35. Int32Rules int32 = 3;
  36. Int64Rules int64 = 4;
  37. UInt32Rules uint32 = 5;
  38. UInt64Rules uint64 = 6;
  39. SInt32Rules sint32 = 7;
  40. SInt64Rules sint64 = 8;
  41. Fixed32Rules fixed32 = 9;
  42. Fixed64Rules fixed64 = 10;
  43. SFixed32Rules sfixed32 = 11;
  44. SFixed64Rules sfixed64 = 12;
  45. BoolRules bool = 13;
  46. StringRules string = 14;
  47. BytesRules bytes = 15;
  48. // Complex Field Types
  49. EnumRules enum = 16;
  50. RepeatedRules repeated = 18;
  51. MapRules map = 19;
  52. // Well-Known Field Types
  53. AnyRules any = 20;
  54. DurationRules duration = 21;
  55. TimestampRules timestamp = 22;
  56. }
  57. }
  58. // FloatRules describes the constraints applied to `float` values
  59. message FloatRules {
  60. // Const specifies that this field must be exactly the specified value
  61. optional float const = 1;
  62. // Lt specifies that this field must be less than the specified value,
  63. // exclusive
  64. optional float lt = 2;
  65. // Lte specifies that this field must be less than or equal to the
  66. // specified value, inclusive
  67. optional float lte = 3;
  68. // Gt specifies that this field must be greater than the specified value,
  69. // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
  70. // range is reversed.
  71. optional float gt = 4;
  72. // Gte specifies that this field must be greater than or equal to the
  73. // specified value, inclusive. If the value of Gte is larger than a
  74. // specified Lt or Lte, the range is reversed.
  75. optional float gte = 5;
  76. // In specifies that this field must be equal to one of the specified
  77. // values
  78. repeated float in = 6;
  79. // NotIn specifies that this field cannot be equal to one of the specified
  80. // values
  81. repeated float not_in = 7;
  82. // IgnoreEmpty specifies that the validation rules of this field should be
  83. // evaluated only if the field is not empty
  84. optional bool ignore_empty = 8;
  85. }
  86. // DoubleRules describes the constraints applied to `double` values
  87. message DoubleRules {
  88. // Const specifies that this field must be exactly the specified value
  89. optional double const = 1;
  90. // Lt specifies that this field must be less than the specified value,
  91. // exclusive
  92. optional double lt = 2;
  93. // Lte specifies that this field must be less than or equal to the
  94. // specified value, inclusive
  95. optional double lte = 3;
  96. // Gt specifies that this field must be greater than the specified value,
  97. // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
  98. // range is reversed.
  99. optional double gt = 4;
  100. // Gte specifies that this field must be greater than or equal to the
  101. // specified value, inclusive. If the value of Gte is larger than a
  102. // specified Lt or Lte, the range is reversed.
  103. optional double gte = 5;
  104. // In specifies that this field must be equal to one of the specified
  105. // values
  106. repeated double in = 6;
  107. // NotIn specifies that this field cannot be equal to one of the specified
  108. // values
  109. repeated double not_in = 7;
  110. // IgnoreEmpty specifies that the validation rules of this field should be
  111. // evaluated only if the field is not empty
  112. optional bool ignore_empty = 8;
  113. }
  114. // Int32Rules describes the constraints applied to `int32` values
  115. message Int32Rules {
  116. // Const specifies that this field must be exactly the specified value
  117. optional int32 const = 1;
  118. // Lt specifies that this field must be less than the specified value,
  119. // exclusive
  120. optional int32 lt = 2;
  121. // Lte specifies that this field must be less than or equal to the
  122. // specified value, inclusive
  123. optional int32 lte = 3;
  124. // Gt specifies that this field must be greater than the specified value,
  125. // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
  126. // range is reversed.
  127. optional int32 gt = 4;
  128. // Gte specifies that this field must be greater than or equal to the
  129. // specified value, inclusive. If the value of Gte is larger than a
  130. // specified Lt or Lte, the range is reversed.
  131. optional int32 gte = 5;
  132. // In specifies that this field must be equal to one of the specified
  133. // values
  134. repeated int32 in = 6;
  135. // NotIn specifies that this field cannot be equal to one of the specified
  136. // values
  137. repeated int32 not_in = 7;
  138. // IgnoreEmpty specifies that the validation rules of this field should be
  139. // evaluated only if the field is not empty
  140. optional bool ignore_empty = 8;
  141. }
  142. // Int64Rules describes the constraints applied to `int64` values
  143. message Int64Rules {
  144. // Const specifies that this field must be exactly the specified value
  145. optional int64 const = 1;
  146. // Lt specifies that this field must be less than the specified value,
  147. // exclusive
  148. optional int64 lt = 2;
  149. // Lte specifies that this field must be less than or equal to the
  150. // specified value, inclusive
  151. optional int64 lte = 3;
  152. // Gt specifies that this field must be greater than the specified value,
  153. // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
  154. // range is reversed.
  155. optional int64 gt = 4;
  156. // Gte specifies that this field must be greater than or equal to the
  157. // specified value, inclusive. If the value of Gte is larger than a
  158. // specified Lt or Lte, the range is reversed.
  159. optional int64 gte = 5;
  160. // In specifies that this field must be equal to one of the specified
  161. // values
  162. repeated int64 in = 6;
  163. // NotIn specifies that this field cannot be equal to one of the specified
  164. // values
  165. repeated int64 not_in = 7;
  166. // IgnoreEmpty specifies that the validation rules of this field should be
  167. // evaluated only if the field is not empty
  168. optional bool ignore_empty = 8;
  169. }
  170. // UInt32Rules describes the constraints applied to `uint32` values
  171. message UInt32Rules {
  172. // Const specifies that this field must be exactly the specified value
  173. optional uint32 const = 1;
  174. // Lt specifies that this field must be less than the specified value,
  175. // exclusive
  176. optional uint32 lt = 2;
  177. // Lte specifies that this field must be less than or equal to the
  178. // specified value, inclusive
  179. optional uint32 lte = 3;
  180. // Gt specifies that this field must be greater than the specified value,
  181. // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
  182. // range is reversed.
  183. optional uint32 gt = 4;
  184. // Gte specifies that this field must be greater than or equal to the
  185. // specified value, inclusive. If the value of Gte is larger than a
  186. // specified Lt or Lte, the range is reversed.
  187. optional uint32 gte = 5;
  188. // In specifies that this field must be equal to one of the specified
  189. // values
  190. repeated uint32 in = 6;
  191. // NotIn specifies that this field cannot be equal to one of the specified
  192. // values
  193. repeated uint32 not_in = 7;
  194. // IgnoreEmpty specifies that the validation rules of this field should be
  195. // evaluated only if the field is not empty
  196. optional bool ignore_empty = 8;
  197. }
  198. // UInt64Rules describes the constraints applied to `uint64` values
  199. message UInt64Rules {
  200. // Const specifies that this field must be exactly the specified value
  201. optional uint64 const = 1;
  202. // Lt specifies that this field must be less than the specified value,
  203. // exclusive
  204. optional uint64 lt = 2;
  205. // Lte specifies that this field must be less than or equal to the
  206. // specified value, inclusive
  207. optional uint64 lte = 3;
  208. // Gt specifies that this field must be greater than the specified value,
  209. // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
  210. // range is reversed.
  211. optional uint64 gt = 4;
  212. // Gte specifies that this field must be greater than or equal to the
  213. // specified value, inclusive. If the value of Gte is larger than a
  214. // specified Lt or Lte, the range is reversed.
  215. optional uint64 gte = 5;
  216. // In specifies that this field must be equal to one of the specified
  217. // values
  218. repeated uint64 in = 6;
  219. // NotIn specifies that this field cannot be equal to one of the specified
  220. // values
  221. repeated uint64 not_in = 7;
  222. // IgnoreEmpty specifies that the validation rules of this field should be
  223. // evaluated only if the field is not empty
  224. optional bool ignore_empty = 8;
  225. }
  226. // SInt32Rules describes the constraints applied to `sint32` values
  227. message SInt32Rules {
  228. // Const specifies that this field must be exactly the specified value
  229. optional sint32 const = 1;
  230. // Lt specifies that this field must be less than the specified value,
  231. // exclusive
  232. optional sint32 lt = 2;
  233. // Lte specifies that this field must be less than or equal to the
  234. // specified value, inclusive
  235. optional sint32 lte = 3;
  236. // Gt specifies that this field must be greater than the specified value,
  237. // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
  238. // range is reversed.
  239. optional sint32 gt = 4;
  240. // Gte specifies that this field must be greater than or equal to the
  241. // specified value, inclusive. If the value of Gte is larger than a
  242. // specified Lt or Lte, the range is reversed.
  243. optional sint32 gte = 5;
  244. // In specifies that this field must be equal to one of the specified
  245. // values
  246. repeated sint32 in = 6;
  247. // NotIn specifies that this field cannot be equal to one of the specified
  248. // values
  249. repeated sint32 not_in = 7;
  250. // IgnoreEmpty specifies that the validation rules of this field should be
  251. // evaluated only if the field is not empty
  252. optional bool ignore_empty = 8;
  253. }
  254. // SInt64Rules describes the constraints applied to `sint64` values
  255. message SInt64Rules {
  256. // Const specifies that this field must be exactly the specified value
  257. optional sint64 const = 1;
  258. // Lt specifies that this field must be less than the specified value,
  259. // exclusive
  260. optional sint64 lt = 2;
  261. // Lte specifies that this field must be less than or equal to the
  262. // specified value, inclusive
  263. optional sint64 lte = 3;
  264. // Gt specifies that this field must be greater than the specified value,
  265. // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
  266. // range is reversed.
  267. optional sint64 gt = 4;
  268. // Gte specifies that this field must be greater than or equal to the
  269. // specified value, inclusive. If the value of Gte is larger than a
  270. // specified Lt or Lte, the range is reversed.
  271. optional sint64 gte = 5;
  272. // In specifies that this field must be equal to one of the specified
  273. // values
  274. repeated sint64 in = 6;
  275. // NotIn specifies that this field cannot be equal to one of the specified
  276. // values
  277. repeated sint64 not_in = 7;
  278. // IgnoreEmpty specifies that the validation rules of this field should be
  279. // evaluated only if the field is not empty
  280. optional bool ignore_empty = 8;
  281. }
  282. // Fixed32Rules describes the constraints applied to `fixed32` values
  283. message Fixed32Rules {
  284. // Const specifies that this field must be exactly the specified value
  285. optional fixed32 const = 1;
  286. // Lt specifies that this field must be less than the specified value,
  287. // exclusive
  288. optional fixed32 lt = 2;
  289. // Lte specifies that this field must be less than or equal to the
  290. // specified value, inclusive
  291. optional fixed32 lte = 3;
  292. // Gt specifies that this field must be greater than the specified value,
  293. // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
  294. // range is reversed.
  295. optional fixed32 gt = 4;
  296. // Gte specifies that this field must be greater than or equal to the
  297. // specified value, inclusive. If the value of Gte is larger than a
  298. // specified Lt or Lte, the range is reversed.
  299. optional fixed32 gte = 5;
  300. // In specifies that this field must be equal to one of the specified
  301. // values
  302. repeated fixed32 in = 6;
  303. // NotIn specifies that this field cannot be equal to one of the specified
  304. // values
  305. repeated fixed32 not_in = 7;
  306. // IgnoreEmpty specifies that the validation rules of this field should be
  307. // evaluated only if the field is not empty
  308. optional bool ignore_empty = 8;
  309. }
  310. // Fixed64Rules describes the constraints applied to `fixed64` values
  311. message Fixed64Rules {
  312. // Const specifies that this field must be exactly the specified value
  313. optional fixed64 const = 1;
  314. // Lt specifies that this field must be less than the specified value,
  315. // exclusive
  316. optional fixed64 lt = 2;
  317. // Lte specifies that this field must be less than or equal to the
  318. // specified value, inclusive
  319. optional fixed64 lte = 3;
  320. // Gt specifies that this field must be greater than the specified value,
  321. // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
  322. // range is reversed.
  323. optional fixed64 gt = 4;
  324. // Gte specifies that this field must be greater than or equal to the
  325. // specified value, inclusive. If the value of Gte is larger than a
  326. // specified Lt or Lte, the range is reversed.
  327. optional fixed64 gte = 5;
  328. // In specifies that this field must be equal to one of the specified
  329. // values
  330. repeated fixed64 in = 6;
  331. // NotIn specifies that this field cannot be equal to one of the specified
  332. // values
  333. repeated fixed64 not_in = 7;
  334. // IgnoreEmpty specifies that the validation rules of this field should be
  335. // evaluated only if the field is not empty
  336. optional bool ignore_empty = 8;
  337. }
  338. // SFixed32Rules describes the constraints applied to `sfixed32` values
  339. message SFixed32Rules {
  340. // Const specifies that this field must be exactly the specified value
  341. optional sfixed32 const = 1;
  342. // Lt specifies that this field must be less than the specified value,
  343. // exclusive
  344. optional sfixed32 lt = 2;
  345. // Lte specifies that this field must be less than or equal to the
  346. // specified value, inclusive
  347. optional sfixed32 lte = 3;
  348. // Gt specifies that this field must be greater than the specified value,
  349. // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
  350. // range is reversed.
  351. optional sfixed32 gt = 4;
  352. // Gte specifies that this field must be greater than or equal to the
  353. // specified value, inclusive. If the value of Gte is larger than a
  354. // specified Lt or Lte, the range is reversed.
  355. optional sfixed32 gte = 5;
  356. // In specifies that this field must be equal to one of the specified
  357. // values
  358. repeated sfixed32 in = 6;
  359. // NotIn specifies that this field cannot be equal to one of the specified
  360. // values
  361. repeated sfixed32 not_in = 7;
  362. // IgnoreEmpty specifies that the validation rules of this field should be
  363. // evaluated only if the field is not empty
  364. optional bool ignore_empty = 8;
  365. }
  366. // SFixed64Rules describes the constraints applied to `sfixed64` values
  367. message SFixed64Rules {
  368. // Const specifies that this field must be exactly the specified value
  369. optional sfixed64 const = 1;
  370. // Lt specifies that this field must be less than the specified value,
  371. // exclusive
  372. optional sfixed64 lt = 2;
  373. // Lte specifies that this field must be less than or equal to the
  374. // specified value, inclusive
  375. optional sfixed64 lte = 3;
  376. // Gt specifies that this field must be greater than the specified value,
  377. // exclusive. If the value of Gt is larger than a specified Lt or Lte, the
  378. // range is reversed.
  379. optional sfixed64 gt = 4;
  380. // Gte specifies that this field must be greater than or equal to the
  381. // specified value, inclusive. If the value of Gte is larger than a
  382. // specified Lt or Lte, the range is reversed.
  383. optional sfixed64 gte = 5;
  384. // In specifies that this field must be equal to one of the specified
  385. // values
  386. repeated sfixed64 in = 6;
  387. // NotIn specifies that this field cannot be equal to one of the specified
  388. // values
  389. repeated sfixed64 not_in = 7;
  390. // IgnoreEmpty specifies that the validation rules of this field should be
  391. // evaluated only if the field is not empty
  392. optional bool ignore_empty = 8;
  393. }
  394. // BoolRules describes the constraints applied to `bool` values
  395. message BoolRules {
  396. // Const specifies that this field must be exactly the specified value
  397. optional bool const = 1;
  398. }
  399. // StringRules describe the constraints applied to `string` values
  400. message StringRules {
  401. // Const specifies that this field must be exactly the specified value
  402. optional string const = 1;
  403. // Len specifies that this field must be the specified number of
  404. // characters (Unicode code points). Note that the number of
  405. // characters may differ from the number of bytes in the string.
  406. optional uint64 len = 19;
  407. // MinLen specifies that this field must be the specified number of
  408. // characters (Unicode code points) at a minimum. Note that the number of
  409. // characters may differ from the number of bytes in the string.
  410. optional uint64 min_len = 2;
  411. // MaxLen specifies that this field must be the specified number of
  412. // characters (Unicode code points) at a maximum. Note that the number of
  413. // characters may differ from the number of bytes in the string.
  414. optional uint64 max_len = 3;
  415. // LenBytes specifies that this field must be the specified number of bytes
  416. // at a minimum
  417. optional uint64 len_bytes = 20;
  418. // MinBytes specifies that this field must be the specified number of bytes
  419. // at a minimum
  420. optional uint64 min_bytes = 4;
  421. // MaxBytes specifies that this field must be the specified number of bytes
  422. // at a maximum
  423. optional uint64 max_bytes = 5;
  424. // Pattern specifes that this field must match against the specified
  425. // regular expression (RE2 syntax). The included expression should elide
  426. // any delimiters.
  427. optional string pattern = 6;
  428. // Prefix specifies that this field must have the specified substring at
  429. // the beginning of the string.
  430. optional string prefix = 7;
  431. // Suffix specifies that this field must have the specified substring at
  432. // the end of the string.
  433. optional string suffix = 8;
  434. // Contains specifies that this field must have the specified substring
  435. // anywhere in the string.
  436. optional string contains = 9;
  437. // NotContains specifies that this field cannot have the specified substring
  438. // anywhere in the string.
  439. optional string not_contains = 23;
  440. // In specifies that this field must be equal to one of the specified
  441. // values
  442. repeated string in = 10;
  443. // NotIn specifies that this field cannot be equal to one of the specified
  444. // values
  445. repeated string not_in = 11;
  446. // WellKnown rules provide advanced constraints against common string
  447. // patterns
  448. oneof well_known {
  449. // Email specifies that the field must be a valid email address as
  450. // defined by RFC 5322
  451. bool email = 12;
  452. // Hostname specifies that the field must be a valid hostname as
  453. // defined by RFC 1034. This constraint does not support
  454. // internationalized domain names (IDNs).
  455. bool hostname = 13;
  456. // Ip specifies that the field must be a valid IP (v4 or v6) address.
  457. // Valid IPv6 addresses should not include surrounding square brackets.
  458. bool ip = 14;
  459. // Ipv4 specifies that the field must be a valid IPv4 address.
  460. bool ipv4 = 15;
  461. // Ipv6 specifies that the field must be a valid IPv6 address. Valid
  462. // IPv6 addresses should not include surrounding square brackets.
  463. bool ipv6 = 16;
  464. // Uri specifies that the field must be a valid, absolute URI as defined
  465. // by RFC 3986
  466. bool uri = 17;
  467. // UriRef specifies that the field must be a valid URI as defined by RFC
  468. // 3986 and may be relative or absolute.
  469. bool uri_ref = 18;
  470. // Address specifies that the field must be either a valid hostname as
  471. // defined by RFC 1034 (which does not support internationalized domain
  472. // names or IDNs), or it can be a valid IP (v4 or v6).
  473. bool address = 21;
  474. // Uuid specifies that the field must be a valid UUID as defined by
  475. // RFC 4122
  476. bool uuid = 22;
  477. // WellKnownRegex specifies a common well known pattern defined as a regex.
  478. KnownRegex well_known_regex = 24;
  479. }
  480. // This applies to regexes HTTP_HEADER_NAME and HTTP_HEADER_VALUE to enable
  481. // strict header validation.
  482. // By default, this is true, and HTTP header validations are RFC-compliant.
  483. // Setting to false will enable a looser validations that only disallows
  484. // \r\n\0 characters, which can be used to bypass header matching rules.
  485. optional bool strict = 25 [default = true];
  486. // IgnoreEmpty specifies that the validation rules of this field should be
  487. // evaluated only if the field is not empty
  488. optional bool ignore_empty = 26;
  489. }
  490. // WellKnownRegex contain some well-known patterns.
  491. enum KnownRegex {
  492. UNKNOWN = 0;
  493. // HTTP header name as defined by RFC 7230.
  494. HTTP_HEADER_NAME = 1;
  495. // HTTP header value as defined by RFC 7230.
  496. HTTP_HEADER_VALUE = 2;
  497. }
  498. // BytesRules describe the constraints applied to `bytes` values
  499. message BytesRules {
  500. // Const specifies that this field must be exactly the specified value
  501. optional bytes const = 1;
  502. // Len specifies that this field must be the specified number of bytes
  503. optional uint64 len = 13;
  504. // MinLen specifies that this field must be the specified number of bytes
  505. // at a minimum
  506. optional uint64 min_len = 2;
  507. // MaxLen specifies that this field must be the specified number of bytes
  508. // at a maximum
  509. optional uint64 max_len = 3;
  510. // Pattern specifes that this field must match against the specified
  511. // regular expression (RE2 syntax). The included expression should elide
  512. // any delimiters.
  513. optional string pattern = 4;
  514. // Prefix specifies that this field must have the specified bytes at the
  515. // beginning of the string.
  516. optional bytes prefix = 5;
  517. // Suffix specifies that this field must have the specified bytes at the
  518. // end of the string.
  519. optional bytes suffix = 6;
  520. // Contains specifies that this field must have the specified bytes
  521. // anywhere in the string.
  522. optional bytes contains = 7;
  523. // In specifies that this field must be equal to one of the specified
  524. // values
  525. repeated bytes in = 8;
  526. // NotIn specifies that this field cannot be equal to one of the specified
  527. // values
  528. repeated bytes not_in = 9;
  529. // WellKnown rules provide advanced constraints against common byte
  530. // patterns
  531. oneof well_known {
  532. // Ip specifies that the field must be a valid IP (v4 or v6) address in
  533. // byte format
  534. bool ip = 10;
  535. // Ipv4 specifies that the field must be a valid IPv4 address in byte
  536. // format
  537. bool ipv4 = 11;
  538. // Ipv6 specifies that the field must be a valid IPv6 address in byte
  539. // format
  540. bool ipv6 = 12;
  541. }
  542. // IgnoreEmpty specifies that the validation rules of this field should be
  543. // evaluated only if the field is not empty
  544. optional bool ignore_empty = 14;
  545. }
  546. // EnumRules describe the constraints applied to enum values
  547. message EnumRules {
  548. // Const specifies that this field must be exactly the specified value
  549. optional int32 const = 1;
  550. // DefinedOnly specifies that this field must be only one of the defined
  551. // values for this enum, failing on any undefined value.
  552. optional bool defined_only = 2;
  553. // In specifies that this field must be equal to one of the specified
  554. // values
  555. repeated int32 in = 3;
  556. // NotIn specifies that this field cannot be equal to one of the specified
  557. // values
  558. repeated int32 not_in = 4;
  559. }
  560. // MessageRules describe the constraints applied to embedded message values.
  561. // For message-type fields, validation is performed recursively.
  562. message MessageRules {
  563. // Skip specifies that the validation rules of this field should not be
  564. // evaluated
  565. optional bool skip = 1;
  566. // Required specifies that this field must be set
  567. optional bool required = 2;
  568. }
  569. // RepeatedRules describe the constraints applied to `repeated` values
  570. message RepeatedRules {
  571. // MinItems specifies that this field must have the specified number of
  572. // items at a minimum
  573. optional uint64 min_items = 1;
  574. // MaxItems specifies that this field must have the specified number of
  575. // items at a maximum
  576. optional uint64 max_items = 2;
  577. // Unique specifies that all elements in this field must be unique. This
  578. // contraint is only applicable to scalar and enum types (messages are not
  579. // supported).
  580. optional bool unique = 3;
  581. // Items specifies the contraints to be applied to each item in the field.
  582. // Repeated message fields will still execute validation against each item
  583. // unless skip is specified here.
  584. optional FieldRules items = 4;
  585. // IgnoreEmpty specifies that the validation rules of this field should be
  586. // evaluated only if the field is not empty
  587. optional bool ignore_empty = 5;
  588. }
  589. // MapRules describe the constraints applied to `map` values
  590. message MapRules {
  591. // MinPairs specifies that this field must have the specified number of
  592. // KVs at a minimum
  593. optional uint64 min_pairs = 1;
  594. // MaxPairs specifies that this field must have the specified number of
  595. // KVs at a maximum
  596. optional uint64 max_pairs = 2;
  597. // NoSparse specifies values in this field cannot be unset. This only
  598. // applies to map's with message value types.
  599. optional bool no_sparse = 3;
  600. // Keys specifies the constraints to be applied to each key in the field.
  601. optional FieldRules keys = 4;
  602. // Values specifies the constraints to be applied to the value of each key
  603. // in the field. Message values will still have their validations evaluated
  604. // unless skip is specified here.
  605. optional FieldRules values = 5;
  606. // IgnoreEmpty specifies that the validation rules of this field should be
  607. // evaluated only if the field is not empty
  608. optional bool ignore_empty = 6;
  609. }
  610. // AnyRules describe constraints applied exclusively to the
  611. // `google.protobuf.Any` well-known type
  612. message AnyRules {
  613. // Required specifies that this field must be set
  614. optional bool required = 1;
  615. // In specifies that this field's `type_url` must be equal to one of the
  616. // specified values.
  617. repeated string in = 2;
  618. // NotIn specifies that this field's `type_url` must not be equal to any of
  619. // the specified values.
  620. repeated string not_in = 3;
  621. }
  622. // DurationRules describe the constraints applied exclusively to the
  623. // `google.protobuf.Duration` well-known type
  624. message DurationRules {
  625. // Required specifies that this field must be set
  626. optional bool required = 1;
  627. // Const specifies that this field must be exactly the specified value
  628. optional google.protobuf.Duration const = 2;
  629. // Lt specifies that this field must be less than the specified value,
  630. // exclusive
  631. optional google.protobuf.Duration lt = 3;
  632. // Lt specifies that this field must be less than the specified value,
  633. // inclusive
  634. optional google.protobuf.Duration lte = 4;
  635. // Gt specifies that this field must be greater than the specified value,
  636. // exclusive
  637. optional google.protobuf.Duration gt = 5;
  638. // Gte specifies that this field must be greater than the specified value,
  639. // inclusive
  640. optional google.protobuf.Duration gte = 6;
  641. // In specifies that this field must be equal to one of the specified
  642. // values
  643. repeated google.protobuf.Duration in = 7;
  644. // NotIn specifies that this field cannot be equal to one of the specified
  645. // values
  646. repeated google.protobuf.Duration not_in = 8;
  647. }
  648. // TimestampRules describe the constraints applied exclusively to the
  649. // `google.protobuf.Timestamp` well-known type
  650. message TimestampRules {
  651. // Required specifies that this field must be set
  652. optional bool required = 1;
  653. // Const specifies that this field must be exactly the specified value
  654. optional google.protobuf.Timestamp const = 2;
  655. // Lt specifies that this field must be less than the specified value,
  656. // exclusive
  657. optional google.protobuf.Timestamp lt = 3;
  658. // Lte specifies that this field must be less than the specified value,
  659. // inclusive
  660. optional google.protobuf.Timestamp lte = 4;
  661. // Gt specifies that this field must be greater than the specified value,
  662. // exclusive
  663. optional google.protobuf.Timestamp gt = 5;
  664. // Gte specifies that this field must be greater than the specified value,
  665. // inclusive
  666. optional google.protobuf.Timestamp gte = 6;
  667. // LtNow specifies that this must be less than the current time. LtNow
  668. // can only be used with the Within rule.
  669. optional bool lt_now = 7;
  670. // GtNow specifies that this must be greater than the current time. GtNow
  671. // can only be used with the Within rule.
  672. optional bool gt_now = 8;
  673. // Within specifies that this field must be within this duration of the
  674. // current time. This constraint can be used alone or with the LtNow and
  675. // GtNow rules.
  676. optional google.protobuf.Duration within = 9;
  677. }

层级目录如下
image.png

在proto文件目录下执行以下命令生成validate文件

protoc -I . --go_out=plugins=grpc:. --validate_out="lang=go:." greeter.proto

server端代码

  1. type Greeter struct{}
  2. func (g Greeter) SayHello(ctx context.Context, person *bark.Person) (*bark.Person, error) {
  3. return &bark.Person{
  4. Id: 32,
  5. }, nil
  6. }
  7. type Validator interface {
  8. Validate() error
  9. }
  10. func main() {
  11. /** 测试validate的调用
  12. p := new(bark.Person)
  13. p.Id = 1000
  14. err := p.Validate()
  15. if err != nil {
  16. panic(err)
  17. }
  18. */
  19. interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
  20. handler grpc.UnaryHandler) (resp interface{}, err error) {
  21. if r, ok := req.(Validator); ok {
  22. if err := r.Validate(); err != nil {
  23. return nil, status.Error(codes.InvalidArgument, err.Error())
  24. }
  25. }
  26. return handler(ctx, req)
  27. }
  28. opt := grpc.UnaryInterceptor(interceptor)
  29. s := grpc.NewServer(opt)
  30. bark.RegisterBarkServer(s, &Greeter{})
  31. lis, err := net.Listen("tcp", ":8080")
  32. if err != nil {
  33. panic(err)
  34. }
  35. s.Serve(lis)
  36. }

client端代

  1. func main() {
  2. conn, err := grpc.Dial(":8080", grpc.WithInsecure())
  3. if err != nil {
  4. panic(err)
  5. }
  6. defer conn.Close()
  7. c := bark.NewBarkClient(conn)
  8. res, err := c.SayHello(context.Background(), &bark.Person{
  9. Id: 1000,
  10. Email: "741883227@qq.com",
  11. Mobile: "18978057553",
  12. })
  13. if err != nil {
  14. fmt.Println("err", err)
  15. return
  16. }
  17. fmt.Println("res", res)
  18. }

启动server端然后使用client端发送请求, 如果数据没有通过校验, 则会在服务端的拦截器里拦截并且返回

image.png
更多规则请参考文档
bingo!