Nesting ArkTypes within a regex

Is there a way to put ArkType types within a regex? As an example
const x = type("0<string<3");
const y = type("0<number<10");
const z = type("/x y/");
const x = type("0<string<3");
const y = type("0<number<10");
const z = type("/x y/");
where 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
TizzySaurus
TizzySaurusOP2y ago
I know I can do something like
const z = type(["string", "|>", callback])
const z = type(["string", "|>", callback])
where callback handles the x and y, but I'm looking for a direct way
TizzySaurus
TizzySaurusOP2y ago
There's no description, so is that actually what I'm after? If so, good to know it's on the backlog
Dimava
Dimava2y ago
You are after `${x} ${y}`, right?
TizzySaurus
TizzySaurusOP2y ago
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
Dimava
Dimava2y ago
How tho? String type can't be a part of regex in general ...well, it can, but that's complicated
TizzySaurus
TizzySaurusOP2y ago
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
Dimava
Dimava2y ago
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
`${0<alpha<3} ${0<integer<10}` = /^\w{1,2}$/ + ' ' + /^\d+$/ = /^(\w{1,2}) (\d+)$/
`${0<alpha<3} ${0<integer<10}` = /^\w{1,2}$/ + ' ' + /^\d+$/ = /^(\w{1,2}) (\d+)$/
TizzySaurus
TizzySaurusOP2y ago
const address = type("/^\d{1,3}[A-Z]? [A-Za-z ']+, [A-Za-z \-]+, [A-Z]{2}\d{1,2} \d{1,2}[A-Z]{2}$/")
const address = type("/^\d{1,3}[A-Z]? [A-Za-z ']+, [A-Za-z \-]+, [A-Z]{2}\d{1,2} \d{1,2}[A-Z]{2}$/")
for something along the lines of "221B Baker Street, London, NW1 6XE". So I guess the equivalent with template literals, would be something like
const houseNumber = type(/^\d{1,3}[A-Z]?$/);
const road = type(/^[A-Za-z ']+$/);
const city = type(/^[A-Za-z \-]+$/);
const postCode = type(/^[A-Z]{2}\d{1,2} \d{1,2}[A-Z]{2}$/);
const address = type(`${houseNumber} ${road}, ${city}, ${postCode}`)
const houseNumber = type(/^\d{1,3}[A-Z]?$/);
const road = type(/^[A-Za-z ']+$/);
const city = type(/^[A-Za-z \-]+$/);
const postCode = type(/^[A-Z]{2}\d{1,2} \d{1,2}[A-Z]{2}$/);
const address = type(`${houseNumber} ${road}, ${city}, ${postCode}`)
which 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"
Dimava
Dimava2y ago
You MUST use selection groups as you may have narrows and morphs Yes
TizzySaurus
TizzySaurusOP2y ago
Wdym?
Dimava
Dimava2y ago
integer string is \d+, integer<987 is the same, you need to parse it to validate further
TizzySaurus
TizzySaurusOP2y ago
\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)
Dimava
Dimava2y ago
Okay, a better case - 0<integer%3<400
TizzySaurus
TizzySaurusOP2y ago
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
Dimava
Dimava2y ago
Well, %3 can be actually (there's a challenge for that) But generally yes
TizzySaurus
TizzySaurusOP2y ago
Right, I see, yeah So we'd need to support types in regex too
Dimava
Dimava2y ago
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?
TizzySaurus
TizzySaurusOP2y ago
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 type
Dimava
Dimava2y ago
I can try to make an implementation if you want
TizzySaurus
TizzySaurusOP2y ago
If you'd like to, sure :) This was just an example I came up with on-the-spot, I'm not actually needing it.
Dimava
Dimava2y ago
Anyways, here's my CSV parser @TizzySaurus
No description
TizzySaurus
TizzySaurusOP2y ago
Nice! :)
ssalbdivad
ssalbdivad2y ago
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...
Want results from more Discord servers?
Add your server