Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VALIDATOR: v2.0.0 #22

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 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.15",
"version": "1.0.16",
"description": "Javascript (and Typescript) valibot number schemas",
"author": "Cristobal Contreras Rubio",
"license": "MIT",
Expand Down
26 changes: 24 additions & 2 deletions schemas/valibot/numbers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
)
Expand All @@ -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}`)
)
Expand Down
47 changes: 30 additions & 17 deletions schemas/valibot/numbers/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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<typeof v.safeParse>
// 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')
})
})
})

Expand Down
5 changes: 3 additions & 2 deletions schemas/valibot/numbers/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ export default defineConfig({
exclude: [
'**/*/constants.ts',
'**/*/types.ts',
'lib/**/*',
'dist/**/*'
'lib/*',
'dist/*',
'*.config.ts'
]
}
}
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.6",
"version": "1.0.7",
"description": "Javascript (and Typescript) zod number schemas",
"author": "Cristobal Contreras Rubio",
"license": "MIT",
Expand Down
26 changes: 24 additions & 2 deletions schemas/zod/numbers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,37 @@ 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<typeof Float32Schema>
// eslint-disable-next-line
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<typeof Float64Schema>
Expand Down
48 changes: 30 additions & 18 deletions schemas/zod/numbers/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -118,23 +118,35 @@ describe('Integers', () => {

describe('Floats', () => {
test('Float32', () => {
let value: number
let result: ReturnType<typeof Float32Schema.safeParse>
// 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')
})
})
})

Expand Down
3 changes: 2 additions & 1 deletion schemas/zod/numbers/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default defineConfig({
'**/*/constants.ts',
'**/*/types.ts',
'lib/**/*',
'dist/**/*'
'dist/**/*',
'*.config.ts'
]
}
}
Expand Down
14 changes: 10 additions & 4 deletions validator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ It is a tiny API based on Zod API + `is` Valibot function API.
`safeParse` return

```typescript
interface SafeParse<T> {
success: boolean
data?: T
errors?: string[]
interface SafeParseSuccess<T> {
success: true
value: T
}

interface SafeParseFailure {
success: false
errors: string[]
}

export type SafeParse<T> = SafeParseSuccess<T> | SafeParseFailure
```
20 changes: 10 additions & 10 deletions validator/package-lock.json

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

Loading