搜尋套件、儲存庫與 APP 分類

套件詳情v2.0.1

verify

驗證變得簡單。基於擴展的狀態驗證DSL。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
61
喜歡數
55
Pub 分數
140

安裝套件

flutter pub add verify

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Verify 是一個 Flutter/Dart 套件,透過直觀的延伸式 DSL 簡化表單與狀態驗證。它讓開發者能夠使用流暢語法直接在資料類型上簡潔地定義驗證規則。此套件支援可自訂的驗證邏輯,並能無縫整合至現有工作流程中,無需撰寫重複程式碼。設計以易用為導向,提升跨多個平台驗證邏輯的可讀性與可維護性。

適用情境

  • 表單輸入驗證
  • 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。