Context-sensitive validation / validation with parameters
What is the best/easiest way to write a validator which ensures that all
bar
s are present in bars
?30 Replies
Probably a narrow? Even if we implemented the relative path comparison syntax, it would likely only be for equality checks like
password: "string", "repeatPassword: "./password"
or whatever.
This is definitely too complex for a dedicated syntax.I guess it would be possible with something like
or something
, but would like to implement it like
So the thing I'm looking for is
beforeValidate
/afterValidate
hooks to do smth like
Oh maybe you were asking for the opposite
bar
s bars
a bit confusing hahaOr even better, being able to use
context
like
That might be achievable at some point with the path syntax because it would be something like
../../bars[number]
I can't '..' because "I have no idea where the
bar
values are placed deep in the object"Maybe
/bars[number]
would be from the root of the type@ssalbdivad what is the most straightforward way to
(i.e. ordered validation)
Oror something
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
What is the goal here? There's actually this related question I've been thinking about regarding preserving ordering through intersections of narrows.
In general, narrows will run sequentially in the order they're defined, but they're not like morphs where the order itself is part of the type.
// as long as the narrows in l and r are individually safe to check // in the order they're specified, checking them in the order // resulting from this intersection should also be safe.
can you return Out from narrow, again?
No? Narrow is a constraint, not a morph
If you mean can it be a type guard then yes, you use
is
like you normally would in TSerm I dunno what I wrote
Couldn't you just use a morph?
...I wrote not the thing I did want I guess
I'm not sure, I huess I can
Morphs are just arbitrary mappings, it makes sense to call other validators from them, and they encode sequentiality so that would probably be the natural way to do it
The thing is, I want to
...
type()
.catch()
Okay that does make proper senseI think it is a good idea
Can you link me the playground?
Is it pinned somewhere?
E.g. you want to transform some error to
undefined
for some config option you don't actually need to respect
No it was just an idea, I have not followed up on it
It would be somewhat complicated to represent in the type system unfortunatelynevermind its on https://arktype.io/docs/
Intro | ArkType
replace(./dev/arktype.io/static,) |> replace({ type },{"{"} type {"}"}) -->
I would have to think more about it. I guess it's just a morph at whatever level
Oh sorry I thought you meant a playground with that functionality
So here's my test case
@ssalbdivad here I go https://stackblitz.com/edit/ymnonu?file=type.ts
@ssalbdivad lemme explain
I have a big-ass (somewhat legacy) graph-based object with, erm, 5 types of IDs and with depth of 5 or something
I just don't think anything other than a custom function is going to result in an efficient way of doing this
30 custom functions
I think I'll try to make a morph-based wrapper for now
I think there are abstractions you could make around these kinds of scenarios for ID -based relationships and resolutions. Probably outside the scope of the core package, but could definitely be a useful library
GitHub
Add
userDefinedContext
to validation context · Issue #843 · arkty...Request a feature Add userDefinedContext (along with objectPath and existing root and path) to validation context and userDefinedContext to define it 🤷 Motivation Sometimes you may want your childr...