Skip to content

Commit

Permalink
X-NUMBERS: patch float checking
Browse files Browse the repository at this point in the history
X-NUMBERS: patch float checking
  • Loading branch information
crisconru authored Sep 12, 2024
2 parents 45195b0 + eb725f0 commit b427b8d
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions schemas/valibot/numbers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion schemas/valibot/numbers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@schemasjs/valibot-numbers",
"version": "1.0.14",
"version": "1.0.15",
"description": "Javascript (and Typescript) valibot number schemas",
"author": "Cristobal Contreras Rubio",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions schemas/valibot/numbers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const FLOAT32_MIN = -3.4e38
export const FLOAT32_MAX = -FLOAT32_MIN
export const Float32Schema = v.pipe(
v.number(),
v.check((num: number) => Float32Array.from([num]).at(0) === num, 'Invalid number, it is not a Float32 number')
v.check((num: number) => Math.abs(Float32Array.from([num])[0] - num) < Number.EPSILON, 'Invalid number, it is not a Float32 number')
// v.minValue(FLOAT32_MIN, `It should be greater than or equal to ${FLOAT32_MIN}`),
// v.maxValue(FLOAT32_MAX, `It should be less than or equal to ${FLOAT32_MAX}`)
)
Expand All @@ -88,7 +88,7 @@ export const FLOAT64_MIN = -1 * Number.MAX_VALUE
export const FLOAT64_MAX = -FLOAT64_MIN
export const Float64Schema = v.pipe(
v.number(),
v.check((num: number) => Float64Array.from([num]).at(0) === num, 'Invalid number, it is not a Float64 number')
v.check((num: number) => Math.abs(Float64Array.from([num])[0] - num) < Number.EPSILON, 'Invalid number, it is not a Float64 number')
// v.minValue(FLOAT64_MIN, `It should be greater than or equal to ${FLOAT64_MIN}`),
// v.maxValue(FLOAT64_MAX, `It should be less than or equal to ${FLOAT64_MAX}`)
)
Expand Down
3 changes: 2 additions & 1 deletion schemas/valibot/numbers/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ describe('Floats', () => {
let value: number
let result: ReturnType<typeof v.safeParse>
// Less than min
value = FLOAT32_MIN - 1.1e38
// value = FLOAT32_MIN - 1.1e38
value = FLOAT32_MIN
result = v.safeParse(Float32Schema, value)
expect(result.success).toBeFalsy()
if(result.issues)
Expand Down
4 changes: 2 additions & 2 deletions schemas/zod/numbers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion schemas/zod/numbers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@schemasjs/zod-numbers",
"version": "1.0.5",
"version": "1.0.6",
"description": "Javascript (and Typescript) zod number schemas",
"author": "Cristobal Contreras Rubio",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions schemas/zod/numbers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ export const NumberSchema = z.number()
export const FLOAT32_MIN = -3.4e38
export const FLOAT32_MAX = -FLOAT32_MIN
export const Float32Schema = NumberSchema
.refine(num => Float32Array.from([num]).at(0) === num, { message: 'Invalid number, it is not a Float32 number' })
.refine((num: number) => Math.abs(Float32Array.from([num])[0] - num) < Number.EPSILON, { message: 'Invalid number, it is not a Float32 number' })
// .min(FLOAT32_MIN, `It should be greater than or equal to ${FLOAT32_MIN}`)
// .max(FLOAT32_MAX, `It should be less than or equal to ${FLOAT32_MAX}`)
export type Float32 = z.infer<typeof Float32Schema>
// eslint-disable-next-line
export const FLOAT64_MIN = -1.8e308
export const FLOAT64_MAX = -FLOAT64_MIN
export const Float64Schema = NumberSchema
.refine(num => Float64Array.from([num]).at(0) === num, { message: 'Invalid number, it is not a Float64 number' })
.refine((num: number) => Math.abs(Float64Array.from([num])[0] - num) < Number.EPSILON, { message: 'Invalid number, it is not a Float64 number' })
// .min(FLOAT64_MIN, `It should be greater than or equal to ${FLOAT64_MIN}`)
// .max(FLOAT64_MAX, `It should be less than or equal to ${FLOAT64_MAX}`)
export type Float64 = z.infer<typeof Float64Schema>
Expand Down
3 changes: 2 additions & 1 deletion schemas/zod/numbers/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ describe('Floats', () => {
let value: number
let result: ReturnType<typeof Float32Schema.safeParse>
// Less than min
value = FLOAT32_MIN - 1.1e38
// value = FLOAT32_MIN - 1.1e38
value = FLOAT32_MIN
result = Float32Schema.safeParse(value)
expect(result.success).toBeFalsy()
if(!result.success)
Expand Down

0 comments on commit b427b8d

Please sign in to comment.