feat: add uri and duration transformed schemas#89
Conversation
|
To view this pull requests documentation preview, visit the following URL: Documentation is deployed and generated using docs.page. |
There was a problem hiding this comment.
Pull request overview
This PR adds two new transformed schema entry points—Ack.uri() and Ack.duration()—and introduces Duration range constraints (.min(), .max()) with JSON Schema emission and validation tests.
Changes:
- Add
Ack.uri()(string →Uri) andAck.duration()(integer milliseconds →Duration) transformed schemas. - Introduce
DurationConstraintandDurationSchemaExtensionsto support inclusive.min()/.max()checks and emitminimum/maximumin JSON Schema. - Add test coverage for URI and Duration parsing/validation, composition, error messaging, and JSON Schema output.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/ack/lib/src/ack.dart | Adds Ack.uri() and Ack.duration() transformed schema factories. |
| packages/ack/lib/src/constraints/duration_constraint.dart | Implements inclusive min/max validation + JSON Schema emission for Duration. |
| packages/ack/lib/src/schemas/extensions/duration_schema_extensions.dart | Adds .min() / .max() fluent APIs to TransformedSchema<int, Duration>. |
| packages/ack/lib/ack.dart | Exposes the new constraint and extension via barrel exports. |
| packages/ack/test/schemas/uri_validation_test.dart | Adds URI validation and JSON Schema tests for the transformed URI schema. |
| packages/ack/test/schemas/duration_validation_test.dart | Adds Duration validation, constraints, composition, error quality, and JSON Schema tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /// Creates a schema that parses URI strings into [Uri] objects. | ||
| /// | ||
| /// The schema validates the URI format before transformation, ensuring only | ||
| /// valid URIs are parsed. | ||
| /// |
There was a problem hiding this comment.
The docstring for Ack.uri() says it validates “valid URIs”, but the underlying PatternConstraint.uri() requires hasScheme and a non-empty host, which rejects valid RFC 3986 URIs like mailto:foo@bar.com or urn:isbn:.... Either loosen the validator to match “uri” semantics, or update the docs (and/or naming) to clarify it validates absolute URIs with an authority/host (i.e., URLs).
| bool isValid(Duration value) => switch (type) { | ||
| DurationComparisonType.min => | ||
| value.inMilliseconds >= reference.inMilliseconds, | ||
| DurationComparisonType.max => | ||
| value.inMilliseconds <= reference.inMilliseconds, | ||
| }; |
There was a problem hiding this comment.
DurationConstraint compares and serializes using inMilliseconds, which silently truncates Durations that aren’t whole-millisecond values (e.g., Duration(microseconds: 1500) becomes 1 ms). Since the schema represents integer milliseconds, consider validating that reference.inMicroseconds % 1000 == 0 (and possibly rejecting/rounding) to avoid surprising off-by-one behavior in min/max checks and JSON Schema output.
|
Addressed Copilot's feedback: URI docs (fixed): Updated the docstring for Duration microsecond truncation (no change): The JSON Schema representation is integer milliseconds, so sub-millisecond precision is intentionally out of scope. Adding a microsecond guard would over-validate a programmer error that the API design already makes unlikely. |
Adds Ack.uri() and Ack.duration() transformed schema entry points in ack.dart.
Introduces DurationConstraint plus DurationSchemaExtensions to support .min() and .max() constraints and JSON Schema emission (minimum/maximum).
Updates barrel exports so the new constraint and extension are publicly available.
Adds comprehensive schema tests for URI and Duration validation, composition behavior, error quality, and JSON Schema output.