zod icon indicating copy to clipboard operation
zod copied to clipboard

add checks @ `ZodBigInt`.

Open igalklebanov opened this issue 2 years ago • 1 comments

Hey 👋

This PR adds .gt, .gte, .lt, .lte, .positive, .negative, .nonpositive, .nonnegative, .gigantic (.unsafe), .safe & .multipleOf to ZodBigInt.

closes #1592.

igalklebanov avatar Dec 16 '22 23:12 igalklebanov

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
Latest commit c465cb4848802e7d6fe3ee33f1647d6eb1264fe8
Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/63d7034736fce70009f36b09
Deploy Preview https://deploy-preview-1711--guileless-rolypoly-866f8a.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

netlify[bot] avatar Dec 16 '22 23:12 netlify[bot]

Thanks Igal! I like a lot of this but I made some changes.

I consolidated ZodBigIntTooSmallIssue into ZodTooSmallIssue by converting minimum: number to minimum: number | bigint. I appreciate the effort to avoid breaking changes on the existing issue types, but I'd rather keep too_big/too_small/etc consolidated into a single issue type.

I got rid of all the .safe()/.unsafe() stuff, since it doesn't apply to bigints - the point of using a bigint is to not worry about being inside the safe integer range. I'd accept a PR to add .safe() to ZodNumber where it'd have more utility. But .gigantic/.unsafe seem unnecessary to me.

colinhacks avatar Dec 24 '22 08:12 colinhacks

Thanks Colin! ⚡

I got rid of all the .safe()/.unsafe() stuff, since it doesn't apply to bigints - the point of using a bigint is to not worry about being inside the safe integer range. I'd accept a PR to add .safe() to ZodNumber where it'd have more utility. But .gigantic/.unsafe seem unnecessary to me.

The use case I was thinking about was a 3rd party library, e.g. a database driver, returning bigint values, but zod consumer knows the underlying value should be in the safe integer range (select count(*) as friends from people left join friendships group by people.id - friends column might return as bigint, but although there's lots of people, most people don't have that many friends). So a helper such as ZodBigInt.safe() allows to describe this explictly and chain with .transform(Number) knowing the parsed underlying value is unchanged.

I'll add a separate PR for ZodNumber.safe() as js behavior with anything beyond Number.MAX_SAFE_INTEGER behaves weirdly.

Done #1753.

igalklebanov avatar Dec 24 '22 14:12 igalklebanov

The use case I was thinking about was a 3rd party library, e.g. a database driver, returning bigint values, but zod consumer knows the underlying value should be in the safe integer range (select count(*) as friends from people left join friendships group by people.id - friends column might return as bigint, but although there's lots of people, most people don't have that many friends). So a helper such as ZodBigInt.safe() allows to describe this explictly and chain with .transform(Number) knowing the parsed underlying value is unchanged.

This makes a lot of sense, sorry.

colinhacks avatar Feb 27 '23 00:02 colinhacks