Unique attribute
Hi i created this schema to validate that a name is unique compared to existing name, however i wonder if it is possible to define the schema in a separate file and send the exiting names within the context
Thank you for your help
7 Replies
It's not possible to provide it in the context
I guess one option would be to have
existingNames
as an exported let
variable, which gets edited by other filesbut existing names here is actually names retreived from the database so i cannot export them
In theory something like this should be fine
You just have an in-memory cache of the names (which I didn't as a set rather than an array for optimisation)
Otherwise you'd need to fetch the names within the
.narrow()
, but doing a database call on every otherwise validation seems nasty to me
@Moon shineThank you for your solution! but i think if exporting a Set of existingNames to be edited by other components can lead to race conditions or stale data and tests may interfere with each other since the Set isn’t isolated
If you're in that scenario, you can look into solutions like Redis, which from my understanding are designed specifically to get around those issues
Thank you for your response i will look into it
Hi @TizzySaurus
i ended up adding a useEffect that will setError manually, however it's overridden by the arktype schema so the i had to add a setTimeout. i just wonder would you do it differently ?
Possibly https://discord.com/channels/957797212103016458/957804102685982740/1399438259641123038
It seems weird to me having additional validation outside the AT type