Skip to content

Validating a string starts with a substring

import 'package:luthor/luthor.dart';
void main() {
final validator = l.string().startsWith('Hello');
print(validator.validateValue('Hello World!'));
}
import 'package:luthor/luthor.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'starts_with.freezed.dart';
part 'starts_with.g.dart';
@luthor
@freezed
abstract class StartsWithSchema with _$StartsWithSchema {
const factory StartsWithSchema({
@StartsWith('Hello') required String value,
}) = _StartsWithSchema;
factory StartsWithSchema.fromJson(Map<String, dynamic> json) =>
_$StartsWithSchemaFromJson(json);
}
void main() {
final result = $StartsWithSchemaValidate({'value': 'Hello World!'});
switch (result) {
case SchemaValidationSuccess(data: final data):
print('✅ Valid: ${data.value}');
case SchemaValidationError(errors: final errors):
print('❌ Errors: $errors');
}
}