Skip to content

Commit

Permalink
fix: bigint (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
soc221b authored Jan 3, 2025
1 parent 80308d2 commit 72e00a8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/zod-bigint-faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class ZodBigIntFaker extends ZodTypeFaker<z.ZodBigInt> {
fake(): z.infer<z.ZodBigInt> {
let min: undefined | bigint = undefined
let max: undefined | bigint = undefined
let multipleOf: undefined | bigint = undefined
let multipleOf: bigint = 1n
for (const check of this.schema._def.checks) {
switch (check.kind) {
case 'min':
Expand All @@ -16,31 +16,31 @@ export class ZodBigIntFaker extends ZodTypeFaker<z.ZodBigInt> {
max = check.value - (check.inclusive ? 0n : 1n)
break
case 'multipleOf':
multipleOf = check.value
multipleOf = check.value < 0n ? -check.value : check.value
break
default:
const _: never = check
}
}

// workaround: https://github.com/faker-js/faker/pull/3363
if (min === undefined && max !== undefined && max < 0n) {
min = max - (multipleOf === undefined ? 1_000n : multipleOf * 1_000n)
}

if (multipleOf !== undefined) {
if (min !== undefined && max !== undefined) {
return min + runFake(faker => faker.number.bigInt({ min: 0n, max: (max - min) / multipleOf })) * multipleOf
} else if (min !== undefined) {
return min + runFake(faker => faker.number.bigInt({ min: 0n })) * multipleOf
} else if (max !== undefined) {
return max - runFake(faker => faker.number.bigInt({ min: 0n })) * multipleOf
} else {
return runFake(faker => faker.number.bigInt({ min: 0n })) * multipleOf
}
const largeThanMultipleOf = multipleOf === undefined ? 1000n : multipleOf * 1000n
if (min !== undefined && max !== undefined) {
} else if (min !== undefined) {
max = min + largeThanMultipleOf
} else if (max !== undefined) {
min = max - largeThanMultipleOf
} else {
return runFake(faker => faker.number.bigInt({ min, max }))
min = -largeThanMultipleOf
max = largeThanMultipleOf
}
const data = min + runFake(faker => faker.number.bigInt({ min: 0n, max: (max - min) / multipleOf })) * multipleOf
const remaining = multipleOf - ((data < 0n ? -data : data) % multipleOf)
return data >= 0n
? data + remaining > max
? data - (multipleOf - remaining)
: data + remaining
: data - remaining < min
? data + (multipleOf - remaining)
: data - remaining
}

static create(schema: z.ZodBigInt): ZodBigIntFaker {
Expand Down
43 changes: 43 additions & 0 deletions tests/zod-bigint-faker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,47 @@ describe('edge case', () => {
const data = faker.fake()
expect(schema.safeParse(data).success).toBe(true)
})

test('multiplyOf', () => {
for (let i = 0n; i < 2000n; i++) {
const schema = z
.bigint()
.multipleOf(37n)
.min(i)
.max(i + 37n)
const faker = zodBigIntFaker(schema)
const data = faker.fake()
expect(schema.safeParse(data).success).toBe(true)
}
for (let i = 0n; i > -2000n; i--) {
const schema = z
.bigint()
.multipleOf(37n)
.min(i - 37n)
.max(i)
const faker = zodBigIntFaker(schema)
const data = faker.fake()
expect(schema.safeParse(data).success).toBe(true)
}
for (let i = 0n; i < 2000n; i++) {
const schema = z
.bigint()
.multipleOf(-37n)
.min(i)
.max(i + 37n)
const faker = zodBigIntFaker(schema)
const data = faker.fake()
expect(schema.safeParse(data).success).toBe(true)
}
for (let i = 0n; i > -2000n; i--) {
const schema = z
.bigint()
.multipleOf(-37n)
.min(i - 37n)
.max(i)
const faker = zodBigIntFaker(schema)
const data = faker.fake()
expect(schema.safeParse(data).success).toBe(true)
}
})
})

0 comments on commit 72e00a8

Please sign in to comment.