Skip to main content

Value transformation

Often you’ll want to parse the raw input, and then transform it into a model that your app actually uses. ZodArt makes this straightforward with the built-in transformation methods (e.g. .toInt(), .toObj(t), .toArray(t), etc.).

These transformation methods are applied only to non-null values. For more about the available transformations, see the types.

See full example here.

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:zodart/zodart.dart';

part 'transformations.freezed.dart';
part 'transformations.zodart.dart';
part 'transformations.zodart.type.dart';

.generateNewClass(outputClassName: 'LanguageDetail')
abstract class LanguageDetailSchema {
static final schema = (
name: ZString(),
version: ZString().optional(),
lastUpdate: ZDateTime(),
notes: ZArray(ZString()).nullable(),
);

static const z = _LanguageDetailSchemaUtils();
static final ZObject<LanguageDetail> zObject = z.zObject;
}


abstract class Language with _$Language {
const factory Language({
required String name,
required String version,
}) = _Language;
}

Language toLang(LanguageDetail l) => Language(name: l.name, version: l.version ?? 'Not available');

void main() {
final languageDetailsSchema = LanguageDetailSchema.zObject;
final languageSchema = languageDetailsSchema.toObj(toLang);

final res = languageSchema.parse({
'name': 'Dart',
'version': '3.9.0',
'lastUpdate': DateTime.parse('2025-08-13'),
'notes': null,
});

print(res.value); // Language(name: Dart, version: 3.9.0)
}