Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derived Schema Validations? #459

Closed
jeremy-code opened this issue Feb 27, 2024 · 1 comment
Closed

Derived Schema Validations? #459

jeremy-code opened this issue Feb 27, 2024 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@jeremy-code
Copy link

jeremy-code commented Feb 27, 2024

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.

@fabian-hiller
Copy link
Owner

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.

@fabian-hiller fabian-hiller self-assigned this Feb 28, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants