Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversun9 committed Sep 19, 2023
1 parent 371ea23 commit 850813b
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 44 deletions.
10 changes: 9 additions & 1 deletion examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ proto_library(
"option_bool.proto",
"option_bytes_ban_values.proto",
"option_bytes_contains.proto",
"option_bytes_equal.proto",
"option_bytes_len.proto",
"option_bytes_pattern.proto",
"option_bytes_prefix_suffix.proto",
Expand All @@ -82,7 +83,14 @@ proto_library(
"option_number_range.proto",
"option_oneof.proto",
"option_repeated.proto",
"option_string.proto",
"option_string_allow_values.proto",
"option_string_ban_values.proto",
"option_string_contains.proto",
"option_string_equal.proto",
"option_string_is_http_header.proto",
"option_string_len.proto",
"option_string_match_pattern.proto",
"option_string_prefix_suffix.proto",
"option_timestamp_range.proto",
"option_timestamp_relative_to_now.proto",
],
Expand Down
30 changes: 30 additions & 0 deletions examples/option_bytes_equal.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

import "buf/validate/validate.proto";

service AccountService {
// DeleteAccount deletes an account.
rpc DeleteAccount(DeleteAccountRequest) returns (DeleteAccountResponse);
}

message DeleteAccountRequest {
// This validates that field `acknowledgement` must be equal to the bytes for
// "I am aware.".
bytes acknowledgement = 1 [(buf.validate.field).bytes.const = "I am aware."];
}

message DeleteAccountResponse {}
3 changes: 3 additions & 0 deletions examples/option_bytes_pattern.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ message NetworkAddress {
// `ipv6` is built in and validates the bytes are a valid ipv6 address in
// binary form, having exactly 16 bytes.
bytes ip_address = 2 [(buf.validate.field).bytes.ipv6 = true];
// Similarly you can validate that it's ipv4 or either version with:
// bytes ip_address = 2 [(buf.validate.field).bytes.ipv4 = true];
// bytes ip_address = 2 [(buf.validate.field).bytes.ip = true];
}
43 changes: 0 additions & 43 deletions examples/option_string.proto

This file was deleted.

28 changes: 28 additions & 0 deletions examples/option_string_allow_values.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

import "buf/validate/validate.proto";

message PartingWords {
string content = 1 [(buf.validate.field).string = {
// `in` validates that a string field is one of the values specified.
// In this case, it validates that content is either "Goodbye.' or 'So long.'.
in: [
"Goodbye.",
"So long."
]
}];
}
35 changes: 35 additions & 0 deletions examples/option_string_ban_values.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

import "buf/validate/validate.proto";

service TeamService {
rpc UpdateTeamName(UpdateTeamNameRequest) returns (UpdateTeamNameResponse);
}

message UpdateTeamNameRequest {
string name = 1 [(buf.validate.field).string = {
// `not_in` validates that a string field is not any value from the list
// defined. In this case, it validates that you cannot set a team name
// to "The winners" or "winners".
not_in: [
"The winners",
"winners"
]
}];
}

message UpdateTeamNameResponse {}
29 changes: 29 additions & 0 deletions examples/option_string_contains.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

import "buf/validate/validate.proto";

// GoodNews is good news in the stock market.
message GoodNews {
string content = 1 [(buf.validate.field).string = {
// `contains` validates that a string field contains this string.
// In this example, it validates that content contains `up`.
contains: "up",
// `not_contains` validates that a string field doesn't contain this string.
// In this example, it validates that content doesn't contain `down`.
not_contains: "down"
}];
}
29 changes: 29 additions & 0 deletions examples/option_string_equal.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

import "buf/validate/validate.proto";

service TermsService {
rpc AcceptTerms(AcceptTermsRequest) returns (AcceptTermsResponse);
}

message AcceptTermsRequest {
// `const` specifies that a string field must be the same as this string.
// In this case, it validates that content must be "I agree to the terms and conditions.".
string content = 1 [(buf.validate.field).string.const = "I agree to the terms and conditions."];
}

message AcceptTermsResponse {}
41 changes: 41 additions & 0 deletions examples/option_string_is_http_header.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

import "buf/validate/validate.proto";

message HttpHeader {
string name = 1 [(buf.validate.field).string = {
// This validates that name is a valid http header name.
well_known_regex: KNOWN_REGEX_HTTP_HEADER_NAME,
}];
string value = 2 [(buf.validate.field).string = {
// This validates that value is a valid http header value.
well_known_regex: KNOWN_REGEX_HTTP_HEADER_VALUE,
// `strict` applies to regexes `HTTP_HEADER_NAME` and `HTTP_HEADER_VALUE` to
// enable strict header validation. By default this value is true and
// validations are [RFC-compliant](https://tools.ietf.org/html/rfc7230#section-3).
// When `strict` is false, validation only disallows `\r\n\0` characters.
strict: false
}];
}

// `well_known_regex` contains some well-known patterns.
//
// | Name | Number | Description |
// |-------------------------------|--------|-------------------------------------------|
// | KNOWN_REGEX_UNSPECIFIED | 0 | |
// | KNOWN_REGEX_HTTP_HEADER_NAME | 1 | HTTP header name as defined by [RFC 7230](https://tools.ietf.org/html/rfc7230#section-3.2) |
// | KNOWN_REGEX_HTTP_HEADER_VALUE | 2 | HTTP header value as defined by [RFC 7230](https://tools.ietf.org/html/rfc7230#section-3.2.4) |
41 changes: 41 additions & 0 deletions examples/option_string_len.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

import "buf/validate/validate.proto";

message User {
string username = 1 [(buf.validate.field).string = {
// `min_len` validates that username must have at least 3 characters.
min_len: 3;
// `max_len` validates that username must have at most 16 characters.
max_len: 16;
}];
string description = 2 [(buf.validate.field).string = {
// `min_bytes` specifies that a string field must have a least this many bytes.
// In this case, it validates that description must have a least 0 byte,
// which is a no-op.
min_bytes: 0,
// `max_bytes` specifies that a string field must have at most this many bytes.
// In this case, it validates that description can have at most 500 bytes.
max_bytes: 500
}];
// `len_bytes` specifies that a string field must have exactly this many bytes.
// In this case, it validates that `id` must have exactly 20 bytes.
string id = 3 [(buf.validate.field).string.len_bytes = 20];
// `len` specifies that a string field must have exactly this many characters.
// In this case, it validates that `abbreviation` must have exactly 3 characters.
string abbreviation = 4 [(buf.validate.field).string.len = 3];
}
63 changes: 63 additions & 0 deletions examples/option_string_match_pattern.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

import "buf/validate/validate.proto";

message UserProfile {
// `uuid` validates that a string field is a valid uuid.
string user_id = 1 [(buf.validate.field).string.uuid = true];
// `email` validates that a string field is a valid email.
string email = 2 [(buf.validate.field).string.email = true];
// phone_number must match the this pattern if it's not empty.
// Note: the `\`s in the regex pattern must be escaped.
string phone_number = 3 [(buf.validate.field) = {
string: {
// `pattern` specifies a regex pattern and validates that the string field
// must match it.
pattern: "^(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]\\d{3}[\\s.-]\\d{4}$",
},
ignore_empty: true,
}];
// `hostname` specifies that the field value must be a valid hostname as defined
// by [RFC 1034](https://tools.ietf.org/html/rfc1034#section-3.5).
// Note: This constraint doesn't support internationalized domain names (IDNs).
string organization_domain = 4 [(buf.validate.field).string.hostname = true];
string personal_page = 5 [
// `uri` validates that a string field must be an absolute uri, as defined by
// [RFC 3986](https://tools.ietf.org/html/rfc3986#section-3).
//
// Or use `uri_ref` to validate that it must be a relative or aboslute URI, as
// defined by [RFC 3986](https://tools.ietf.org/html/rfc3986#section-3).
// (buf.validate.field).string.uri_ref = true,
(buf.validate.field).string.uri = true,
(buf.validate.field).ignore_empty = true
];
// `ip` specifies that a string field must be a valid ip address, in either v4 or v6.
string last_login_at = 6 [(buf.validate.field).string.ip = true];
// To validate against a specific version, use `ipv4` or `ipv6` instead.
//
// string last_login = 5 [(buf.validate.field).string.ipv4 = true];
// string last_login = 5 [(buf.validate.field).string.ipv6 = true];
}

message HyperLink {
string text = 1;
// `address` specifies that the field value must be either a valid hostname
// as defined by [RFC 1034](https://tools.ietf.org/html/rfc1034#section-3.5)
// (which doesn't support internationalized domain names or IDNs) or a valid
// IP (v4 or v6).
string address = 2 [(buf.validate.field).string.address = true];
}
Loading

0 comments on commit 850813b

Please sign in to comment.