Skip to content

Commit

Permalink
Add isIpPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
higebu committed Sep 28, 2023
1 parent 8151372 commit d9ccde3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/cel.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ in `protovalidate`. These are free to use within [custom constraints](custom-con
| `isEmail` | `string.isEmail() -> bool` | Test whether the string is a valid email address |
| `isHostname` | `string.isHostname() -> bool` | Test whether the string is a valid hostname |
| `isIp` | `string.isIp() -> bool`<br/>`string.isIp(4) -> bool`<br/>`string.isIp(6) -> bool` | Test whether the string is a valid IP address, optionally limited to a specific version (v4 or v6) |
| `isIpPrefix` | `string.isIpPrefix() -> bool`<br/>`string.isIpPrefix(4) -> bool`<br/>`string.isIpPrefix(6) -> bool` | Test whether the string is a valid IP prefix, optionally limited to a specific version (v4 or v6) |
| `isUriRef` | `string.isUriRef() -> bool` | Tests whether the string is a valid (absolute or relative) URI |
| `isUri` | `string.isUri() -> bool` | Tests whether the string is a valid absolute URI |
| `unique` | `list(bool).unique() -> bool`<br/>`list(int).unique() -> bool`<br/>`list(uint).unique() -> bool`<br/>`list(double).unique() -> bool`<br/>`list(string).unique() -> bool`<br/>`list(bytes).unique() -> bool` | Test whether the items in the list are all unique. |
Expand Down
38 changes: 38 additions & 0 deletions examples/cel_string_is_ip_prefix.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 LocationService {
rpc LocationForIpPrefix(LocationForIpPrefixRequest) returns (LocationForIpPrefixResponse);
}

message LocationForIpPrefixRequest {
string ip_prefix = 1 [(buf.validate.field).cel = {
id: "valid_prefix",
message: ".",
// `some_string.isIpPrefix()` returns whether the string is a valid ip prefix.
// `isIpPrefix(4)` returns whehter a string is an ipv4 prefix.
// `isIpPrefix(6)` returns whehter a string is an ipv6 prefix.
// In this case, it validates that field `ip_prefix` must be a valid ip
// prefix, either ipv4 or ipv6.
expression: "this.isIpPrefix()"
}];
}

message LocationForIpPrefixResponse {
// ...
}
48 changes: 48 additions & 0 deletions proto/protovalidate/buf/validate/validate.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2818,6 +2818,54 @@ message StringRules {
expression: "!this.matches('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$') ? 'value must be a valid UUID' : ''"
}];

//`ip_prefix` specifies that the field value must be a valid IP (v4 or v6) prefix.
//If the field value isn't a valid IP prefix, an error message will be
//generated.
//
//```proto
//message MyString {
// // value must be a valid IP prefix
// string value = 1 [(buf.validate.field).string.ip_prefix = true];
//}
//```
bool ip_prefix = 25 [(priv.field).cel = {
id: "string.ip_prefix",
message: "value must be a valid IP prefix",
expression: "this.isIpPrefix()",
}];

//`ipv4_prefix` specifies that the field value must be a valid IPv4
//prefix. If the field value isn't a valid IPv4 prefix, an error message
//will be generated.
//
//```proto
//message MyString {
// // value must be a valid IPv4 prefix
// string value = 1 [(buf.validate.field).string.ipv4_prefix = true];
//}
//```
bool ipv4_prefix = 26 [(priv.field).cel = {
id: "string.ipv4_prefix",
message: "value must be a valid IPv4 prefix",
expression: "this.isIpPrefix(4)"
}];

//`ipv6_prefix` specifies that the field value must be a valid
//IPv6 prefix.
//If the field value is not a valid IPv6 prefix, an error message will be generated.
//
//```proto
//message MyString {
// // value must be a valid IPv6 prefix
// string value = 1 [(buf.validate.field).string.ipv6_prefix = true];
//}
//```
bool ipv6_prefix = 27 [(priv.field).cel = {
id: "string.ipv6_prefix",
message: "value must be a valid IPv6 prefix",
expression: "this.isIpPrefix(6)",
}];

//`well_known_regex` specifies a common well-known pattern
//defined as a regex. If the field value doesn't match the well-known
//regex, an error message will be generated.
Expand Down

0 comments on commit d9ccde3

Please sign in to comment.