Skip to content

Commit

Permalink
Update example for checking uniqueness of repeated fields (#227)
Browse files Browse the repository at this point in the history
This also adds an example of `map`. Adding it into the same example file
because I think a large percentage `map` usage in protovalidate is for
checking uniqueness.
  • Loading branch information
oliversun9 authored Jul 24, 2024
1 parent 5bbf2bf commit cbbac69
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 35 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ load("@rules_buf//buf:repositories.bzl", "rules_buf_dependencies", "rules_buf_to

rules_buf_dependencies()

rules_buf_toolchains(version = "v1.33.0")
rules_buf_toolchains(version = "v1.35.0")

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

Expand Down
2 changes: 1 addition & 1 deletion examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ proto_library(
"cel_repeated_field_all.proto",
"cel_repeated_field_exists_one.proto",
"cel_repeated_field_filter_and_count.proto",
"cel_repeated_field_unique.proto",
"cel_repeated_field_transform_and_unique.proto",
"cel_string_concatenation.proto",
"cel_string_contains.proto",
"cel_string_is_email.proto",
Expand Down
8 changes: 4 additions & 4 deletions examples/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ deps:
- remote: buf.build
owner: bufbuild
repository: protovalidate
commit: b9b8148056b94f6898cc669574bae125
digest: shake256:5660d7a38dd2ff9a7b8a6304bca6fe798dc6bcd7ecb06c4ce8ebdc0649c2fe969356b90a445a391195fdeae461fbbd9a917dab7687e090465addcb2dbb285b36
commit: a6c49f84cc0f4e038680d390392e2ab0
digest: shake256:3deb629c655e469d87c58babcfbed403275a741fb4a269366c4fd6ea9db012cf562a1e64819508d73670c506f96d01f724c43bc97b44e2e02aa6e8bbdd160ab2
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 28151c0d0a1641bf938a7672c500e01d
digest: shake256:49215edf8ef57f7863004539deff8834cfb2195113f0b890dd1f67815d9353e28e668019165b9d872395871eeafcbab3ccfdb2b5f11734d3cca95be9e8d139de
commit: 2bbd25900cb34c79bae97d85c948d3cf
digest: shake256:a6446e23f4408160217c22738a0c8c370ff3ff966f601d678960b803829cf53b5cf5998d20fcb3f9c9be944b24337843e7beb6e681ddbf9d036fb6491c0e47e7
44 changes: 44 additions & 0 deletions examples/cel_repeated_field_transform_and_unique.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 Author {
string name = 1;
}

message Book {
repeated string genres = 1 [(buf.validate.field).cel = {
id: "book_unique_genre"
message: "a genre cannot appear twice in the same book"
// `unique()` evaluates to whether each element in a list is unique.
//
// In this case, the expression validates that genres are unique.
expression: "this.unique()"
}];

repeated Author authors = 2 [(buf.validate.field).cel = {
id: "book_unique_authors"
message: "a name cannot appear twice in authors"
// `some_list.map(x, expression_x)` transforms list [x1, x2, ...] into [expression-x1, expression-x2, ...].
// In this case, the list of authors is transformed into the list of author names.
//
// `unique()` evaluates to whether each element in a list is unique.
//
// In this case, the expression validates that names in the list of authors are unique.
expression: "this.map(author, author.name).unique()"
}];
}
29 changes: 0 additions & 29 deletions examples/cel_repeated_field_unique.proto

This file was deleted.

0 comments on commit cbbac69

Please sign in to comment.