Generated toJson and safeToJson skip model-level refinements
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 42/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- dart
调研方向
Start by tracing how @Schemable() generates WindowSchema, the loader, and toJson/safeToJson; compare that path with Window.schema.safeEncode and its model-level refinement. Identify a generator-supported registration point that avoids a recursive Window.schema reference. Done means the generated loader and encode helpers enforce the refinement, with WindowSchema.schema still usable as a base.
由索引模型根据 Issue 内容生成。
描述
Reproduction
Verified on Ack and ack_generator 2.0.0-beta.3 from main at b8eb8eb with Dart 3.14.0. Put this in lib/encode_repro.dart in a package with ack, ack_generator, and build_runner, then run dart run build_runner build and dart run lib/encode_repro.dart.
import 'package:ack/ack.dart';
part 'encode_repro.g.dart';
@Schemable()
final class const Window({required final int start, required final int end})
with _$WindowAck {
static final schema = WindowSchema.schema.refine(
(window) => window.start <= window.end,
message: 'start must not exceed end',
);
}
void main() {
const invalid = Window(start: 2, end: 1);
print(invalid.toJson());
print(invalid.safeToJson().isOk);
final checked = Window.schema.safeEncode(invalid);
print(checked.isOk);
print(checked.getError().runtimeType);
}
Actual
toJson() returns {start: 2, end: 1} and safeToJson().isOk is true. The same instance sent through Window.schema.safeEncode fails with SchemaValidationError. The generated helpers go through WindowSchema, which knows the field rules but not the cross-field refinement on Window.schema.
Suggested change
Provide an explicit generator-supported way to register a model-level refinement so it is part of the schema used by both the generated loader and the generated encode helpers. Keep WindowSchema.schema usable as a base without introducing a recursive reference to Window.schema. Until then, callers can use Window.schema.safeParse and Window.schema.safeEncode explicitly.
- 主要语言
- Dart
- 星标
- 64
- 派生
- 3
- 平均合并
- 17 小时 32 分钟
- 30 天内合并 PR
- 33
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
btwld/ack 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 68/100
维护者通常 1 天内回复
-
2.0 enhancement
难度 5/5 一周以上 新手友好度 30/100
维护者通常 1 天内回复
-
bug
难度 5/5 一周以上 新手友好度 35/100
维护者通常 1 天内回复
-
2.0 enhancement
难度 4/5 3-5 天 新手友好度 25/100
维护者通常 1 天内回复
-
2.0 enhancement
难度 5/5 一周以上 新手友好度 18/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 80/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
fujidaiti/smooth_sheets#655 ·
维护者通常 1 天内回复
-
Drop the decision-issue rule from ARCHITECTURE.md可能已有人在做 @abianche 今天认领。 未关闭chore
难度 1/5 1 小时以内 新手友好度 88/100
维护者通常 1 天内回复
-
难度 1/5 1-3 小时 新手友好度 61/100
维护者通常 1 天内回复
-
area: library feature request
难度 2/5 1-3 小时 新手友好度 64/100