Violation represents a single instance where a validation rule, expressed
as a Rule, was not met. It provides information about the field that
caused the violation, the specific rule that wasn't fulfilled, and a
human-readable error message.
For example, consider the following message:
message User {
int32 age = 1 [(buf.validate.field).cel = {
id: "user.age",
expression: "this < 18 ? 'User must be at least 18 years old' : ''",
}];
}
It could produce the following violation:
{
"ruleId": "user.age",
"message": "User must be at least 18 years old",
"field": {
"elements": [
{
"fieldNumber": 1,
"fieldName": "age",
"fieldType": "TYPE_INT32"
}
]
},
"rule": {
"elements": [
{
"fieldNumber": 23,
"fieldName": "cel",
"fieldType": "TYPE_MESSAGE",
"index": "0"
}
]
}
}
Violation represents a single instance where a validation rule, expressed
as a Rule, was not met. It provides information about the field that
caused the violation, the specific rule that wasn't fulfilled, and a
human-readable error message.
For example, consider the following message:
message User {
int32 age = 1 [(buf.validate.field).cel = {
id: "user.age",
expression: "this < 18 ? 'User must be at least 18 years old' : ''",
}];
}
It could produce the following violation:
{
"ruleId": "user.age",
"message": "User must be at least 18 years old",
"field": {
"elements": [
{
"fieldNumber": 1,
"fieldName": "age",
"fieldType": "TYPE_INT32"
}
]
},
"rule": {
"elements": [
{
"fieldNumber": 23,
"fieldName": "cel",
"fieldType": "TYPE_MESSAGE",
"index": "0"
}
]
}
}
Optional[customReturns a safe value for JSON logs.
Returns a safe value for JSON logs.
Optional[unknownPreserves protobuf fields that this SDK version does not recognize.
Contains the fully qualified protobuf type name.
Optionalfieldfield is a machine-readable path to the field that failed validation.
This could be a nested field, in which case the path will include all the parent fields leading to the actual field that caused the violation.
For example, consider the following message:
message Message {
bool a = 1 [(buf.validate.field).required = true];
}
It could produce the following violation:
violation {
field { element { field_number: 1, field_name: "a", field_type: 8 } }
...
}
Optionalforfor_key indicates whether the violation was caused by a map key, rather than a value.
Optionalmessagemessage is a human-readable error message that describes the nature of the violation.
This can be the default error message from the violated Rule, or it can be a custom message that gives more context about the violation.
Optionalrulerule is a machine-readable path that points to the specific rule that failed validation.
This will be a nested field starting from the FieldRules of the field that failed validation.
For custom rules, this will provide the path of the rule, e.g. cel[0].
For example, consider the following message:
message Message {
bool a = 1 [(buf.validate.field).required = true];
bool b = 2 [(buf.validate.field).cel = {
id: "custom_rule",
expression: "!this ? 'b must be true': ''"
}]
}
It could produce the following violations:
violation {
rule { element { field_number: 25, field_name: "required", field_type: 8 } }
...
}
violation {
rule { element { field_number: 23, field_name: "cel", field_type: 11, index: 0 } }
...
}
Optionalrulerule_id is the unique identifier of the Rule that was not fulfilled.
This is the same id that was specified in the Rule message, allowing easy tracing of which rule was violated.
Violationrepresents a single instance where a validation rule, expressed as aRule, was not met. It provides information about the field that caused the violation, the specific rule that wasn't fulfilled, and a human-readable error message.For example, consider the following message:
It could produce the following violation: