colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
32.66k stars 1.13k forks source link

Documentation: More specific on use of validator.js #1018

Closed llermaly closed 2 years ago

llermaly commented 2 years ago

I have an application using zod but I'd like to use some methods from a different library (validator.js) zod documentation says:

Check out validator.js for a bunch of other useful string validation functions.

Not sure if that means this functions are implemented on zod, or I have to also install validator.js, in that other case how I can use both libraries together? cant find any example.

I got this answer from stackoverflow, is this the way you refer in your docs?

import { z } from "zod";
import isCreditCard  from "validator/lib/isCreditCard";

const userSchema = z.object({
  name: z.string(),
  creditCard: z.string().refine(isCreditCard, {
    message: 'Must be a valid credit card number'
  }),
})

Thanks!

scotttrinh commented 2 years ago

That works! Really, any so-called "predicate function" (a function that takes an input and returns true or false) works here and validator.js just happens to be a library full of such functions.

Going to close for now, but feel free to follow-up on this issue with any questions or problems you might run into.

jbogheand commented 5 months ago

I think it would still be useful to clarify if the validator.js are already implemented in zod or not. Asking because I can see the TS intellisense showing something like that for zod.string()

image

It is very clear how any "predicate function" can be used, but it is not clear if validator.js is already implemented in zod.

scotttrinh commented 5 months ago

it is not clear if validator.js is already implemented in zod.

Validator.js is unrelated to Zod, and is just our recommendation. Hopefully that's clear from the fact we're linking out to them rather than documenting some alternative API like you're showing in your screenshot. To be clear, this is what you'd do:

import { z } from "zod";
import validator from "validator";

const url = z.string().refine(validator.isURL);

Open to suggestion on how to make the documentation more clear.

jbogheand commented 5 months ago

Check out validator.js for a bunch of other useful string validation functions that can be used in conjunction with Refinements.

I think a more specific mention this is only an external library would have been more clear to me.

Check out validator.js for a bunch of other useful string validation functions that can be used in conjunction with Refinements (validator.js is an external library consisting of string validation functions which can be passed as arguments to our Refinements functions).

Thank you very much for addressing my issue so promptly and thank you for your work.