Closed dearlordylord closed 1 week ago
If you expect an array from your JSON, you should also use the array
schema. You can then use transform
to decode it back to a set.
import * as v from 'valibot';
const Schema = v.object({
uniq: v.pipe(
v.array(v.string()),
v.transform((input) => new Set(input))
),
});
thanks for clarification @fabian-hiller ,
for anyone stumbling upon this, the Set behaviour that I expect can be generically represented like this
const set = <S extends BaseSchema<unknown, unknown, BaseIssue<unknown>>>(schema: S) => pipe(
array(schema),
check((v) => new Set(v).size === v.length, 'Expected unique items'),
transform((v) => new Set(v))
);
This is somewhat counterintuitive approach. What parsers usually receive is JSONs over a network that has fewer data types available. For instance, for a Set structure, it'll be an array of unique values over the network. The parser is expected to validate uniqueness and convert to a JS set. Compare with https://valibot.dev/api/isoDate/ : it doesn't expect a JS Date object, after all.