Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt AEP-202: Fields #155

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
54 changes: 54 additions & 0 deletions aep/general/0202/aep.md.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Fields

In many cases, a field's particular value can be represented in many different ways.
String representations of dates are an excellent example, where a date can be represented in multiple ways
and an API cannot necessarily normalize the data without additional information about its format.

The allowed formats for a field's value must be properly documented and enforced so that the API
can ensure that it receives data in a way that it can properly handle and send it back in a way
that consumers expect.

The [`buf.validate.FieldConstraints`][protovalidate] type, through its accompanying
extension `buf.validate.field`, enriches a field's schema beyond the basic
name and type information by specifying constraints or validation rules.

## Guidance

If a field follows a set pattern, it **should** be annotated with a `buf.validate.field` annotation.

If a field has a `buf.validate.field` annotation, that annotation **must** be included on all requests and responses.

If a field has a `buf.validate.field`, any values that do not follow the `buf.validate.field` annotation must return an error.

If a field has a `buf.validate.field`, an API **must** return an error if it attempts to return a value that does not follow the annotation.


#### Backwards compatibility

Adding a validation rule to an existing, unspecified field **is not** backwards
compatible, *unless* the field in question has always been documented as conforming to the format
being specified.

Changing an existing validation rule to a different one in all cases **is not**
backwards compatible.

## Rationale

#### Why add a format specifier?
rambleraptor marked this conversation as resolved.
Show resolved Hide resolved

The format of a primitive-typed field can be critical to its usability. Some
programming languages may convey a specific type format as a standalone type,
as Java does with [UUID][java uuid]. Most have specific structural requirements
that are validated by the service, so conveying the format to the user ahead of
time is critical to their experience.

#### Why document value normalizations?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, what guidance does this refer to?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above guidance should satisfy this rationale.

Copy link
Collaborator

@rofrankel rofrankel Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which guidance, specifically? As in, which line of the file says to document value normalization? The word "normalization" doesn't appear before line 37.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wasn't being precise enough about that. It was implicit. I've added some information in the beginning explaining how validators are useful for field normalization and why that process occurs.

Let me know if it isn't precise enough.


In many cases, the API will be responsible for parsing the user input and normalizing it
into a value that the API expects.

While primitive comparison is not recommended for any of the supported formats,
uniform normalization of values is important to set consumer expectations, and
create a user-friendly surface.

[protovalidate]: https://github.com/bufbuild/protovalidate/tree/main/proto/protovalidate
7 changes: 7 additions & 0 deletions aep/general/0202/aep.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
id: 202
state: approved
slug: fields
created: 2024-07-12
placement:
category: protobuf
Loading