Skip to content
On this page

Validators

You will learn

Sometimes, it is important to validate data after request based on input parameters. For example, you can have a query that takes ID and returns an object with this, but you want to validate that object has the same ID as you requested. In this case, you can use Validator.

ts
const blockQuery = createQuery({
  effect: createEffect(async ({ id }) => {
    const response = await fetch(`https://api.salo.com/blocks/${id}.json`);

    return response.json();
  }),
  validate: ({ result, params }) => result.id === params.id,
});

Validators vs Contracts

TL;DR

Contract for structure, Validator for content

Validator is a dynamic check that does not have any effect on the result of the query, it is only used to check if data is valid. While, Contract is a static check that ensures the exact structure of the data, it casts unknown result to a specific type.

Released under the MIT License.