paoloricciuti / sveltekit-search-params

The easiest way to read and WRITE from query parameters in sveltekit.
https://sveltekit-search-params.netlify.app
MIT License
478 stars 13 forks source link

Setting a default if enumerated parameter is invalid #38

Open Rykuno opened 1 year ago

Rykuno commented 1 year ago

Describe the problem

Provide an api to check validity of query keys and provide defaults if invalid.

For whatever reason some users try to modify our search params manually. We assume this mostly happens when a url not fully copied to the clipboard. Nonetheless you'll end up with a query that looks like ?sort=highes for example; where the "t" is cutoff.

At the moment we are the default parameter but that only adds a default value if the key is not present instead of checking if the value is valid it seems.

We are currently checking this in our onMount like so; but it gets verbose when there are 15 keys to check for (search heavy data app).

    onMount(() => {
        if ($store.sort !== 'highest' ?? $store.sort !== 'recent') {
            $store.sort = 'highest';
        }
    });

Describe the proposed solution

An api similar to the default feature that would not only check for a missing key, but check for its validity and set a default value if invalid.

paoloricciuti commented 12 months ago

Mmm i like this idea but i have to think through the api and see what's possible. I'll definitely try this out as soon as i have time

paoloricciuti commented 8 months ago

I just realized you can already achieve this with a custom encode and decode function