パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v2.0.1

verify

検証を簡単にする。拡張ベースの状態検証DSL。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
61
いいね数
55
Pub ポイント
140

パッケージをインストール

flutter pub add verify

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

Verify は、直感的な拡張ベースのDSLを介してフォームおよび状態検証を簡素化するFlutter/Dartパッケージです。開発者はデータ型上でフラuent構文を使用して、検証ルールを簡潔に定義できます。このパッケージはカスタマイズ可能な検証ロジックをサポートし、ボイラープレートコードを必要とせずに既存のワークフローにシームレスに統合されます。使いやすさを重視しており、複数のプラットフォームで検証ロジックの読みやすさと保守性を向上させます。

用途

  • フォーム入力検証
  • UI コンポーネントにおける状態の検証
  • リアルタイムユーザーのフィードバック
  • カスタム検証ルール
  • クロスプラットフォーム検証
  • 複雑な検証ロジックの簡素化

主な機能

  • 拡張ベースのDSL
  • 最小限のボイラープレート
  • チェーン可能な検証メソッド
  • カスタムバリデーターをサポートしています
  • すべての Flutter プラットフォームで動作します
  • 既存コードとの簡単な統合

適している対象

  • 簡単な検証を求める開発者
  • 綺麗なコードを重視するチーム
  • 多数のフォーム入力を持つアプリ
  • 複数のプラットフォームをターゲットにするプロジェクト
  • フロントエンド中心のFlutterアプリケーション

注意事項

  • 基本的な検証パターンに限定される
  • 組み込みの国際化なし
  • Dart の拡張について理解が必要
  • 非常に動的な検証ロジックには適していない
  • 複雑なシナリオでは追加の設定が必要になる場合があります

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

Validations made simple

Build Status Pub Package Codecov MIT License

A fp inspired validation DSL. For Dart and Flutter projects.

Features

  • Completely extensible (create your own combinators, validator primitives, etc)
  • Flexible Verify is an extension based API (There is not single class created its all pure functions)
  • Customizable (Define you own error types if required) organize validators how ever you want
  • Bloc friendly (See examples for a concrete implementation)
  • Null safe (as a prerelease)

Usage

Creating validators

A Validator is just a simple function alias:

// One of 2 variats depending on wether the validated subject will be transformed into another type or not
typedef ValidatorT<S, T> = Either<List<dynamic>, T> Function(S subject);
typedef Validator<S> = Either<List<dynamic>, S> Function(S subject);

So you can create your own validator by just specifying a function for example:

final Validator<String> emailValidator = (String email) {
  return email.contains('@') ? Right(email) : Left('Bad email format')
};

Create simple validators from predicates

A simpler way is to use some of the built in helpers.

final contains@ = Verify.that(
  (String email) => email.contains('@'),
    error: 'Bad email format'
    );

Use custom errors and filter them by type

Reuse validators

Use composition to build up more complex validators.

final Validator<String> emailValidator = Verify.all([ contains@, notEmpty ])
Validate and transform

Validators are also capable of transforming their input, so for instance we can do parsing and validation in one go.

final Validator<String, int> intParsingValidator = (String str) => Right(int.parse(str));

final validator = intParsingValidator.onException((_) => Error('not an integer'));
Field validations

Given a model, for instance a user:

class User extends Equatable {
  final String? phone;
  final String? mail;
  final int? age;

  const User(this.phone, this.mail, this.age);

  @override
  List<Object> get props => [phone ?? '', mail ?? '', age ?? ''];
}

Additional checks can be performed on the object and its fields by chaining a series of check and checkField methods.

final userValidator = Verify.empty<User>()
    .check((user) => !user.phone!.isEmpty, error: Error('phone empty'))
    .checkField((user) => user.mail!, emailValidator);

final someUser = User('','', 25);
final Either<List<Error>, User> validationResult = userValidator.verify(someUser);

Note: The difference between check and checkField is that the later ignore the verification when the value is null, this will likely change in next version supporting null safety.

Run a validator

Running a validator is a simple as passing in a parameter since its just a function. To be a bit more eloquent a verify method is provided, this method is special because besides forwarding the argument to the calling validator it can also be used to filter the error list and have it cast to a specific error type. Just supply a specific type parameter.

final signUpValidation = Verify.subject<SignUpState>();
final errors = signUpValidation
    .verify<SignUpError>(newState); // Either<List<SignUpError>, SignUpState>
Built in validators

Verify doesn't come with many built in validators, because they are so simple to create.

It does come with some regex shorthands.

final validator = Verify.fromRegex(RegExp(r"(^\d+$)", error: Error('not just digits')) // Validator<String, int>
Form validation

Often times you will have modeled your error type similar to:

enum FormField {
  email,
  password,
  passwordConfirmation,
}

class SignUpError extends ValidationError {
  final String message;
  final FormField field;

  SignUpError(this.message, {required this.field});

  @override
  String get errorDescription => message;
}

In these scenarios its convenient to be able to group errors by field.

The solution verify provides for this is:

final validator = Verify.inOrder<SignUpFormState>([
  validateMail,
  validatePassword,
  validateConfirmation,
]);

final Map<FormField, SignUpError> errorMap = validator
    .verify<SignUpError>(someState)
    .errorsGroupedBy((error) => error.field);

Sequencing

A slightly different API can be used to achieve the same results as the inOrder composition function.

final numberValidator = Verify.subject<int>()
  .then(Verify.that(
    (subject) => subject % 2 == 0,
    error: Error('not even'),
  ))
  .then(Verify.that(
    (subject) => subject >= 10,
    error: Error('single digit'),
  ));

final errors2 = numberValidator.errors(3); // yields 1 error
final errors = numberValidator.errors(4); // yields 1 error

This way you have quick access to errors segmented by field.

カテゴリー

依存関係

依存関係一覧 2 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。