Validator
Allows you to define custom logic to determine whether your local data is still valid or if it needs to be refreshed from the remote source using the Fetcher.
Purpose of the Validator
- Data Freshness: Ensures the data served to your application is up-to-date and meets your application’s specific validity criteria.
- Optimizing Network Usage: Prevents unnecessary network calls by using valid cached data when appropriate.
- Consistency Control: Gives you fine-grained control over when to refresh data, enhancing consistency between the client and server.
APIs
Validator
Validator has the following structure:
The type representing your domain data model. For example,
if you have a Store<Int, Post>
, the Output
is Post
.
A suspending function that determines whether the given item is still valid.
Data Flow
Data Retrieval Request
When your application requests cached data from the Store, it first checks the Memory Cache and the Source of Truth to see if the data is available.
The Validator only operates on data from the Source of Truth and does not validate data coming directly from the network.
Validation Check
The Validator’s isValid
method is called with the cached data as the parameter.
Data Update
If new data is fetched, it’s stored in the Memory Cache and the Source of Truth for future requests.
Implementing a Validator
You can create a Validator using the Validator.by
factory method:
Examples
Time-Based Validation
If your application has data that should be refreshed every 24 hours:
Versioning
If your application data model changes and you need to invalidate old cached data:
User Authentication
In cases where authentication tokens expire:
Best Practices
- Keep Validation Logic Lightweight: The
isValid
function should execute quickly to avoid slowing down data retrieval. Complex computations or I/O operations should be avoided. - No Side Effects: The Validator should not modify the data or state. It should only assess the validity of the provided item.
- Decide on Validity Criteria: Clearly define what makes data valid or invalid in your application’s context. This could be based on timestamps, data content, user preferences, or other domain-specific factors.