decoders 2.2.0-test3 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
Elegant and battle-tested validation library for type-safe input data for
|
|
8
8
|
[TypeScript](https://www.typescriptlang.org/).
|
|
9
9
|
|
|
10
|
-
##
|
|
10
|
+
## Introduction
|
|
11
11
|
|
|
12
12
|
Data entering your application from the outside world should not be trusted without
|
|
13
13
|
validation and often is of the `any` type, effectively disabling your type checker around
|
|
@@ -16,14 +16,12 @@ program's boundaries. This has two benefits: (1) your inputs are getting validat
|
|
|
16
16
|
(2) you can now statically know for sure the shape of the incoming data. **Decoders help
|
|
17
17
|
solve both of these problems at once.**
|
|
18
18
|
|
|
19
|
-
##
|
|
19
|
+
## Basic example
|
|
20
20
|
|
|
21
21
|
```typescript
|
|
22
22
|
import { array, iso8601, number, object, optional, string } from 'decoders';
|
|
23
23
|
|
|
24
|
-
//
|
|
25
24
|
// Incoming data at runtime
|
|
26
|
-
//
|
|
27
25
|
const externalData = {
|
|
28
26
|
id: 123,
|
|
29
27
|
name: 'Alison Roberts',
|
|
@@ -31,9 +29,7 @@ const externalData = {
|
|
|
31
29
|
tags: ['foo', 'bar'],
|
|
32
30
|
};
|
|
33
31
|
|
|
34
|
-
//
|
|
35
32
|
// Write the decoder (= what you expect the data to look like)
|
|
36
|
-
//
|
|
37
33
|
const userDecoder = object({
|
|
38
34
|
id: number,
|
|
39
35
|
name: string,
|
|
@@ -41,20 +37,36 @@ const userDecoder = object({
|
|
|
41
37
|
tags: array(string),
|
|
42
38
|
});
|
|
43
39
|
|
|
44
|
-
//
|
|
45
40
|
// Call .verify() on the incoming data
|
|
46
|
-
//
|
|
47
41
|
const user = userDecoder.verify(externalData);
|
|
48
42
|
// ^^^^
|
|
49
|
-
// TypeScript
|
|
50
|
-
//
|
|
43
|
+
// TypeScript automatically infers this type as:
|
|
51
44
|
// {
|
|
52
45
|
// id: number;
|
|
53
46
|
// name: string;
|
|
54
47
|
// createdAt?: Date;
|
|
55
48
|
// tags: string[];
|
|
56
49
|
// }
|
|
57
|
-
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npm install decoders
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Requirements
|
|
59
|
+
|
|
60
|
+
You must set `strict: true` in your `tsconfig.json` in order for type inference to work
|
|
61
|
+
correctly!
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
// tsconfig.json
|
|
65
|
+
{
|
|
66
|
+
"compilerOptions": {
|
|
67
|
+
"strict": true
|
|
68
|
+
}
|
|
69
|
+
}
|
|
58
70
|
```
|
|
59
71
|
|
|
60
72
|
## Documentation
|