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
  • 相关阅读:
    Win7怎么进入安全模式 三种轻松进入Win7安全模式方法
    Eclipse中新建applet 错误
    经典语录
    轻松一刻
    WIN XP蓝屏代码大全
    大话设计模式简单总结
    生命是闹着玩儿,事事显出如此 (转)
    Educational Codeforces Round 42 (Rated for Div. 2) A
    2018年东北农业大学春季校赛----不完整题解
    POJ
  • 原文地址:https://www.cnblogs.com/rsapaper/p/13718698.html
Copyright © 2011-2022 走看看