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

This project is currently in alpha. The API should be considered unstable and likely to change

PGV is a protoc plugin to generate polyglot message validators. While protocol buffers effectively guarantee the types of structured data, they cannot enforce semantic rules for values. This plugin adds support to protoc-generated code to validate such constraints.

Developers import the PGV extension and annotate the messages and fields in their proto files with constraint rules:

syntax = "proto3";

package examplepb;

import "validate/validate.proto";

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: "^[^[0-9]A-Za-z]+( [^[0-9]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 }];
}
}

Executing protoc with PGV and the target language's default plugin will create Validate methods on the generated types:

p := new(Person)

err := p.Validate() // err: Id must be greater than 999
p.Id = 1000 err = p.Validate() // err: Email must be a valid email address
p.Email = "example@lyft.com" err = p.Validate() // err: Name must match pattern '^[^\d\s]+( [^\d\s]+)*$'
p.Name = "Protocol Buffer" err = p.Validate() // err: Home is required
p.Home = &Location{37.7, 999} err = p.Validate() // err: Home.Lng must be within [-180, 180]
p.Home.Lng = -122.4 err = p.Validate() // err: nil

This project is currently in alpha. The API should be considered unstable and likely to change

PGV is a protoc plugin to generate polyglot message validators. While protocol buffers effectively guarantee the types of structured data, they cannot enforce semantic rules for values. This plugin adds support to protoc-generated code to validate such constraints.

Developers import the PGV extension and annotate the messages and fields in their proto files with constraint rules:

syntax = "proto3";

package examplepb;

import "validate/validate.proto";

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: "^[^[0-9]A-Za-z]+( [^[0-9]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 }];
}
}

Executing protoc with PGV and the target language's default plugin will create Validate methods on the generated types:

p := new(Person)

err := p.Validate() // err: Id must be greater than 999
p.Id = 1000 err = p.Validate() // err: Email must be a valid email address
p.Email = "example@lyft.com" err = p.Validate() // err: Name must match pattern '^[^\d\s]+( [^\d\s]+)*$'
p.Name = "Protocol Buffer" err = p.Validate() // err: Home is required
p.Home = &Location{37.7, 999} err = p.Validate() // err: Home.Lng must be within [-180, 180]
p.Home.Lng = -122.4 err = p.Validate() // err: nil

protoc-gen-validate (PGV)的更多相关文章

  1. 解析大型.NET ERP系统数据访问 对象关系映射框架LLBL Gen Pro

    LLBL Gen Pro是一个为.NET开发人员设计的的对象关系映射(ORM)框架,与NHibernate,Entity Framework等框架一样,通过实体与数据表的映射,实现关系数据库持久化. ...

  2. Enterprise Solution 3.1 企业应用开发框架 .NET ERP/CRM/MIS 开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    行业:基于数据库的制造行业管理软件,包含ERP.MRP.CRM.MIS.MES等企业管理软件 数据库平台:SQL Server 2005或以上 系统架构:C/S 开发技术 序号 领域 技术 1 数据库 ...

  3. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  4. LLBL Gen Pro 5.0 企业应用开发入门

    Solutions Design 公司于2016年5月发布了LLBL Gen Pro 5.0,这个新版本的发布出乎于我的意料.我的猜想是从4.2升级到4.5,再升级5.x版本,主版本号的变更会给原有客 ...

  5. jQuery Validate 表单验证 — 用户注册简单应用

    相信很多coder在表单验证这块都是自己写验证规则的,今天我们用jQuery Validate这款前端验证利器来写一个简单的应用. 可以先把我写的这个小demo运行试下,先睹为快.猛戳链接--> ...

  6. jquery validate表单验证插件-推荐

    1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家.     1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素  3.鼠标离开后的正确.错误提示及鼠标移入时的帮 ...

  7. jq.validate隐藏元素忽略验证

    jq.validate隐藏元素忽略验证 现在有这样一个需求,当触发某类事件时候,需要在页面中显示input框,但是当不需要加载页面中的元素时候,进行隐藏.在这个需求的前提下,程序中对于input中的输 ...

  8. jq.validate 自定义验证两个日期

    jq.validate 自定义验证两个日期 首先定义有一个表单,date1和date2是属于表单的元素,若date1大于date2,返回false:若date1<date2,返回true.使用j ...

  9. 修改 jquery.validate.js 支持非form标签

    尝试使用markdown来写一篇blog,啦啦啦 源代码传送门:github 在特殊情况下我们使用jquery.validate.js对用户输入的内容做验证的时候,表单并不是一定包含在form之中,有 ...

随机推荐

  1. 服务器挂掉导致需要对k8s新增节点

    [导读]今日是周六,本想午休起来之后写篇有关kubernetes service的文章,没想到两台虚拟机接连挂掉,所以又重新创建了两台虚拟机.新建时又一直黑屏,所以只能克隆. 由于虚拟机是新建的,所以 ...

  2. 在C#中使用OpenCV(使用GOCW)

    在C#中使用OpenCV(使用GOCW) 1.什么是GOCW       为了解决在Csharp下编写OpenCV程序的问题,我做过比较深入的研究,并且实现了高效可用的方法GreenOpenCshar ...

  3. Abp(net core)+easyui+efcore实现仓储管理系统——出库管理之八(五十七)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统--ABP总体介绍(一) abp(net core)+ ...

  4. 【mysql】- Expalin篇

    简介 id:在一个大的查询语句中每个 SELECT 关键字都对应一个唯一的id 与查询优化器有关,假如被优化过,那么可能是上下两个的id都是一样的 select_type:SELECT 关键字对应的那 ...

  5. 如果对象为空,java函数String.valueOf(Object obj)返回"null"字符串

    源码如下: public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toStr ...

  6. Spring项目出现--Error:java: Compilation failed: internal java compiler error

    错误现象 使用Idea导入新项目或升级idea或新建项目时会出现以下异常信息: Error:java: Compilation failed: internal java compiler error ...

  7. Logstash-input-jdbc同步mysql数据到ES - sql_last_value

    问题:使用 Logstash-input-jdbc同步mysql到ES, 当中使用了 sql_last_value ,如何重新同步? 答: logstash把上一次同步的最后一条记录id写到 c:\用 ...

  8. MySQL 中的WAL机制

    本文主要概括的是 WAL 机制涉及的三种日志,具体与其他组件的联系.执行可查看 一条 sql 的执行过程详解 .MySQL中的事务原理和锁机制 . 是什么 WAL,全称是Write-Ahead Log ...

  9. 最实用的visual studio插件,值得收藏!

    1.ReSharper(VS必装,代码重构.代码修正功能) ReSharper 是一个JetBrains公司出品的著名的代码生成工具.其能帮助Microsoft Visual Studio成为一个更佳 ...

  10. String、StringBuffer 和 StringBuilder 的区别

    面试简答 区别: 1) String 长度大小不可变 2) StringBuffer 和 StringBuilder 长度可变 3) StringBuffer 线程安全 StringBuilder 线 ...