Nesting ArkTypes within a regex
Is there a way to put ArkType types within a regex? As an examplewhere for
z
something like hi 3
would be valid, but not hi 13
or hello 5
-- i.e. to be valid, the value passed into z
has to be a string of length 1 or 2, followed by a space, followed by a number 1-9 (inclusive))25 Replies
I know I can do something likewhere
callback
handles the x
and y
, but I'm looking for a direct wayThere's no description, so is that actually what I'm after?
If so, good to know it's on the backlog
You are after
`${x} ${y}`
, right?Hmm, I guess, yeah
I'd like it to work within regexes too
I guess I can add a comment to the issue stating that
How tho?
String type can't be a part of regex in general
...well, it can, but that's complicated
Hmm, I guess it doesn't strictly have to work within regex, but would be nice to have that
Since a regex will always have an equivalent arktype type if this template literal stuff is a thing
for that you need a "complete" regex type
Like
/^\d+$/
. And if you get like /\d+\d+\d+\d+/
that's DDOS
But well, I like your idea of "regex literals"
It's 0<alpha<3
btw
Then it can be inferred as for something along the lines of "221B Baker Street, London, NW1 6XE".
So I guess the equivalent with template literals, would be something likewhich is much tidier and clearer what's going on.
Isn't that different? I think
alpha
is just letters or something, whereas string
is "any string"You MUST use selection groups as you may have narrows and morphs
Yes
Wdym?
integer string is
\d+
, integer<987 is the same, you need to parse it to validate further\d+
isn't the same as integer < 999
though. To be the same it'd have to be \d{1,3}
(and exclude 999 + handle negatives, I guess)Okay, a better case -
0<integer%3<400
Which means "greater than 0, less than 400, and a multiple of 3"?
Right, so is your point that this can't be represented in regex? I.e. need more validation than just a regex
Well, %3 can be actually (there's a challenge for that)
But generally yes
Right, I see, yeah
So we'd need to support types in regex too
Also I would prefer it somehow being able to parse that into object, or a tuple
A tuple I think
That's where you need regex groups
So you first capture group to morph from template literal into a tuple, and then you just have a tuple type do whatever you want
I can make an implementation actually
Want one?
All regexes have an equivalent ArkType type though, right? Even if it requires you to use a morph. So do you really need all types to be possible in regex?
Not being able to do
0<integer%3<400
in regex doesn't seem like an issue, since you can just use an ArkType type with template literals instead of a regex typeI can try to make an implementation if you want
If you'd like to, sure :)
This was just an example I came up with on-the-spot, I'm not actually needing it.
Anyways, here's my CSV parser @TizzySaurus
Nice! :)
Really cool stuff 😍
For anyone looking at this in the future, please refer/discuss here:
https://github.com/arktypeio/arktype/issues/491
https://github.com/arktypeio/arktype/issues/695
GitHub
Type-safe regex · Issue #695 · arktypeio/arktype
This would involve updating the static parser to give more detailed feedback and inference for regex literals embedded in strings. The general goals would be: If a RegExp will throw an error when i...