Skip to content

Installation

Minimum
Dart SDK 3.11
Flutter 3.41
analyzer resolved by your project, for luthor_generator 10, up to and including 14

luthor has no dependencies. luthor_generator works with freezed, json_serializable, dart_mappable, and plain classes with a fromJson. Those packages are optional companions. On Dart 3.11, use freezed 3.2.x; freezed 4 needs Dart 3.13 or later.

Terminal window
dart pub add luthor

Either command adds the dependency to pubspec.yaml:

pubspec.yaml
dependencies:
luthor: ^1.0.0

Import the package in each file that builds a validator.

import 'package:luthor/luthor.dart';

Skip this section if you will write every schema by hand. The generator reads classes annotated with @luthor and writes a schema, a validate function, and typed keys for each one. It runs through build_runner and needs no build.yaml.

Terminal window
dart pub add dev:build_runner dev:luthor_generator
pubspec.yaml
dev_dependencies:
build_runner: ^2.16.0
luthor_generator: ^1.0.0

Add the companion for your models alongside it. With freezed:

Terminal window
dart pub add freezed_annotation json_annotation
dart pub add dev:freezed dev:json_serializable

With dart_mappable:

Terminal window
dart pub add dart_mappable
dart pub add dev:dart_mappable_builder

Models without freezed covers json_serializable on its own, plain classes, and dart_mappable.

Run the generator after you change an annotated class:

Terminal window
dart run build_runner build

Use watch instead of build to regenerate on save.

Run this file.

bin/check.dart
import 'package:luthor/luthor.dart';
void main() {
final email = l.string().email().required();
print(email.validate('[email protected]'));
}
dart run bin/check.dart (first line)
ValidationSuccess(data: [email protected])
ValidationFailure(errors: {(root): [value must be a valid email address]})
ValidationFailure(errors: {email: [email must be a valid email address]})

Next: Your first validator.