From c7d474566a1c6f1a8d2fa903aa30e58b3eeb1afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crist=C3=B3bal=20Contreras=20Rubio?= Date: Fri, 13 Sep 2024 07:57:34 +0200 Subject: [PATCH 1/4] VALIBOT-NUMBERS: v1.0.16 patched and tested floats checking --- schemas/valibot/numbers/package-lock.json | 4 +- schemas/valibot/numbers/package.json | 2 +- schemas/valibot/numbers/src/index.ts | 26 +++++++++++- schemas/valibot/numbers/tests/index.test.ts | 47 +++++++++++++-------- schemas/valibot/numbers/vitest.config.ts | 5 ++- 5 files changed, 60 insertions(+), 24 deletions(-) diff --git a/schemas/valibot/numbers/package-lock.json b/schemas/valibot/numbers/package-lock.json index 5dbc2e2..9313568 100644 --- a/schemas/valibot/numbers/package-lock.json +++ b/schemas/valibot/numbers/package-lock.json @@ -1,12 +1,12 @@ { "name": "@schemasjs/valibot-numbers", - "version": "1.0.15", + "version": "1.0.16", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@schemasjs/valibot-numbers", - "version": "1.0.15", + "version": "1.0.16", "license": "MIT", "peerDependencies": { "valibot": ">0.31.0" diff --git a/schemas/valibot/numbers/package.json b/schemas/valibot/numbers/package.json index 6552154..725a98f 100644 --- a/schemas/valibot/numbers/package.json +++ b/schemas/valibot/numbers/package.json @@ -1,6 +1,6 @@ { "name": "@schemasjs/valibot-numbers", - "version": "1.0.15", + "version": "1.0.16", "description": "Javascript (and Typescript) valibot number schemas", "author": "Cristobal Contreras Rubio", "license": "MIT", diff --git a/schemas/valibot/numbers/src/index.ts b/schemas/valibot/numbers/src/index.ts index fef2835..1ae1d35 100644 --- a/schemas/valibot/numbers/src/index.ts +++ b/schemas/valibot/numbers/src/index.ts @@ -77,7 +77,18 @@ export const FLOAT32_MIN = -3.4e38 export const FLOAT32_MAX = -FLOAT32_MIN export const Float32Schema = v.pipe( v.number(), - v.check((num: number) => Math.abs(Float32Array.from([num])[0] - num) < Number.EPSILON, 'Invalid number, it is not a Float32 number') + v.check((num: number) => { + const arr = Float32Array.from([num])[0] + const diff = Math.abs(arr - num) + const error = Math.abs(num / 10) + // console.log(`Float32Schema + // num -> ${num} + // arr -> ${arr} + // dif -> ${diff} + // err -> ${error} + // `) + return diff < error + }, '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}`) ) @@ -88,7 +99,18 @@ 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) => Math.abs(Float64Array.from([num])[0] - num) < Number.EPSILON, 'Invalid number, it is not a Float64 number') + v.check((num: number) => { + const arr = Float64Array.from([num])[0] + const diff = Math.abs(arr - num) + const error = Math.abs(num / 10) + // console.log(`Float32Schema + // num -> ${num} + // arr -> ${arr} + // dif -> ${diff} + // err -> ${error} + // `) + return diff < error + }, '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}`) ) diff --git a/schemas/valibot/numbers/tests/index.test.ts b/schemas/valibot/numbers/tests/index.test.ts index 30da447..d172a48 100644 --- a/schemas/valibot/numbers/tests/index.test.ts +++ b/schemas/valibot/numbers/tests/index.test.ts @@ -1,6 +1,6 @@ import * as v from 'valibot' import { describe, test, expect } from 'vitest' -import { BigUintSchema, FLOAT32_MAX, FLOAT32_MIN, Float32Schema, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT8_MAX, INT8_MIN, Int16ArraySchema, Int16ArrayTransform, Int16Schema, Int32ArraySchema, Int32ArrayTransform, Int32Schema, Int8ArraySchema, Int8ArrayTransform, Int8Schema, UINT16_MAX, UINT32_MAX, UINT8_MAX, UINT_MIN, Uint16ArraySchema, Uint16ArrayTransform, Uint16Schema, Uint32ArraySchema, Uint32ArrayTransform, Uint32Schema, Uint8ArraySchema, Uint8ArrayTransform, Uint8Schema, passToArray } from '../src' +import { BigUintSchema, FLOAT32_MAX, FLOAT32_MIN, Float32Schema, Float64Schema, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT8_MAX, INT8_MIN, Int16ArraySchema, Int16ArrayTransform, Int16Schema, Int32ArraySchema, Int32ArrayTransform, Int32Schema, Int8ArraySchema, Int8ArrayTransform, Int8Schema, UINT16_MAX, UINT32_MAX, UINT8_MAX, UINT_MIN, Uint16ArraySchema, Uint16ArrayTransform, Uint16Schema, Uint32ArraySchema, Uint32ArrayTransform, Uint32Schema, Uint8ArraySchema, Uint8ArrayTransform, Uint8Schema, passToArray } from '../src' describe('Unsigned Integers', () => { test('Uint8', () => { @@ -119,23 +119,36 @@ describe('Integers', () => { describe('Floats', () => { test('Float32', () => { - let value: number + // Error + [FLOAT32_MIN - 1.1e38, FLOAT32_MAX + 1.1e38] + .forEach(value => { + const result = v.safeParse(Float32Schema, value) + expect(result.success).toBeFalsy() + if(result.issues) + // expect(result.issues[0].message).toBe(`It should be greater than or equal to ${FLOAT32_MIN}`) + expect(result.issues[0].message).toBe('Invalid number, it is not a Float32 number') + }); + // OK + [FLOAT32_MIN, FLOAT32_MAX] + .forEach(value => { + const result = v.safeParse(Float32Schema, value) + expect(result.success).toBeTruthy() + if(result.success) + // expect(result).toHaveProperty('output') + expect(result.output).toBeTypeOf('number') + }) + }) + + test('Float64', () => { let result: ReturnType - // Less than min - // value = FLOAT32_MIN - 1.1e38 - value = FLOAT32_MIN - result = v.safeParse(Float32Schema, value) - expect(result.success).toBeFalsy() - if(result.issues) - // expect(result.issues[0].message).toBe(`It should be greater than or equal to ${FLOAT32_MIN}`) - expect(result.issues[0].message).toBe('Invalid number, it is not a Float32 number') - // More than max - value = FLOAT32_MAX + 1.1e38 - result = v.safeParse(Float32Schema, value) - expect(result.success).toBeFalsy() - if(result.issues) - expect(result.issues[0].message).toBe('Invalid number, it is not a Float32 number') - // expect(result.issues[0].message).toBe(`It should be less than or equal to ${FLOAT32_MAX}`) + [FLOAT32_MIN - 1.1e38, FLOAT32_MAX + 1.1e38, FLOAT32_MIN, FLOAT32_MAX] + .forEach(value => { + result = v.safeParse(Float64Schema, value) + expect(result.success).toBeTruthy() + if(result.success) + // expect(result).toHaveProperty('output') + expect(result.output).toBeTypeOf('number') + }) }) }) diff --git a/schemas/valibot/numbers/vitest.config.ts b/schemas/valibot/numbers/vitest.config.ts index c25db75..6ed49c8 100644 --- a/schemas/valibot/numbers/vitest.config.ts +++ b/schemas/valibot/numbers/vitest.config.ts @@ -7,8 +7,9 @@ export default defineConfig({ exclude: [ '**/*/constants.ts', '**/*/types.ts', - 'lib/**/*', - 'dist/**/*' + 'lib/*', + 'dist/*', + '*.config.ts' ] } } From 4550cfaea7c6844d50c20ecc3e99145ed8b63707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crist=C3=B3bal=20Contreras=20Rubio?= Date: Fri, 13 Sep 2024 07:58:14 +0200 Subject: [PATCH 2/4] ZOD-NUMBERS: v1.0.7 patched and tested floats checking --- schemas/zod/numbers/package-lock.json | 4 +-- schemas/zod/numbers/package.json | 2 +- schemas/zod/numbers/src/index.ts | 26 ++++++++++++-- schemas/zod/numbers/tests/index.test.ts | 48 +++++++++++++++---------- schemas/zod/numbers/vitest.config.ts | 3 +- 5 files changed, 59 insertions(+), 24 deletions(-) diff --git a/schemas/zod/numbers/package-lock.json b/schemas/zod/numbers/package-lock.json index 0628076..88baaaa 100644 --- a/schemas/zod/numbers/package-lock.json +++ b/schemas/zod/numbers/package-lock.json @@ -1,12 +1,12 @@ { "name": "@schemasjs/zod-numbers", - "version": "1.0.6", + "version": "1.0.7", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@schemasjs/zod-numbers", - "version": "1.0.6", + "version": "1.0.7", "license": "MIT", "peerDependencies": { "zod": "^3.23.8" diff --git a/schemas/zod/numbers/package.json b/schemas/zod/numbers/package.json index 4f12599..68b9ec8 100644 --- a/schemas/zod/numbers/package.json +++ b/schemas/zod/numbers/package.json @@ -1,6 +1,6 @@ { "name": "@schemasjs/zod-numbers", - "version": "1.0.6", + "version": "1.0.7", "description": "Javascript (and Typescript) zod number schemas", "author": "Cristobal Contreras Rubio", "license": "MIT", diff --git a/schemas/zod/numbers/src/index.ts b/schemas/zod/numbers/src/index.ts index 2c76afd..0c3d959 100644 --- a/schemas/zod/numbers/src/index.ts +++ b/schemas/zod/numbers/src/index.ts @@ -54,7 +54,18 @@ export const NumberSchema = z.number() export const FLOAT32_MIN = -3.4e38 export const FLOAT32_MAX = -FLOAT32_MIN export const Float32Schema = NumberSchema - .refine((num: number) => Math.abs(Float32Array.from([num])[0] - num) < Number.EPSILON, { message: 'Invalid number, it is not a Float32 number' }) + .refine((num: number) => { + const arr = Float32Array.from([num])[0] + const diff = Math.abs(arr - num) + const error = Math.abs(num / 10) + // console.log(`Float32Schema + // num -> ${num} + // arr -> ${arr} + // dif -> ${diff} + // err -> ${error} + // `) + return diff < error + }, { 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 @@ -62,7 +73,18 @@ export type Float32 = z.infer export const FLOAT64_MIN = -1.8e308 export const FLOAT64_MAX = -FLOAT64_MIN export const Float64Schema = NumberSchema - .refine((num: number) => Math.abs(Float64Array.from([num])[0] - num) < Number.EPSILON, { message: 'Invalid number, it is not a Float64 number' }) + .refine((num: number) => { + const arr = Float64Array.from([num])[0] + const diff = Math.abs(arr - num) + const error = Math.abs(num / 10) + // console.log(`Float32Schema + // num -> ${num} + // arr -> ${arr} + // dif -> ${diff} + // err -> ${error} + // `) + return diff < error + }, { 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 diff --git a/schemas/zod/numbers/tests/index.test.ts b/schemas/zod/numbers/tests/index.test.ts index 73c5a1d..48e3b4c 100644 --- a/schemas/zod/numbers/tests/index.test.ts +++ b/schemas/zod/numbers/tests/index.test.ts @@ -1,5 +1,5 @@ import { describe, test, expect } from 'vitest' -import { BigUintSchema, FLOAT32_MAX, FLOAT32_MIN, Float32Schema, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT8_MAX, INT8_MIN, Int16ArraySchema, Int16ArrayTransform, Int16Schema, Int32ArraySchema, Int32ArrayTransform, Int32Schema, Int8ArraySchema, Int8ArrayTransform, Int8Schema, UINT16_MAX, UINT32_MAX, UINT8_MAX, UINT_MIN, Uint16ArraySchema, Uint16ArrayTransform, Uint16Schema, Uint32ArraySchema, Uint32ArrayTransform, Uint32Schema, Uint8ArraySchema, Uint8ArrayTransform, Uint8Schema, passToArray } from '../src' +import { BigUintSchema, FLOAT32_MAX, FLOAT32_MIN, Float32Schema, Float64Schema, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT8_MAX, INT8_MIN, Int16ArraySchema, Int16ArrayTransform, Int16Schema, Int32ArraySchema, Int32ArrayTransform, Int32Schema, Int8ArraySchema, Int8ArrayTransform, Int8Schema, UINT16_MAX, UINT32_MAX, UINT8_MAX, UINT_MIN, Uint16ArraySchema, Uint16ArrayTransform, Uint16Schema, Uint32ArraySchema, Uint32ArrayTransform, Uint32Schema, Uint8ArraySchema, Uint8ArrayTransform, Uint8Schema, passToArray } from '../src' describe('Unsigned Integers', () => { test('Uint8', () => { @@ -118,23 +118,35 @@ describe('Integers', () => { describe('Floats', () => { test('Float32', () => { - let value: number - let result: ReturnType - // Less than min - // value = FLOAT32_MIN - 1.1e38 - value = FLOAT32_MIN - result = Float32Schema.safeParse(value) - expect(result.success).toBeFalsy() - if(!result.success) - expect(result.error.issues[0].message).toBe('Invalid number, it is not a Float32 number') - // expect(result.error.issues[0].message).toBe(`It should be greater than or equal to ${FLOAT32_MIN}`) - // More than max - value = FLOAT32_MAX + 1.1e38 - result = Float32Schema.safeParse(value) - expect(result.success).toBeFalsy() - if(!result.success) - expect(result.error.issues[0].message).toBe('Invalid number, it is not a Float32 number') - // expect(result.error.issues[0].message).toBe(`It should be less than or equal to ${FLOAT32_MAX}`) + // Error + [FLOAT32_MIN - 1.1e38, FLOAT32_MAX + 1.1e38] + .forEach(value => { + const result = Float32Schema.safeParse(value) + expect(result.success).toBeFalsy() + if(result.error) + // expect(result.issues[0].message).toBe(`It should be greater than or equal to ${FLOAT32_MIN}`) + expect(result.error.issues[0].message).toBe('Invalid number, it is not a Float32 number') + }); + // OK + [FLOAT32_MIN, FLOAT32_MAX] + .forEach(value => { + const result = Float32Schema.safeParse(value) + expect(result.success).toBeTruthy() + if(result.success) + // expect(result).toHaveProperty('output') + expect(result.data).toBeTypeOf('number') + }) + }) + + test('Float64', () => { + [FLOAT32_MIN - 1.1e38, FLOAT32_MAX + 1.1e38, FLOAT32_MIN, FLOAT32_MAX] + .forEach(value => { + const result = Float64Schema.safeParse(value) + expect(result.success).toBeTruthy() + if(result.success) + // expect(result).toHaveProperty('output') + expect(result.data).toBeTypeOf('number') + }) }) }) diff --git a/schemas/zod/numbers/vitest.config.ts b/schemas/zod/numbers/vitest.config.ts index c25db75..ea19530 100644 --- a/schemas/zod/numbers/vitest.config.ts +++ b/schemas/zod/numbers/vitest.config.ts @@ -8,7 +8,8 @@ export default defineConfig({ '**/*/constants.ts', '**/*/types.ts', 'lib/**/*', - 'dist/**/*' + 'dist/**/*', + '*.config.ts' ] } } From 2a37369b270a3006e5e02d0949c0bae98d990a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crist=C3=B3bal=20Contreras=20Rubio?= Date: Fri, 13 Sep 2024 09:30:45 +0200 Subject: [PATCH 3/4] VALIDATOR: v2.0.0 update safeParse API and deps --- validator/README.md | 14 ++-- validator/package-lock.json | 20 +++--- validator/package.json | 6 +- validator/src/index.ts | 6 +- validator/tests/index.test.ts | 116 ++++++++++++++++++++++++---------- validator/vitest.config.ts | 3 +- 6 files changed, 109 insertions(+), 56 deletions(-) diff --git a/validator/README.md b/validator/README.md index f054df3..49a4b82 100644 --- a/validator/README.md +++ b/validator/README.md @@ -67,9 +67,15 @@ It is a tiny API based on Zod API + `is` Valibot function API. `safeParse` return ```typescript -interface SafeParse { - success: boolean - data?: T - errors?: string[] +interface SafeParseSuccess { + success: true + value: T } + +interface SafeParseFailure { + success: false + errors: string[] +} + +export type SafeParse = SafeParseSuccess | SafeParseFailure ``` diff --git a/validator/package-lock.json b/validator/package-lock.json index f71d24c..fdddd09 100644 --- a/validator/package-lock.json +++ b/validator/package-lock.json @@ -1,16 +1,16 @@ { "name": "@schemasjs/validator", - "version": "1.0.2", + "version": "2.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@schemasjs/validator", - "version": "1.0.2", + "version": "2.0.0", "license": "MIT", "devDependencies": { - "@schemasjs/valibot-numbers": "^1.0.11", - "@schemasjs/zod-numbers": "^1.0.4" + "@schemasjs/valibot-numbers": "^1.0.15", + "@schemasjs/zod-numbers": "^1.0.6" }, "optionalDependencies": { "valibot": ">0.31.0", @@ -18,9 +18,9 @@ } }, "node_modules/@schemasjs/valibot-numbers": { - "version": "1.0.13", - "resolved": "https://registry.npmjs.org/@schemasjs/valibot-numbers/-/valibot-numbers-1.0.13.tgz", - "integrity": "sha512-rQ+dgwgpcZH1uapYhVIcQNJgCkM82FkGmf5aoo0oTnDSxSyn/e+ZYgyNEaviZbN0ebEA2vMQtXYycRmbT895iQ==", + "version": "1.0.15", + "resolved": "https://registry.npmjs.org/@schemasjs/valibot-numbers/-/valibot-numbers-1.0.15.tgz", + "integrity": "sha512-hmbAlpNyKNrXyspyA5d2T+B1oK4q5gT88I8+z7iwqW4ULH/UcNsmdS1whe2VXbQvuVR57Up9Xr6hoVTPXrIszg==", "dev": true, "license": "MIT", "peerDependencies": { @@ -28,9 +28,9 @@ } }, "node_modules/@schemasjs/zod-numbers": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/@schemasjs/zod-numbers/-/zod-numbers-1.0.4.tgz", - "integrity": "sha512-7oXTGJI1MAwxeSS6dfudn72Ietb7RhHvMbcyHcG5DPy8Zl+y0sl3UUZkfBx1prXZ4n8bodispE77mjHU20AGFA==", + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/@schemasjs/zod-numbers/-/zod-numbers-1.0.6.tgz", + "integrity": "sha512-otin4oHrVHWl4fyPcNy1Dvk2UGjfulG6r532NqsyJXJSdJhW57WNPj5EpJJ7BMv+SV8V8wmRV19YyZdwDlHuCw==", "dev": true, "license": "MIT", "peerDependencies": { diff --git a/validator/package.json b/validator/package.json index 486d028..1388018 100644 --- a/validator/package.json +++ b/validator/package.json @@ -1,6 +1,6 @@ { "name": "@schemasjs/validator", - "version": "1.0.2", + "version": "2.0.0", "description": "An opinionated type validator API indpendent from schema validators like Zod, Valibot, etc", "author": "Cristobal Contreras Rubio", "license": "MIT", @@ -56,8 +56,8 @@ "zod": "^3.23.8" }, "devDependencies": { - "@schemasjs/valibot-numbers": "^1.0.11", - "@schemasjs/zod-numbers": "^1.0.4" + "@schemasjs/valibot-numbers": "^1.0.15", + "@schemasjs/zod-numbers": "^1.0.6" }, "ts-standard": { "ignore": [ diff --git a/validator/src/index.ts b/validator/src/index.ts index 0c31fb5..51104e2 100644 --- a/validator/src/index.ts +++ b/validator/src/index.ts @@ -3,7 +3,7 @@ import { z } from 'zod' interface SafeParseSuccess { success: true - data: T + value: T } interface SafeParseFailure { @@ -24,7 +24,7 @@ export const ZodValidator = (schema: z.ZodSchema): Schema => ({ parse: (data: any) => schema.parse(data), safeParse: (data: any) => { const parsed = schema.safeParse(data) - if (parsed.success) return { success: parsed.success, data: parsed.data } + if (parsed.success) return { success: parsed.success, value: parsed.data } return { success: parsed.success, errors: [parsed.error.message] } } }) @@ -35,7 +35,7 @@ export const ValibotValidator = (schema: v.BaseSchema): Schema => ({ parse: (data: any) => v.parse(schema, data), safeParse: (data: any) => { const parsed = v.safeParse(schema, data) - if (parsed.success) return { success: parsed.success, data: parsed.output } + if (parsed.success) return { success: parsed.success, value: parsed.output } return { success: parsed.success, errors: parsed.issues.map(issue => issue.message) } } }) diff --git a/validator/tests/index.test.ts b/validator/tests/index.test.ts index d31590f..bc7d9a3 100644 --- a/validator/tests/index.test.ts +++ b/validator/tests/index.test.ts @@ -1,62 +1,108 @@ import { describe, expect, test } from 'vitest' -import * as v from 'valibot' -import { z } from 'zod' -import { type Uint8 as Uint8Valibot, Uint8Schema as Uint8ValibotSchema } from '@schemasjs/valibot-numbers' -import { type Uint8 as Uint8Zod, Uint8Schema as Uint8ZodSchema } from '@schemasjs/zod-numbers' +import { Float32Schema as Float32ValibotSchema, type Uint8 as Uint8Valibot, Uint8Schema as Uint8ValibotSchema, type Float32 as Float32Valibot } from '@schemasjs/valibot-numbers' +import { type Uint8 as Uint8Zod, Uint8Schema as Uint8ZodSchema, type Float32 as Float32Zod, Float32Schema as Float32ZodSchema } from '@schemasjs/zod-numbers' import { ValibotValidator, ZodValidator } from '../src' describe('Valibot', () => { const Uint8Schema = ValibotValidator(Uint8ValibotSchema) + const Float32Schema = ValibotValidator(Float32ValibotSchema) test('is', () => { - expect(Uint8Schema.is(8)).toBeTruthy() - expect(Uint8Schema.is(8.9)).toBeFalsy() + // OK + [ + { schema: Uint8Schema, num: 8 }, + { schema: Float32Schema, num: -3.4e38 } + ].forEach(({schema, num}) => expect(schema.is(num)).toBeTruthy()); + // Error + [ + { schema: Uint8Schema, num: -8 }, + { schema: Float32Schema, num: -3.4e39 } + ].forEach(({schema, num}) => expect(schema.is(num)).toBeFalsy()) }) test('parse', () => { - expect(Uint8Schema.parse(8)).toBe(8) - expect(() => Uint8Schema.parse(8.9)).toThrowError() + // OK + [ + { schema: Uint8Schema, num: 8 }, + { schema: Float32Schema, num: -3.4e38 } + ].forEach(({schema, num}) => expect(schema.parse(num)).toBe(num)); + // Error + [ + { schema: Uint8Schema, num: -8 }, + { schema: Float32Schema, num: -3.4e39 } + ].forEach(({schema, num}) => expect(() => schema.parse(num)).toThrowError()) }) test('safeParse', () => { - const good = Uint8Schema.safeParse(8) - expect(good.success).toBeTruthy() - if (good.success) { - expect(good.data).toBe(8) - } - const bad = Uint8Schema.safeParse(-8) - const valibotBad = v.safeParse(Uint8ValibotSchema, -8) - expect(bad.success).toBeFalsy() - expect(bad).toHaveProperty('errors') - // @ts-ignore - bad.errors?.forEach((error: string, idx: number) => expect(error).toBe(valibotBad.issues[idx].message)) + // OK + [ + { good: Uint8Schema.safeParse(8), num: 8 }, + { good: Float32Schema.safeParse(-3.4e38), num: -3.4e38 } + ].forEach(({good, num}) => { + expect(good.success).toBeTruthy() + if (good.success) { + expect(good.value).toBe(num) + } + }); + // Error + [Uint8Schema.safeParse(-8), Float32Schema.safeParse(-3.4e39)].forEach(bad => { + expect(bad.success).toBeFalsy() + expect(bad).toHaveProperty('errors') + if (!bad.success) { + expect(bad.errors.length).toBeGreaterThan(0) + } + }) }) }) -describe('Zod', () => { +describe.skip('Zod', () => { const Uint8Schema = ZodValidator(Uint8ZodSchema) + const Float32Schema = ZodValidator(Float32ZodSchema) test('is', () => { - expect(Uint8Schema.is(8)).toBeTruthy() - expect(Uint8Schema.is(8.9)).toBeFalsy() + // OK + [ + { schema: Uint8Schema, num: 8 }, + { schema: Float32Schema, num: -3.4e38 } + ].forEach(({schema, num}) => expect(schema.is(num)).toBeTruthy()); + // Error + [ + { schema: Uint8Schema, num: -8 }, + { schema: Float32Schema, num: -3.4e39 } + ].forEach(({schema, num}) => expect(schema.is(num)).toBeFalsy()) }) test('parse', () => { - expect(Uint8Schema.parse(8)).toBe(8) - expect(() => Uint8Schema.parse(8.9)).toThrowError() + // OK + [ + { schema: Uint8Schema, num: 8 }, + { schema: Float32Schema, num: -3.4e38 } + ].forEach(({schema, num}) => expect(schema.parse(num)).toBe(num)); + // Error + [ + { schema: Uint8Schema, num: -8 }, + { schema: Float32Schema, num: -3.4e39 } + ].forEach(({schema, num}) => expect(() => schema.parse(num)).toThrowError()) }) test('safeParse', () => { - const good = Uint8Schema.safeParse(8) - expect(good.success).toBeTruthy() - if (good.success) { - expect(good.data).toBe(8) - } - const bad = Uint8Schema.safeParse(-8) - const zodBad = Uint8ZodSchema.safeParse(-8) - expect(bad.success).toBeFalsy() - expect(bad).toHaveProperty('errors') - // @ts-ignore - expect(bad.errors[0]).toBe(zodBad.error?.message) + // OK + [ + { good: Uint8Schema.safeParse(8), num: 8 }, + { good: Float32Schema.safeParse(-3.4e38), num: -3.4e38 } + ].forEach(({good, num}) => { + expect(good.success).toBeTruthy() + if (good.success) { + expect(good.value).toBe(num) + } + }); + // Error + [Uint8Schema.safeParse(-8), Float32Schema.safeParse(-3.4e39)].forEach(bad => { + expect(bad.success).toBeFalsy() + expect(bad).toHaveProperty('errors') + if (!bad.success) { + expect(bad.errors.length).toBeGreaterThan(0) + } + }) }) }) diff --git a/validator/vitest.config.ts b/validator/vitest.config.ts index c25db75..ea19530 100644 --- a/validator/vitest.config.ts +++ b/validator/vitest.config.ts @@ -8,7 +8,8 @@ export default defineConfig({ '**/*/constants.ts', '**/*/types.ts', 'lib/**/*', - 'dist/**/*' + 'dist/**/*', + '*.config.ts' ] } } From 6260514ddd5492d2d9b982dc0762d221b9c7fc7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crist=C3=B3bal=20Contreras=20Rubio?= Date: Fri, 13 Sep 2024 09:30:51 +0200 Subject: [PATCH 4/4] update deps --- package-lock.json | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 26145ed..9987214 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5893,8 +5893,7 @@ "node_modules/valibot": { "version": "0.31.1", "resolved": "https://registry.npmjs.org/valibot/-/valibot-0.31.1.tgz", - "integrity": "sha512-2YYIhPrnVSz/gfT2/iXVTrSj92HwchCt9Cga/6hX4B26iCz9zkIsGTS0HjDYTZfTi1Un0X6aRvhBi1cfqs/i0Q==", - "peer": true + "integrity": "sha512-2YYIhPrnVSz/gfT2/iXVTrSj92HwchCt9Cga/6hX4B26iCz9zkIsGTS0HjDYTZfTi1Un0X6aRvhBi1cfqs/i0Q==" }, "node_modules/vite": { "version": "5.4.4", @@ -6323,7 +6322,7 @@ }, "schemas/valibot/numbers": { "name": "@schemasjs/valibot-numbers", - "version": "1.0.15", + "version": "1.0.16", "license": "MIT", "peerDependencies": { "valibot": ">0.31.0" @@ -6331,7 +6330,7 @@ }, "schemas/zod/numbers": { "name": "@schemasjs/zod-numbers", - "version": "1.0.6", + "version": "1.0.7", "license": "MIT", "peerDependencies": { "zod": "^3.23.8" @@ -6339,22 +6338,16 @@ }, "validator": { "name": "@schemasjs/validator", - "version": "1.0.2", + "version": "2.0.0", "license": "MIT", "devDependencies": { - "@schemasjs/valibot-numbers": "^1.0.11", - "@schemasjs/zod-numbers": "^1.0.4" + "@schemasjs/valibot-numbers": "^1.0.15", + "@schemasjs/zod-numbers": "^1.0.6" }, "optionalDependencies": { "valibot": ">0.31.0", "zod": "^3.23.8" } - }, - "validator/node_modules/valibot": { - "version": "0.32.0", - "resolved": "https://registry.npmjs.org/valibot/-/valibot-0.32.0.tgz", - "integrity": "sha512-FXBnJl4bNOmeg7lQv+jfvo/wADsRBN8e9C3r+O77Re3dEnDma8opp7p4hcIbF7XJJ30h/5SVohdjer17/sHOsQ==", - "optional": true } } }