fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖

Home Page:https://valibot.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Derived Schema Validations?

jeremy-code opened this issue · comments

I apologize if the the title doesn't make sense, here is an example of what I am referring to:

I am attempting to validate that a string is of the correct CIDR notation, or of format ip/prefix. Ideally, I would like to be able to use the existing ip() validation since IP validation is complicated (hence, why I am not simply using one custom() validation).

The following code sample works, albeit is a bit verbose.

const IpSuffix = coerce(number([integer(), minValue(0), maxValue(128)]), Number);

const Ip = transform(
  coerce(tuple([string([ip()]), IpPrefixSuffix]), (i) => `${i}`.split("/")),
  ([ip, suffix]) => `${ip}/${suffix}`
);

My question is: would it be possible to validate on something derived from the schema, rather than having to coerce it to a different form and then convert it back? It would be convenient if transform(coerce(derivedSchemaValidations, convert to Derived Schema), convert back to original) could somehow be simplified.

I apologize if I am not making any sense, let me know if you have any questions.

I think I would use custom or special in this case. We export each regex so that you can use our IPv4 and IPv6 regex within custom or special.