Skip to content

Commit d1202b8

Browse files
Shuunenjordan-boyer
authored andcommitted
chore: update README and user schemas to include phone number with default value
1 parent ed6b68d commit d1202b8

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,39 @@ At the time of writing, Node 22 is around 2 times slower than Bun v1 ^^'
2929

3030
Here are the library used in this comparison :
3131

32-
- [arktype](https://github.com/arktypeio/arktype) v2.0.4 from David Blass
33-
- [valibot](https://github.com/fabian-hiller/valibot) v1.0.0-rc.1 from Fabian Hiller
34-
- [zod](https://github.com/colinhacks/zod) v3.24.2 from Colin McDonnell
32+
- [arktype](https://github.com/arktypeio/arktype) from David Blass
33+
- [valibot](https://github.com/fabian-hiller/valibot) from Fabian Hiller
34+
- [zod](https://github.com/colinhacks/zod) from Colin McDonnell
3535

36-
| Date | Score | Library | Deps | Size | Light | Input | Throw | Safe | Execution | Fast |
37-
| ---------- | :---: | :-----: | :---: | :----: | :---: | :---: | :---: | :---: | :-------: | :---: |
38-
| 2025-02-21 | 2 | arktype | 2 | 133 KB | 0 | **1** | 0 | **1** | 155 ms | 0 |
39-
| 2025-02-21 | 6 | valibot | **0** | 4 KB | **2** | **1** | **1** | **1** | 74 ms | **1** |
40-
| 2025-02-21 | 4 | zod | **0** | 62 KB | 0 | **1** | **1** | **1** | 75 ms | **1** |
36+
| Date | Score | Library | Deps | Size | Light | Input | Throw | Safe | Execution | Fast | Readability |
37+
| ---------- | :---: | :-----: | :---: | :----: | :---: | :---: | :---: | :---: | :---------: | :---: | :---------: |
38+
| 2025-03-04 | 3 | arktype | 2 | 140 KB | _0_ | **1** | _0_ | **1** | 155 ms_0_ | _0_ | **1** |
39+
| 2025-03-04 | 6 | valibot | **0** | 5 KB | **1** | **1** | **1** | **1** | 74 ms **1** | **1** | _0_ |
40+
| 2025-03-04 | 6 | zod | **0** | 60 KB | _0_ | **1** | **1** | **1** | 75 ms **1** | **1** | **1** |
4141

4242
Legend :
4343

4444
- Deps : the number of dependencies of the library
4545
- Size : the minified build size in bytes of the related file in `src`, run `bun run build` to see by yourself
46-
- Light : 1 point if the build is less than 10 KB, 1 bonus point if it's less than 5 KB
46+
- Light : 1 point if the build is less than 10 KB
4747
- Input : 1 point if the library can see that `age` is optional in the input but not optional in `type User` the output type
4848
- Throw : 1 point if the library have a parse or throw method, useful when we don't want to handle the error cases
4949
- Safe : 1 point if the library have a safe parse method that will not throw and usually return a `Result` type
50-
- Execution : average time in milliseconds to execute the test file with bun, check the `bun run bench` command output
50+
- Execution : 1 point if the average time in milliseconds to execute the test file with bun is under 100ms, check the `bun run bench` command output
5151
- Fast : 1 point if the library execution time is less than 100 ms
52+
- Readability : 1 point if the library is easy to write & read, the syntax need to be intuitive
5253

5354
## My favorite pick
5455

55-
Valibot is my favorite pick because it's fast as Zod but has a lighter impact on the bundle size.
56+
Zod is my favorite pick because it's fast as Valibot but provide a better readability. Ok the build size is bigger but it does not impact the performance.
5657

5758
## Todo
5859

5960
- [ ] vanilla version ?
6061
- [ ] check error messages
6162
- [ ] check custom error messages
6263
- [ ] check custom validation rules
64+
- [ ] check the perf without using `console`
6365

6466
## How to contribute
6567

src/arktype.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { checkUserA, checkUserB, checkUserC } from './utils.ts'
44
const userSchema = type({
55
name: type.string,
66
age: type.number.default(42),
7+
phone: type.string.or(type.number).pipe(phone => typeof phone === 'number' ? phone.toString() : phone).default("123-456-7890"),
78
})
89

910
export type User = typeof userSchema.inferOut
@@ -16,7 +17,7 @@ checkUserA(userA)
1617

1718
function createUser (input: UserInput) {
1819
const user = userSchema(input)
19-
if (user instanceof type.errors) return { name: "Jordan", age: 42 } // cannot use userA here
20+
if (user instanceof type.errors) return userA as User // :'''''(
2021
return user
2122
}
2223

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
type User = {
22
name: string,
33
age?: number,
4+
phone: string
45
}
56

67
export function checkUserA (userA: User) {
@@ -9,8 +10,9 @@ export function checkUserA (userA: User) {
910

1011
export function checkUserB (userB: User) {
1112
console.assert(userB.age === 35, 'userB.age should be 35')
13+
console.assert(userB.phone === "1234567890", 'userB.phone should be 1234567890')
1214
}
1315

1416
export function checkUserC (userC: User) {
1517
console.assert(userC.name === "Jordan", 'userC.name should be Jordan')
16-
}
18+
}

src/valibot.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { number, object, optional, parse, safeParse, string, type InferInput, type InferOutput } from 'valibot'
1+
import { number, union, object, optional, parse, safeParse, string, type InferInput, type InferOutput, pipe, transform } from 'valibot'
22
import { checkUserA, checkUserB, checkUserC } from './utils.ts'
33

44
const userSchema = object({
55
name: string(),
66
age: optional(number(), 42),
7+
phone: pipe(
8+
optional(union([string(), number()]), "123-456-7890"),
9+
transform((phone) => typeof phone === 'number' ? phone.toString() : phone)
10+
),
711
})
812

913
export type User = InferOutput<typeof userSchema>
@@ -19,7 +23,7 @@ function createUser (input: UserInput) {
1923
return result.output
2024
}
2125

22-
const userB = createUser({ name: "Romain", age: 35 })
26+
const userB = createUser({ name: "Romain", age: 35, phone: 1234567890 })
2327
checkUserB(userB)
2428

2529
// @ts-expect-error age should be a number

src/zod.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { z } from "zod"
1+
import { string, number, object, type infer as InferOutput, type input as InferInput } from "zod"
22
import { checkUserA, checkUserB, checkUserC } from './utils.ts'
33

4-
const userSchema = z.object({
5-
name: z.string(),
6-
age: z.number().default(42),
4+
const userSchema = object({
5+
name: string(),
6+
age: number().default(42),
7+
phone: string().or(number()).default("123-456-7890").transform(phone => typeof phone === 'number' ? phone.toString() : phone),
78
})
89

9-
export type User = z.infer<typeof userSchema>
10+
export type User = InferOutput<typeof userSchema>
1011

11-
export type UserInput = z.input<typeof userSchema>
12+
export type UserInput = InferInput<typeof userSchema>
1213

1314
const userA = userSchema.parse({ name: "Jordan" })
1415
checkUserA(userA)
@@ -19,7 +20,7 @@ function createUser (input: UserInput) {
1920
return result.data
2021
}
2122

22-
const userB = createUser({ name: "Romain", age: 35 })
23+
const userB = createUser({ name: "Romain", age: 35, phone: 1234567890 })
2324
checkUserB(userB)
2425

2526
// @ts-expect-error age should be a number

0 commit comments

Comments
 (0)