Skip to content

Commit 83ea1f0

Browse files
authored
docs(arktype-utils): Adds README (#6)
1 parent 2c162ce commit 83ea1f0

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.changeset/**/*.md

packages/arktype-utils/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Arktype Utils
2+
3+
[ArkType](https://www.npmjs.com/package/arktype) is a TypeScript validator that delivers highly optimized validators.
4+
5+
I created some wrappers around ArkType's validators.
6+
7+
**_This is still an early version, the shape might change. I will attempt to keep a nice deprecation path._**
8+
9+
## Turning FormData into an object
10+
11+
This is a helper method, primarily for Node-based backends that need to turn `FormData` values into a JS object.
12+
13+
**NOTE**: This does _no_ validation, it just turns the values into an object
14+
15+
```ts
16+
import { formDataToObject } from '@jhecht/arktype-utils';
17+
18+
const fd = new FormData();
19+
fd.append('name', 'Bob');
20+
fd.append('surname', 'Surbob');
21+
fd.append('age', '31');
22+
23+
const obj = formDataToObject(fd);
24+
```
25+
26+
For repeated entries, if there is more than 1 value found, it will be turned into an array
27+
28+
```ts
29+
import { formDataToObject } from '@jhecht/arktype-utils';
30+
const fd = new FormData();
31+
fd.append('name', 'Bob');
32+
fd.append('name', 'John');
33+
34+
const obj = formDataToObject(fd);
35+
36+
// { name: ['Bob', 'John']}
37+
console.info(obj);
38+
```
39+
40+
This method all does it's best guess for JSON-parsible values, including BigInt
41+
42+
```ts
43+
import { formDataToObject } from '@jhecht/arktype-utils';
44+
45+
const fd = new FormData();
46+
fd.append('number', '13');
47+
fd.append('otherNumber', '3.1415');
48+
fd.append('bigNumber', '300n');
49+
50+
const obj = formDataToObject(fd);
51+
52+
// { number: 13, otherNumber: 3.1415, bigNumber: 300n } -- no strings!
53+
console.info(obj);
54+
```
55+
56+
You can also for an element to _always_ be an array by ending it with `[]`:
57+
58+
```ts
59+
import { formDataToObject } from '@jhecht/arktype-utils';
60+
61+
const fd = new FormData();
62+
fd.append('email[]', 'bob@bob.bob');
63+
64+
const obj = formDataToObject(fd);
65+
66+
// { email: ['bob@bob.bob'] }
67+
console.info(obj);
68+
```
69+
70+
## Validating Data with ArkType
71+
72+
I created this wrapper, which `throws` if there are any `error` values. It takes two arguments, one being a `FormData` object, and the other a `type` or `scope` call from ArkType, which serves as the validator for the object-ified `FormData`.
73+
74+
```ts
75+
import { validateFormData } from '@jhecht/arktype-utils';
76+
import { type } from 'arktype';
77+
78+
const fd = new FormData();
79+
// Assume `fd` is gotten from the request body here
80+
81+
try {
82+
const obj = validateFormData(
83+
fd,
84+
type({
85+
name: 'string>=2',
86+
age: '13<=number',
87+
favoriteMovies: 'string[]',
88+
}),
89+
);
90+
// If this code is ran we know that the value of the `fd` variables passes the above validations when turned into an object
91+
console.info(obj);
92+
} catch (e) {
93+
// If we end up here, `e` will be the `Problems` object returned by ArkType's validator
94+
console.error(e);
95+
}
96+
```

0 commit comments

Comments
 (0)