feat(sdk): tighten Tailor field output types#1640
Conversation
🦋 Changeset detectedLatest commit: c9f0b8f The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| date: /^\d{4}-\d{2}-\d{2}$/, | ||
| datetime: | ||
| /^\d{4}-\d{2}-\d{2}[Tt](?<hour>[01]\d|2[0-3]):(?<minute>[0-5]\d):(?<second>[0-5]\d|60)(\.(?<fraction>\d+))?(?<offset>[Zz]|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/, | ||
| time: /^(?<hour>\d{2}):(?<minute>\d{2})$/, |
There was a problem hiding this comment.
The time regex only checks digit count, so isTimeString("99:99") returns true.
By contrast, the datetime regex above (lines 4-5) validates the actual range with [01]\d|2[0-3] / [0-5]\d, so isDateTimeString("2024-01-01T99:99:00Z") returns false.
Should time be range-restricted the same way?
| import type { OptionalKeysOf } from "type-fest"; | ||
| import type { z } from "zod"; | ||
|
|
||
| type UUIDString = `${string}-${string}-${string}-${string}-${string}`; |
There was a problem hiding this comment.
Instead of redefining UUIDString in this file, import it from packages/sdk/src/configure/types/scalar.types.ts.
If the real type is tightened later, this test will keep passing against the stale local definition.
| * @param value - Value to check | ||
| * @returns True when the value is a UUID string | ||
| */ | ||
| export function isUUIDString(value: unknown): value is UUIDString { |
There was a problem hiding this comment.
[nit] The is*/parse*/assert* helpers for UUID/date/datetime/time/decimal (15 functions total) are identical copy-paste apart from the validator and label string.
A generic factory like makeScalarHelpers(isValid, expected) would let a 6th scalar type be added in one place.
| // Generate union type from enum values | ||
| typeStr = metadata.allowedValues.map((v) => `"${v.value}"`).join(" | "); | ||
| } | ||
| const typeStr = |
There was a problem hiding this comment.
This 7-level nested ternary chain maps field type to a TS type string, which is hard to read.
A flat Record<string, string> lookup table would make this easier to scan and extend.
This comment has been minimized.
This comment has been minimized.
Resolve conflicts from parallel test refactors on v2: reconcile test.each table-driven rewrites with this branch's UUIDString/ DateString/Timestamp scalar tightening, restoring date/datetime edge-case coverage that the v2 shrink pass had dropped.
Code Metrics Report (packages/sdk)
Details | | main (00327c6) | #1640 (c638904) | +/- |
|--------------------|----------------|-----------------|-------|
+ | Coverage | 72.5% | 73.7% | +1.2% |
| Files | 433 | 430 | -3 |
| Lines | 16411 | 16474 | +63 |
+ | Covered | 11898 | 12151 | +253 |
+ | Code to Test Ratio | 1:0.4 | 1:0.4 | +0.0 |
| Code | 108441 | 111763 | +3322 |
+ | Test | 47500 | 49237 | +1737 |Code coverage of files in pull request scope (79.0% → 82.7%)DetailsSDK Configure Bundle Size
Runtime Performance
Type Performance (instantiations)
Reported by octocov |
|
LGTM |
Summary
Tailor field outputs now infer stricter string shapes for UUID, date, datetime, time, and decimal fields.
Type Output
string`${string}-${string}-${string}-${string}-${string}`stringDateStringstring | DateDateTimeString | DatestringTimeStringstring`${number}`Helper Usage
Scalar helpers are available when a value starts as
stringorunknownand needs to be narrowed before passing it to a strict scalar API. UUID example:The same
is*String,assert*String, andparse*Stringhelper families are available for date, datetime, time, and decimal strings.Type Cost
Notes
stringvalues to inferred UUID, date, datetime, time, or decimal field outputs may need matching string shapes or an explicit widen.yyyy-MM-ddTHH:mm:ssZyyyy-MM-ddTHH:mm:ss+09:00is*String,parse*String, andassert*Stringvariants are available for UUID, date, datetime, time, and decimal strings.tailor.d.tsattributes/attribute lists, and runtime user IDs now use the same strict scalar aliases.