zoukankan      html  css  js  c++  java
  • protoc-gen-validate (PGV)

    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 '^[^ds]+( [^ds]+)*$'
    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 '^[^ds]+( [^ds]+)*$'
    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
  • 相关阅读:
    epoll精髓【epoll】
    linux下调试core的命令,察看堆栈状态命令 摘录(http://blog.csdn.net/yearn520/article/details/6663265)
    使用epoll 在 linux 上开发高性能应用服务器【epoll】
    linux下epoll如何实现高效处理百万句柄的[转]【epoll】
    log4cplus入门
    非阻塞式服务器和客户端程序(TCP)【简单的原理例子】
    Linux有用的命令记录
    在Linux上的使用开源C++日志库log4cplus
    静态库和动态库的区别
    localtime多线下不安全,localtime_r线程安全
  • 原文地址:https://www.cnblogs.com/rsapaper/p/13718698.html
Copyright © 2011-2022 走看看