A
arktype•5d ago
McKamyk

validating 0x prefixed hex

How can I validate a hex string that is prefixed by 0x<hex> in typescript...
type Address = `0x${string}`
type Address = `0x${string}`
Can I do this same thing here?
6 Replies
McKamyk
McKamykOP•5d ago
In zod I would
const Address = z.string().startsWith('0x')
const Address = z.string().startsWith('0x')
but that never passed through to 0x... in typescript land
ssalbdivad
ssalbdivad•5d ago
You can use a regex literal: https://arktype.io/docs/primitives#string-patterns You can also use the builtin string.hex keyword. If you want narrowed type inference, you will have to use .as<0x${string}>() at least until I publish 2.2 which includes regex inference 😊
ArkType
ArkType Docs
TypeScript's 1:1 validator, optimized from editor to runtime
McKamyk
McKamykOP•5d ago
but the regex makes sense
McKamyk
McKamykOP•5d ago
No description
McKamyk
McKamykOP•5d ago
No description
McKamyk
McKamykOP•5d ago
I like this better

Did you find this page helpful?