validate_example.proto 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // Copyright 2025 Kuban Technologies
  2. //
  3. // 验证系统使用示例
  4. // 展示如何使用 kuban.api.validate 验证框架
  5. syntax = "proto3";
  6. package kuban.api.example;
  7. import "kuban/api/validate/validate.proto";
  8. // import "kuban/api/validate/business_rules.proto"; // 未使用
  9. // import "kuban/api/validate/rule_groups.proto"; // 未使用
  10. option go_package = "git.ikuban.com/server/kubanapis/kuban/api/example;example";
  11. // ============================================================================
  12. // 示例 1: 基本字段验证
  13. // ============================================================================
  14. message BasicValidationExample {
  15. // 必填字符串,长度 2-50
  16. string name = 1 [(kuban.api.validate.field) = {
  17. required: true,
  18. string: {
  19. min_len: 2,
  20. max_len: 50
  21. }
  22. }];
  23. // 必填邮箱
  24. string email = 2 [(kuban.api.validate.field) = {
  25. required: true,
  26. string: {
  27. email: true
  28. }
  29. }];
  30. // 年龄范围 0-150
  31. int32 age = 3 [(kuban.api.validate.field) = {
  32. int32: {
  33. gte: 0,
  34. lte: 150
  35. }
  36. }];
  37. // 可选字段,设置时必须符合 URL 格式
  38. optional string website = 4 [(kuban.api.validate.field) = {
  39. string: {
  40. uri: true
  41. }
  42. }];
  43. }
  44. // ============================================================================
  45. // 示例 2: 中文业务验证
  46. // ============================================================================
  47. message ChineseBusinessExample {
  48. // 中国手机号验证(简化方式)
  49. string mobile = 1 [(kuban.api.validate.field) = {
  50. required: true,
  51. string: {
  52. chinese_mobile: true // 使用内置的中国手机号验证
  53. }
  54. }];
  55. // 中国手机号验证(带自定义错误消息)
  56. string mobile_advanced = 2 [(kuban.api.validate.field) = {
  57. required: true,
  58. error_message: "请输入有效的中国大陆手机号",
  59. string: {
  60. chinese_mobile: true
  61. }
  62. }];
  63. // 中国身份证验证
  64. string id_card = 3 [(kuban.api.validate.field) = {
  65. required: true,
  66. error_message: "请输入有效的18位身份证号",
  67. string: {
  68. chinese_id_card: true
  69. }
  70. }];
  71. // 中文姓名验证
  72. string chinese_name = 4 [(kuban.api.validate.field) = {
  73. required: true,
  74. error_message: "请输入2-20个字符的中文姓名",
  75. string: {
  76. chinese_name: true,
  77. min_len: 2,
  78. max_len: 20
  79. }
  80. }];
  81. // 统一社会信用代码(使用 CEL 或正则表达式验证)
  82. string uscc = 5 [(kuban.api.validate.field) = {
  83. error_message: "请输入有效的企业统一社会信用代码(18位)",
  84. string: {
  85. pattern: "^[0-9A-HJ-NPQRTUWXY]{2}\\d{6}[0-9A-HJ-NPQRTUWXY]{10}$",
  86. len: 18
  87. }
  88. }];
  89. // 银行卡号(使用正则表达式和长度验证)
  90. string bank_card = 6 [(kuban.api.validate.field) = {
  91. error_message: "请输入有效的银行卡号(16-19位)",
  92. string: {
  93. pattern: "^\\d{16,19}$",
  94. min_len: 16,
  95. max_len: 19
  96. }
  97. }];
  98. // 中国邮政编码
  99. string postcode = 7 [(kuban.api.validate.field) = {
  100. error_message: "请输入有效的6位邮政编码",
  101. string: {
  102. chinese_postcode: true
  103. }
  104. }];
  105. }
  106. // ============================================================================
  107. // 示例 3: 使用 CEL 自定义验证
  108. // ============================================================================
  109. message CustomCELExample {
  110. string username = 1 [(kuban.api.validate.field) = {
  111. required: true,
  112. cel: [
  113. {
  114. id: "username.format",
  115. message: "用户名只能包含字母、数字和下划线",
  116. expression: "this.matches('^[a-zA-Z0-9_]+$')"
  117. },
  118. {
  119. id: "username.no_admin",
  120. message: "用户名不能包含 'admin'",
  121. expression: "!this.contains('admin')"
  122. }
  123. ],
  124. string: {
  125. min_len: 3,
  126. max_len: 20
  127. }
  128. }];
  129. int32 score = 2 [(kuban.api.validate.field) = {
  130. cel: [
  131. {
  132. id: "score.range",
  133. message: "分数必须在 0-100 之间",
  134. message_en: "Score must be between 0 and 100",
  135. expression: "this >= 0 && this <= 100"
  136. }
  137. ]
  138. }];
  139. // 跨字段验证
  140. string password = 3;
  141. string password_confirm = 4 [(kuban.api.validate.field) = {
  142. cel: [
  143. {
  144. id: "password.match",
  145. message: "两次输入的密码不一致",
  146. expression: "this == this.password" // 引用同一消息的其他字段
  147. }
  148. ]
  149. }];
  150. }
  151. // ============================================================================
  152. // 示例 4: 分组验证
  153. // ============================================================================
  154. message GroupValidationExample {
  155. // 用户名在创建和更新时都需要验证
  156. string username = 1 [(kuban.api.validate.field) = {
  157. required: true,
  158. groups: ["create", "update"],
  159. string: {
  160. min_len: 3,
  161. max_len: 20
  162. }
  163. }];
  164. // 密码仅在创建和修改密码时需要验证
  165. string password = 2 [(kuban.api.validate.field) = {
  166. required: true,
  167. groups: ["create", "change_password"],
  168. string: {
  169. min_len: 8,
  170. max_len: 32
  171. }
  172. }];
  173. // 手机号在实名认证时必填
  174. string mobile = 3 [(kuban.api.validate.field) = {
  175. groups: ["real_name_auth"],
  176. required: true,
  177. string: {
  178. chinese_mobile: true
  179. }
  180. }];
  181. // 可选字段,但在严格模式下必填
  182. optional string nickname = 4 [(kuban.api.validate.field) = {
  183. groups: ["strict"],
  184. required: true,
  185. string: {
  186. min_len: 2,
  187. max_len: 30
  188. }
  189. }];
  190. }
  191. // ============================================================================
  192. // 示例 5: 复杂类型验证
  193. // ============================================================================
  194. message ComplexTypeExample {
  195. // 重复字段验证
  196. repeated string tags = 1 [(kuban.api.validate.field) = {
  197. repeated: {
  198. min_items: 1,
  199. max_items: 10,
  200. unique: true,
  201. items: {
  202. string: {
  203. min_len: 2,
  204. max_len: 20
  205. }
  206. }
  207. }
  208. }];
  209. // Map 验证
  210. map<string, int32> scores = 2 [(kuban.api.validate.field) = {
  211. map: {
  212. min_pairs: 1,
  213. max_pairs: 100,
  214. keys: {
  215. string: {
  216. min_len: 1,
  217. max_len: 50
  218. }
  219. },
  220. values: {
  221. int32: {
  222. gte: 0,
  223. lte: 100
  224. }
  225. }
  226. }
  227. }];
  228. // 嵌套消息验证
  229. Address address = 3 [(kuban.api.validate.field) = {
  230. required: true
  231. }];
  232. }
  233. message Address {
  234. string province = 1 [(kuban.api.validate.field) = {
  235. required: true,
  236. string: {
  237. min_len: 2,
  238. max_len: 20
  239. }
  240. }];
  241. string city = 2 [(kuban.api.validate.field) = {
  242. required: true,
  243. string: {
  244. min_len: 2,
  245. max_len: 20
  246. }
  247. }];
  248. string detail = 3 [(kuban.api.validate.field) = {
  249. required: true,
  250. string: {
  251. min_len: 5,
  252. max_len: 200
  253. }
  254. }];
  255. string postcode = 4 [(kuban.api.validate.field) = {
  256. string: {
  257. chinese_postcode: true
  258. }
  259. }];
  260. }
  261. // ============================================================================
  262. // 示例 6: 消息级别验证
  263. // ============================================================================
  264. message MessageLevelExample {
  265. option (kuban.api.validate.message) = {
  266. cel: [
  267. {
  268. id: "date_range.valid",
  269. message: "结束日期必须大于开始日期",
  270. expression: "this.end_date > this.start_date"
  271. },
  272. {
  273. id: "required_fields",
  274. message: "姓名和手机号至少需要填写一个",
  275. expression: "has(this.name) || has(this.mobile)"
  276. }
  277. ],
  278. groups: ["create", "update"]
  279. };
  280. optional string name = 1;
  281. optional string mobile = 2;
  282. int64 start_date = 3;
  283. int64 end_date = 4;
  284. }
  285. // ============================================================================
  286. // 示例 7: Oneof 验证
  287. // ============================================================================
  288. message OneofExample {
  289. // 联系方式:邮箱和手机号必须选择一个
  290. oneof contact {
  291. option (kuban.api.validate.oneof).required = true;
  292. string email = 1 [(kuban.api.validate.field) = {
  293. string: { email: true }
  294. }];
  295. string mobile = 2 [(kuban.api.validate.field) = {
  296. string: { chinese_mobile: true }
  297. }];
  298. }
  299. }
  300. // ============================================================================
  301. // 示例 8: 忽略策略
  302. // ============================================================================
  303. message IgnoreRuleExample {
  304. // 仅在值不为空时验证
  305. optional string optional_url = 1 [(kuban.api.validate.field) = {
  306. ignore: IGNORE_IF_ZERO,
  307. string: {
  308. uri: true
  309. }
  310. }];
  311. // 始终验证(即使未设置)
  312. optional string required_email = 2 [(kuban.api.validate.field) = {
  313. ignore: IGNORE_NEVER,
  314. required: true,
  315. string: {
  316. email: true
  317. }
  318. }];
  319. // 暂时忽略验证(用于开发阶段)
  320. string temp_field = 3 [(kuban.api.validate.field) = {
  321. ignore: IGNORE_ALWAYS,
  322. string: {
  323. min_len: 10 // 这个规则不会生效
  324. }
  325. }];
  326. }
  327. // ============================================================================
  328. // 示例 9: 实际业务场景 - 用户注册
  329. // ============================================================================
  330. message UserRegistrationRequest {
  331. option (kuban.api.validate.message) = {
  332. groups: ["register"]
  333. };
  334. // 用户名
  335. string username = 1 [(kuban.api.validate.field) = {
  336. required: true,
  337. groups: ["register"],
  338. string: {
  339. min_len: 3,
  340. max_len: 20,
  341. pattern: "^[a-zA-Z0-9_]+$"
  342. },
  343. cel: [
  344. {
  345. id: "username.reserved",
  346. message: "该用户名为系统保留,不可使用",
  347. expression: "!['admin', 'root', 'system'].contains(this.toLowerCase())"
  348. }
  349. ]
  350. }];
  351. // 密码
  352. string password = 2 [(kuban.api.validate.field) = {
  353. required: true,
  354. groups: ["register"],
  355. string: {
  356. min_len: 8,
  357. max_len: 32
  358. },
  359. cel: [
  360. {
  361. id: "password.complexity",
  362. message: "密码必须包含大小写字母、数字和特殊字符",
  363. expression: "this.matches('(?=.*[a-z])(?=.*[A-Z])(?=.*\\\\d)(?=.*[@$!%*?&])[A-Za-z\\\\d@$!%*?&]+')"
  364. }
  365. ]
  366. }];
  367. // 手机号
  368. string mobile = 3 [(kuban.api.validate.field) = {
  369. required: true,
  370. groups: ["register"],
  371. string: {
  372. chinese_mobile: true
  373. }
  374. }];
  375. // 验证码
  376. string verification_code = 4 [(kuban.api.validate.field) = {
  377. required: true,
  378. groups: ["register"],
  379. string: {
  380. len: 6,
  381. numeric: true
  382. }
  383. }];
  384. // 邮箱(可选)
  385. optional string email = 5 [(kuban.api.validate.field) = {
  386. groups: ["register"],
  387. ignore: IGNORE_IF_ZERO,
  388. string: {
  389. email: true
  390. }
  391. }];
  392. // 同意服务条款
  393. bool agree_terms = 6 [(kuban.api.validate.field) = {
  394. required: true,
  395. groups: ["register"],
  396. bool: {
  397. const: true
  398. },
  399. cel: [
  400. {
  401. id: "agree_terms.must_true",
  402. message: "必须同意服务条款才能注册",
  403. expression: "this == true"
  404. }
  405. ]
  406. }];
  407. }
  408. // ============================================================================
  409. // 示例 10: 实际业务场景 - 企业认证
  410. // ============================================================================
  411. message EnterpriseAuthRequest {
  412. option (kuban.api.validate.message) = {
  413. groups: ["enterprise_auth"],
  414. cel: [
  415. {
  416. id: "complete_info",
  417. message: "企业认证信息不完整",
  418. expression: "has(this.company_name) && has(this.uscc) && has(this.legal_person)"
  419. }
  420. ]
  421. };
  422. // 企业名称
  423. string company_name = 1 [(kuban.api.validate.field) = {
  424. required: true,
  425. groups: ["enterprise_auth"],
  426. string: {
  427. min_len: 2,
  428. max_len: 100
  429. }
  430. }];
  431. // 统一社会信用代码
  432. string uscc = 2 [(kuban.api.validate.field) = {
  433. required: true,
  434. groups: ["enterprise_auth"],
  435. error_message: "请输入有效的企业统一社会信用代码(18位)",
  436. string: {
  437. pattern: "^[0-9A-HJ-NPQRTUWXY]{2}\\d{6}[0-9A-HJ-NPQRTUWXY]{10}$",
  438. len: 18
  439. }
  440. }];
  441. // 法人代表姓名
  442. string legal_person = 3 [(kuban.api.validate.field) = {
  443. required: true,
  444. groups: ["enterprise_auth"],
  445. error_message: "请输入2-20个字符的中文姓名",
  446. string: {
  447. chinese_name: true,
  448. min_len: 2,
  449. max_len: 20
  450. }
  451. }];
  452. // 法人身份证号
  453. string legal_person_id_card = 4 [(kuban.api.validate.field) = {
  454. required: true,
  455. groups: ["enterprise_auth"],
  456. error_message: "请输入有效的18位身份证号",
  457. string: {
  458. chinese_id_card: true
  459. }
  460. }];
  461. // 企业联系电话
  462. string contact_phone = 5 [(kuban.api.validate.field) = {
  463. required: true,
  464. groups: ["enterprise_auth"],
  465. string: {
  466. chinese_mobile: true
  467. }
  468. }];
  469. // 营业执照附件
  470. bytes business_license_image = 6 [(kuban.api.validate.field) = {
  471. required: true,
  472. groups: ["enterprise_auth"],
  473. bytes: {
  474. min_len: 1024, // 至少 1KB
  475. max_len: 5242880 // 最大 5MB
  476. }
  477. }];
  478. }