error_case

Last updated:

0 purchases

error_case Image
error_case Images
Add to Cart

Description:

error case

Error Case #
Welcome to the error_case library. A library to define error cases that we can use to test our code.
Features #

Data class validation
Empty string validation
Minimum number validation
Maximum number validation
More to come in the future...

How to use #
Let's start with an example of a data class.
class User implements JsonModel {

User(this.id, this.name, this.address, this.age);

final String id;
final String name;
final Map<String, String> address;
final int age;

factory User.fromJson(Map<String, dynamic> json) =>
_$UserItemFromJson(json);

@override
Map<String, dynamic> toJson() => _$UserItemToJson(this);
}
copied to clipboard
In our data class User we have id, name, address, age properties.
This will be our sample data class throughout the guide.
It is important to note that this implements JsonModel a class that contains fromJson() & toJson() functions.
In this example I am also using json_serializable to generate fromJson & toJson functions.
class InvalidUserModelErrorCase extends ObjectSingleErrorCase {

InvalidUserModelErrorCase(): super(
['id', 'name', 'address.country', age], // Required properties
minimumValue: {'age': 21},
maximumValue: {'age': 200}
);
}

// P.S. There's a shorter way to implement this but we need Dart 2.16
copied to clipboard
In our error case we've defined our required fields id, name, address.country, age.
What is address.country? This defines a nested lookup that validates the keys it passes through and value at the end.
If we don't include a property of the class. It will be considered optional, no validation will happened to that property.
Let's see a brief example of how we use these classes we've written.
void main() {

final user = User( // Here's a sample User class
'ABCD1234', // id
'David Hamster', // name
{'country': 'Kingdom of Nocturnia'}, // address map
24 // age
);

const errorCase = InvalidUserModelErrorCase();

errorCase.validate(user,
_onInvalidUserModel,
_onValidUserModel);
}

void _onInvalidUserModel(Exception ex) {
// 'ex' can be a child of ErrorCaseException or a different Exception altogether.
}

void _onValidUserModel(User user) {
// When there are no errors, the data that was validated will be passed on as a parameter for this function.
}
copied to clipboard
To summarize the example above.

We have a user class with data.
We create a const of an Error case.
We then call .validate() and pass data, errorFunction, successFunction.

Non Returning Error Cases #
These are error cases that do not expect a return value on the error and success functions.
Classes

SingleErrorCase
ObjectSingleErrorCase

See error_case_test.dart
Returning Error Cases #
These are error cases that expect a return value on the error and success functions.
Classes

SingleErrorReturnCase
ObjectSingleErrorReturnCase

See error_case_return_test.dart
Good Practices #
DO: Create a new error case class for a specific scenario. Just like when defining a Use Case for specific business logics.
DONT: Combine with other scenarios. This will make it difficult to locate where the error is happening when on reports.
--
DO: Declare a new instance of an error case with const for compile time benefits.
DONT: Declare a new instance of an error case without const on the UI code because it can be recreated.
Developed by #

Edamama
Edamama Github Org

Contributors #

Azma Ahmed
Gian Patrick Quintana
Harsh Chaurasia
Omkar Gaikwad
Pruitvi Sandiri
Reyster Fresco
Shiv Pratap Singh
Shubhanshu Singh

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.