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
TizzySaurus17mo 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
TizzySaurus17mo ago
There's no description, so is that actually what I'm after? If so, good to know it's on the backlog
Dimava
Dimava17mo ago
You are after `${x} ${y}`, right?
TizzySaurus
TizzySaurus17mo 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
Dimava17mo ago
How tho? String type can't be a part of regex in general ...well, it can, but that's complicated
TizzySaurus
TizzySaurus17mo 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
Dimava17mo 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
TizzySaurus17mo 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
Dimava17mo ago
You MUST use selection groups as you may have narrows and morphs Yes
TizzySaurus
TizzySaurus17mo ago
Wdym?
Dimava
Dimava17mo ago
integer string is \d+, integer<987 is the same, you need to parse it to validate further
TizzySaurus
TizzySaurus17mo 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
Dimava17mo ago
Okay, a better case - 0<integer%3<400
TizzySaurus
TizzySaurus17mo 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
Want results from more Discord servers?
Add your server