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
  • 相关阅读:
    Android 最简单的基于FFmpeg的移动端例子:Android HelloWorld
    实现简易的android 直播技术
    Android Service和Thread的关系
    Android Fragment 你应该知道的一切
    SQLyog普通版与SQLyog企业版对比分析
    Sqoop异常解决ERROR tool.ImportTool: Encountered IOException running import job: java.io.IOException: No columns to generate for ClassWriter问题
    SQLyog软件里无法插入中文(即由默认的latin1改成UTF8编码格式)
    Sqoop-1.4.6工具import和export使用详解(官网)
    Sqoop Export原理和详细流程讲解
    Sqoop Import原理和详细流程讲解
  • 原文地址:https://www.cnblogs.com/rsapaper/p/13718698.html
Copyright © 2011-2022 走看看