Latest Threat ResearchGlassWorm Loader Hits Open VSX via Developer Account Compromise.Details
Socket
Book a DemoInstallSign in
Socket

trpc.group/trpc-go/trpc-filter/validation

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

trpc.group/trpc-go/trpc-filter/validation

Go Modules
Version
v1.0.1
Version published
Created
Source

Validation

Parameter automatic validation plugin. Supports custom error codes.

Usage

Import this plugin in your code.

import (
    _ "trpc.group/trpc-go/trpc-filter/validation"
)

Configure the trpc-go config file. In the server's filter configuration, enable the validation interceptor to automatically validate the req request parameters.

server:
 ...
 filter:
  ...
  - validation

Configure the trpc-go config file. In the client's filter configuration, enable the validation interceptor to automatically validate the rsp response parameters.

client:
 ...
 filter:
  ...
  - validation

Enable local interception log recording (optional).

plugins:                     
  auth:
    validation:
      enable_error_log: true

Customize error codes (optional).

When req validation fails in the server filter, the default error code errs.RetServerValidateFail 51 will be used. When rsp validation fails in the client filter, the default error code errs.RetClientValidateFail 151 will be used.

You can customize the error codes with the following configuration:

plugins:
  auth:
    validation:
      enable_error_log: true
      server_validate_err_code: 100101
      client_validate_err_code: 100102

Writing Proto Protocol Files

syntax = "proto3";

package trpc.test.helloworld;

import "validate/validate.proto";

option go_package="trpc.group/trpcprotocol/test/helloworld";

/* SearchRequest represents a search query, with pagination options to
 * indicate which results to include in the response.
 * Hint use https://regex-golang.appspot.com/assets/html/index.html for
 *  Regex validation in Go
 */

message SearchRequest {
  string query = 1 [(validate.rules).string = {
    pattern:   "([A-Za-z]+) ([A-Za-z]+)*$",
    max_bytes: 50,
  }];
  string email_1= 2 [(validate.rules).string.alphabets = true];
  string email_2= 3 [(validate.rules).string.alphanums = true];
  string email_3= 4 [(validate.rules).string.lowercase = true];
}

FAQs

Package last updated on 20 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts