diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel index 2f618b26..1db7ad4c 100644 --- a/examples/BUILD.bazel +++ b/examples/BUILD.bazel @@ -72,7 +72,7 @@ proto_library( "option_duration_range.proto", "option_enum_allow_values.proto", "option_enum_disallow_values.proto", - "option_field_ignore_empty.proto", + "option_field_ignore.proto", "option_field_presence.proto", "option_field_skip_validation.proto", "option_map.proto", diff --git a/examples/option_field_ignore.proto b/examples/option_field_ignore.proto new file mode 100644 index 00000000..860df568 --- /dev/null +++ b/examples/option_field_ignore.proto @@ -0,0 +1,51 @@ +// 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 ProfileService { + rpc CreateProfile(CreateProfileRequest) returns (CreateProfileResponse); + rpc UpdateProfile(UpdateProfileRequest) returns (UpdateProfileResponse); +} + +message CreateProfileRequest { + string name = 1 [(buf.validate.field).required = true]; + string email = 2 [ + (buf.validate.field).string.email = true, + // If email field is left empty, it's not validated. + (buf.validate.field).ignore = IGNORE_EMPTY + ]; + string description = 3; +} + +message UpdateProfileRequest { + uint64 id = 1; + // The following fields are optional as this is an update request. + optional string name = 2; + optional string email = 3 [ + (buf.validate.field).string.email = true, + // `IGNORE_DEFAULT` is used here: + // If this field is unpopulated, we don't validate it as an email. + // If this field is populated and is set to the empty string (the default + // value), it is also ignored. This is to allow users to unset their emails. + (buf.validate.field).ignore = IGNORE_DEFAULT + ]; + optional string description = 4; +} + +message CreateProfileResponse {} + +message UpdateProfileResponse {} diff --git a/examples/option_field_ignore_empty.proto b/examples/option_field_ignore_empty.proto deleted file mode 100644 index 1fb85e13..00000000 --- a/examples/option_field_ignore_empty.proto +++ /dev/null @@ -1,36 +0,0 @@ -// 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 FeedbackService { - rpc SubmitFeedback(SubmitFeedbackRequest) returns (SubmitFeedbackResponse); -} - -message SubmitFeedbackRequest { - // feedback is the content of feedback to submit. - string feedback = 1; - // email is the email. - string email = 2 [ - // `ignore_empty` skips validation on a field if it's empty. - // The email will be not validated if it's empty. - (buf.validate.field).ignore_empty = true, - // `string.email` validates that a string field is a valid email. - (buf.validate.field).string.email = true - ]; -} - -message SubmitFeedbackResponse {} diff --git a/examples/option_number_allow_values.proto b/examples/option_number_allow_values.proto index 79daad23..0605bbad 100644 --- a/examples/option_number_allow_values.proto +++ b/examples/option_number_allow_values.proto @@ -23,8 +23,8 @@ service CarService { message SearchCarRequest { optional fixed32 number_of_seats = 1 [(buf.validate.field) = { - // `ignore_empty` skips validation if the field isn't set. - ignore_empty: true, + // `IGNORE_EMPTY` skips validation if the field isn't set. + ignore: IGNORE_EMPTY, fixed32: { // `in` requires that the value must be one of the specified values. // In this case, it validates that the number of seats is either 5 or 7. diff --git a/examples/option_number_range.proto b/examples/option_number_range.proto index 5613d5c1..d548a19d 100644 --- a/examples/option_number_range.proto +++ b/examples/option_number_range.proto @@ -37,7 +37,7 @@ message CreateReviewRequest { message CreateReviewResponse { // aggregated_review is the aggregated review after this one in request is created. // This value is empty if there are less than a 100 reviews submitted. - AggregatedReview aggregated_review = 1 [(buf.validate.field).ignore_empty = true]; + AggregatedReview aggregated_review = 1 [(buf.validate.field).ignore = IGNORE_EMPTY]; } message AggregatedReview { diff --git a/examples/option_string_match_pattern.proto b/examples/option_string_match_pattern.proto index 1bac8a72..a8f0216f 100644 --- a/examples/option_string_match_pattern.proto +++ b/examples/option_string_match_pattern.proto @@ -29,7 +29,7 @@ message UserProfile { // must match it. pattern: "^(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]\\d{3}[\\s.-]\\d{4}$", }, - ignore_empty: true, + ignore: IGNORE_EMPTY, }]; // `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). @@ -43,7 +43,7 @@ message UserProfile { // 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 + (buf.validate.field).ignore = IGNORE_EMPTY ]; // `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];