zto library

Annotation-based DTO validation and OpenAPI schema generation for Dart.

Quickstart

@Dto(description: 'Create user')
class CreateUserDto with ZtoDto<CreateUserDto> {
  @ZString('name') @ZMinLength(2) final String name;
  @ZInt('age')  @ZMin(18)      final int age;

  const CreateUserDto({required this.name, required this.age});

  static CreateUserDto fromMap(Map<String, dynamic> map) =>
      CreateUserDto(name: map['name'] as String, age: map['age'] as int);
}

// In a route:
final dto = Zto.parse(body, CreateUserDto.fromMap)
    .refine((d) => d.age < 120, message: 'Unrealistic age');

Classes

Dto
@deprecated Use @ZDto instead.
DtoToOpenApi
Converts a ZtoSchema into an OpenAPI 3.0 JSON Schema object definition.
FieldDescriptor
Internal model of a single DTO field, built from its annotations.
FieldValidator
Validates a single field value against its FieldDescriptor.
ZAlphanumeric
Validates that the string contains only alphanumeric characters.
ZBase64
Validates Base64 encoded string.
ZBool
ZDate
ZDouble
ZDto
Marks a class as a Zto DTO with optional parseType for key inference.
ZEmail
Validates e-mail format.
ZEndsWith
Validates that the string ends with suffix.
ZEntity
Marks a class as a Zto Entity. Semantically equivalent to ZDto.
ZEnum<T>
ZFile
ZFinite
Validates that the value is finite.
ZHex
Validates hexadecimal string.
ZHttpUrl
Validates HTTP or HTTPS URL.
ZIncludes
Validates that the string contains substring.
ZInt
ZInteger
Validates that the value is an integer (no fractional part).
ZIpv4
Validates IPv4 address format.
ZIpv6
Validates IPv6 address format.
ZIsoDate
Validates ISO 8601 date string (YYYY-MM-DD).
ZIsoDateTime
Validates ISO 8601 datetime string.
ZJwt
Validates JWT format.
ZLength
Enforces exact string length.
ZList
ZListOf
ZLowercase
Validates that the string is already in lowercase form.
ZMap
ZMax
Enforces maximum numeric value (inclusive).
ZMaxLength
Enforces maximum string length.
ZMetaData
ZMin
Enforces minimum numeric value (inclusive).
ZMinLength
Enforces minimum string length.
ZModel
Marks a class as a Zto Model — a persistable representation of a data aggregate (not necessarily a database table). Semantically equivalent to ZEntity/ZDto: it generates the same ZtoSchema. Use ZModel for persistable aggregates, ZEntity for domain entities with business rules, and ZDto for transport contracts.
ZMultipleOf
Validates that the value is a multiple of n.
ZNegative
Validates that the value is less than zero.
ZNonNegative
Validates that the value is greater than or equal to zero.
ZNonPositive
Validates that the value is less than or equal to zero.
ZNum
ZObj
ZObject
Annotation for nested DTO object fields.
ZPattern
Validates that the value matches the given regex pattern.
ZPositive
Validates that the value is greater than zero.
ZSafeInt
Validates that the value is within JavaScript safe integer range.
ZSlug
Validates URL slug format.
ZStartsWith
Validates that the string starts with prefix.
ZString
Zto
Main entry point for the zto package.
ZtoBoolRule
ZtoDoubleRule
ZtoDtos
@deprecated Use ZtoGenerateSchemas instead.
ZtoField
Base class for all Zto field type annotations.
ZtoGenerateSchemas
Lists DTO classes to include in generated $ZtoSchemas for Zto.registerSchemas.
ZtoIntRule
ZtoIssue
A single validation failure for one field or the DTO as a whole.
ZtoMap
Fluent Zod-like validation map for use without code generation.
ZtoMapResult
The result of a successful ZtoMap.parse call.
ZtoObjectRule
ZtoRule
Abstract base for fluent validation rules used with ZtoMap.
ZtoSchema
Describes a Zto DTO: its class name and the list of field descriptors generated from its @Z* annotations.
ZtoSchemaBase
Abstract base for generated DTO schema objects.
ZtoStringRule
ZtoValidator
Base for all Zto validator annotations.
ZUppercase
Validates that the string is already in uppercase form.
ZUrl
Validates URL format.
ZUuid
Validates UUID v4 format.

Enums

ParseType
Defines how Dart field names are converted to JSON map keys when no explicit ZtoField.mapKey is provided.

Mixins

ZtoDto<T>
Optional mixin that adds cross-field validation (refine) to any Dart class.

Extensions

ZtoRefineExtension on T
Adds refine to any value, so cross-field / business-rule validation can be chained right after ZtoSchema.parse / Zto.parse without requiring the ZtoDto mixin on the parsed class.

Constants

z → const _Z
Global singleton providing factory methods for fluent ZtoRule builders.

Typedefs

ZtoSchemaRegistration = (Object, ZtoSchema)
A (Type|factory, ZtoSchema) pair for Zto.registerSchemas.

Exceptions / Errors

ZtoException
Thrown when DTO validation fails.