Skip to content

Commit f04ad02

Browse files
authored
feat: new helpers (#16)
* feat(helper): new `email` helper * up * up * feat(helpers): new `dateAsString` helper * up
1 parent 1f21dfd commit f04ad02

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ yarn add h3-valibot
2121
## Validation
2222

2323
```ts router.ts
24-
import { useValidatedBody, v } from 'h3-valibot'
24+
import { useValidatedBody, v, vh } from 'h3-valibot'
2525

2626
import { createApp, createRouter, eventHandler } from 'h3';
2727
import { email, minLength, string, objectAsync } from 'valibot';
2828

2929
export const app = createApp();
3030
const LoginSchema = v.object({
31-
email: v.pipe(v.string(), v.email()),
31+
email: vh.email,
3232
password: v.pipe(v.string(), v.minLength(8)),
3333
});
3434

@@ -76,8 +76,10 @@ It also provides a set of helpers via `vh` object, mainly related to string vali
7676

7777
- `boolAsString`
7878
- `checkboxAsString`
79+
- `dateAsString`
7980
- `intAsString`
8081
- `numAsString`
82+
- `email`
8183
- `uuid`
8284

8385
For more details or examples please refer to their JSdocs or [source code](/src/core/schemas.ts).

src/core/schemas.ts

+34
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ export const numAsString = v.pipe(
5757
v.transform(Number),
5858
)
5959

60+
/**
61+
* Valibot schema to parse strings that are dates or datetime in ISO format.
62+
* Use to parse <input type="date" /> and <input type="datetime-local" /> values.
63+
* @example
64+
* ```ts
65+
* v.parse(dateAsString, '2022-01-01') -> new Date('2022-01-01')
66+
* v.parse(dateAsString, '2022-01-01T12:00:00') -> new Date('2022-01-01T12:00:00')
67+
* ```
68+
*/
69+
export const dateAsString = v.pipe(
70+
v.union([
71+
v.pipe(
72+
v.string(),
73+
v.isoDate(e => `Must be a date string in ISO format, received: ${e.received}`),
74+
),
75+
v.pipe(
76+
v.string(),
77+
v.isoDateTime(e => `Must be a datetime string in ISO format, received: ${e.received}`),
78+
),
79+
]),
80+
v.transform(d => new Date(d)),
81+
)
82+
6083
/**
6184
* Valibot schema to parse strings that are valid UUID.
6285
* @example
@@ -67,3 +90,14 @@ export const uuid = v.pipe(
6790
v.string(),
6891
v.uuid(e => `Must be a valid UUID, received: ${e.received}`),
6992
)
93+
94+
/**
95+
* Valibot schema to parse strings that are valid Email.
96+
* @example
97+
* ```ts
98+
* v.parse(vh.email, 'user@example') -> throws an error
99+
*/
100+
export const email = v.pipe(
101+
v.string(),
102+
v.email(e => `Must be a valid Email, received: ${e.received}`),
103+
)

test/schemas.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ describe('numAsString', () => {
7373
})
7474
})
7575

76+
describe('dateAsString', () => {
77+
it('parses a valid date as string', () => {
78+
expect(v.parse(vh.dateAsString, '1994-03-14')).toSatisfy(val => v.is(v.date(), val))
79+
})
80+
it('parses a valid datetime as string', () => {
81+
expect(v.parse(vh.dateAsString, '1994-03-14T12:00')).toSatisfy(val => v.is(v.date(), val))
82+
})
83+
it('throws on non-date string', () => {
84+
expect(() => v.parse(vh.dateAsString, '12:00')).toThrowError()
85+
})
86+
})
87+
7688
describe('uuid', () => {
7789
it('parses string to be a valid UUID', () => {
7890
expect(v.parse(vh.uuid, 'aa3452d1-ad94-4bd2-9782-1f69b3372784')).toBeTruthy()
@@ -81,3 +93,12 @@ describe('uuid', () => {
8193
expect(() => v.parse(vh.uuid, '123')).toThrowError()
8294
})
8395
})
96+
97+
describe('email', () => {
98+
it('parses string to be a valid Email', () => {
99+
expect(v.parse(vh.email, '[email protected]')).toBeTruthy()
100+
})
101+
it('parses string to not be a valid Email', () => {
102+
expect(() => v.parse(vh.email, 'user@example')).toThrowError()
103+
})
104+
})

0 commit comments

Comments
 (0)