zto_generator 0.1.0
zto_generator: ^0.1.0 copied to clipboard
build_runner code generator for zto — generates ZtoSchema constants and validated fromMap functions from @Dto-annotated classes.
zto_generator #
build_runner code generator for zto. It reads
your @ZDto-annotated classes and generates the $<Dto>Schema constants used by
zto for runtime validation and OpenAPI 3.0 schema generation.
You don't use this package directly — you add it as a dev_dependency and run
build_runner. The annotations and the runtime API live in
zto.
Installation #
dependencies:
zto: ^0.1.0
dev_dependencies:
build_runner: ^2.4.0
zto_generator: ^0.1.0
Usage #
- Annotate a DTO and add the
partdirective:
import 'package:zto/zto.dart';
part 'user_dto.g.dart'; // generated by zto_generator
@ZDto(description: 'Create a user')
class CreateUserDto with ZtoDto<CreateUserDto> {
@ZString(example: 'Alice')
@ZMinLength(2)
final String name;
@ZString()
@ZEmail()
final String email;
const CreateUserDto({required this.name, required this.email});
factory CreateUserDto.fromMap(Map<String, dynamic> map) => CreateUserDto(
name: map['name'] as String,
email: map['email'] as String,
);
}
- Run the generator:
dart run build_runner build --delete-conflicting-outputs
This produces user_dto.g.dart containing the $CreateUserDtoSchema constant:
final dto = $CreateUserDtoSchema.parse(body, CreateUserDto.fromMap);
Build-time validation #
Incompatible annotations fail the build with a clear message instead of failing
silently at runtime — e.g. applying @ZEmail() to a numeric field, or
@ZMin() to a String.
See also #
zto— annotations + runtime validationdart_frog_open_api— turns the generated schemas into Swagger / Scalar docs for Dart Frog
License #
MIT