Validation

バリデーションは、オブジェクトの値が受け入れ可能かどうかを確認し、見つかった問題をすべて報告する仕組みです。

なぜ必要か

入力はさまざまな場所から来ます。リクエストボディ、フォーム、設定ファイル、キューからのメッセージなどです。それぞれの値には確認が必要です。名前が空白でないこと、年齢が 18 以上であること、メールアドレスがその形をしていることなどです。こうした確認を if 文で毎回書くのは繰り返しが多く、書き忘れも起きやすくなります。

バリデーションでは、ルールをプロパティのアノテーションとして一度だけ書きます。バリデータがアノテーションを読み、破られたルールをすべて報告します。各エラーにはエラーコードが付くので、メッセージファイルでユーザー向けの文に変えられます。

自分のコードが生成し、すでに信頼できる値には必要ありません。型の確認にも必要ありません。それは Dart の型システムが行います。

短い例

import 'package:spring_core_dart/spring_core_dart.dart';

// @Introspected lets the generator describe the properties,
// so the validator can read them without reflection.
@Introspected()
class SignupForm {
  @NotBlank()
  @Size(max: 20)
  String name = '';

  @NotNull()
  @Email()
  String? email;

  @Min(18)
  int age = 0;
}
import 'package:my_app/src/beans.dart';
import 'package:my_app/src/signup_form.dart';
import 'package:spring_core_dart/spring_core_dart.dart';

const log = SpringLog('signup');

void main() {
  final context = ApplicationContext.run([appBeans]);

  // Use the context's introspector: it knows the application's classes.
  final validator = BeanValidator(
    introspector: context.getBean<BeanIntrospector>(),
  );

  final form = SignupForm()
    ..name = 'Ada'
    ..age = 17;

  for (final violation in validator.validateObject(form)) {
    // email: must not be null
    // age: must be greater than or equal to 18
    log.warning('${violation.propertyPath}: ${violation.message}');
  }
  context.close();
}

モジュール appBeans はアプリケーションで一度だけ @ComponentScan() final BeanModule appBeans = $appBeans; として宣言します(Beans を参照)。

自分で呼ばなくても検証が行われる場所

それ以外の場合は、BeanValidator を自分で呼ぶか、DataBinder を使って文字列の入力をオブジェクトにコピーし、同時に検証します。

このセクションのページ

Spring (Java) では

Java で対応するのは org.springframework.validation と Jakarta Bean Validation(通常は Hibernate Validator)の組み合わせです。

class SignupForm {
  @NotBlank @Size(max = 20) private String name;
  @NotNull @Email private String email;
  @Min(18) private int age;
}

Set<ConstraintViolation<SignupForm>> violations = validator.validate(form);

重要な違い:

関連項目