feat(protoc-gen-es): narrow JSON type for finite float/double fields#1399
Open
odsod wants to merge 1 commit intobufbuild:mainfrom
Open
feat(protoc-gen-es): narrow JSON type for finite float/double fields#1399odsod wants to merge 1 commit intobufbuild:mainfrom
odsod wants to merge 1 commit intobufbuild:mainfrom
Conversation
Add `json_types=protovalidate_finite` option that narrows the generated
JSON type for float/double fields from
`number | "NaN" | "Infinity" | "-Infinity"` to `number` when the field
has `(buf.validate.field).{float,double}.finite = true`.
- Extend minimal-validate.proto with FloatRules/DoubleRules messages
containing the `finite` field
- Add `isProtovalidateFinite()` helper to detect the constraint
- Update `fieldJsonType()` to return `"number"` when finite is detected
- Support compound `json_types` option values (e.g.
`json_types=protovalidate_finite`)
- `json_types=true` continues to work unchanged (no breaking change)
Author
|
@timostamm I submitted this mostly to check whether you think this is a good idea. The context on why we would like this is that we are using Vercel SSR + Edition 2023 (generated types always have a prototype chain for presence tracking), so to avoid serialization issues we use the Json types as the canonical types in our frontend. This improves the main friction point for us in using the generated Json types (not having to use functions that typecast away NaN and Inf when working with floats). What do you think - would something like this make sense? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
json_types=protovalidate_finiteis enabled and afloat/doublefield has(buf.validate.field).{float,double}.finite = true, the generated JSON type is narrowed fromnumber | "NaN" | "Infinity" | "-Infinity"to justnumber.This follows the existing pattern of
valid_types=protovalidate_required— reading protovalidate constraints at codegen time to produce tighter TypeScript types.Example
Generated with
json_types=protovalidate_finite:Option syntax
json_types=true— existing behavior, unchangedjson_types=protovalidate_finite— enables json types AND narrows finite-constrained fieldsChanges
minimal-validate.protowithFloatRules/DoubleRules(just thefinitefield) in theFieldRules.typeoneofisProtovalidateFinite()helper invalid-types.tsfieldJsonType()inutil.tsto return"number"when the constraint is detectedjson_typesoption values (analogous tovalid_types=protovalidate_required+legacy_required)Test plan
npm run generateinpackages/protobuf-testto regenerate with the updated pluginnpm testpasses (type-level tests assert narrowed types)json_types=truestill works identically (no regression)