Skip to content

Commit aaf6055

Browse files
authored
Merge pull request #4 from mojotech/cpjolicoeur-patch-1
Update README - Properly capitalize TypeScript (and link the first occurrence to the website. - Renamed the variables in the example code to be a little less "coder generic" - Added proper Markdown spacing around code blocks - Added code block syntax highlighting markers
2 parents 822731e + bf41d62 commit aaf6055

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# JSON Type Validation
22

3-
A typescript library to perform type checking and validation on untyped JSON
4-
data at runtime.
3+
A [TypeScript](https://www.typescriptlang.org/) library to perform type checking and validation on untyped JSON data at runtime.
54

65
This library owes thanks to:
6+
77
- [JsonDecoder](https://github.com/aische/JsonDecoder) by Daniel van den Eijkel
88
- [Type-safe JSON Decoder](https://github.com/ooesili/type-safe-json-decoder) by Wesley Merkel
99
- The Elm [Json.Decode](http://package.elm-lang.org/packages/elm-lang/core/latest/Json-Decode) API
@@ -15,12 +15,13 @@ TODO
1515
## Motivation
1616

1717
Let's say we're creating a web app for our pet sitting business, and we've
18-
picked typescript as one of our core technologies. This is a great choice
19-
because the extra stability and type safety that typescript provides is really
18+
picked TypeScript as one of our core technologies. This is a great choice
19+
because the extra stability and type safety that TypeScript provides is really
2020
going to help us market our business.
2121

2222
We've defined the data we need to track about each client's pet:
23-
```
23+
24+
```typescript
2425
interface Pet {
2526
name: string;
2627
species: string;
@@ -30,24 +31,25 @@ interface Pet {
3031
```
3132

3233
And we've got some data about current client's pets which is stored as JSON:
33-
```
34-
const pet1: Pet = JSON.parse('{"name":"Lyle","species":"Crocodile","isCute":true}')
35-
const pet2: Pet = JSON.parse('{"name":"Bullwinkle","age":59}')
34+
35+
```typescript
36+
const croc: Pet = JSON.parse('{"name":"Lyle","species":"Crocodile","isCute":true}')
37+
const moose: Pet = JSON.parse('{"name":"Bullwinkle","age":59}')
3638
```
3739

38-
But that can't be right -- our data for `pet2` is missing information required
39-
for the `Pet` interface, but typescript compiles the code just fine!
40+
But that can't be right -- our data for `moose` is missing information required
41+
for the `Pet` interface, but TypeScript compiles the code just fine!
4042

41-
Of course this isn't an issue with typescript, but with our own type
42-
annotations. In typescript `JSON.parse` has a return type of `any`, which pushes
43+
Of course this isn't an issue with TypeScript, but with our own type
44+
annotations. In TypeScript `JSON.parse` has a return type of `any`, which pushes
4345
the responsibility of verifying the type of data onto the user. By assuming that
4446
all of our data is correctly formed, we've left ourselves open to unexpected
4547
errors at runtime.
4648

47-
Unfortunately typescript doesn't provide a good built-in way to deal with this
48-
issue. Providing run-time type information is one of typescript's
49+
Unfortunately TypeScript doesn't provide a good built-in way to deal with this
50+
issue. Providing run-time type information is one of TypeScript's
4951
[non-goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals#non-goals),
50-
and our web app is too important to risk using a forked version of typescript
52+
and our web app is too important to risk using a forked version of TypeScript
5153
with that added functionality.
5254
[Type guards](https://basarat.gitbooks.io/typescript/docs/types/typeGuard.html)
5355
work, but are limited in that they circumvent type inference instead of working
@@ -56,7 +58,8 @@ with it, and can be cumbersome to write.
5658
With `json-type-validation` we can define decoders that validate untyped json
5759
input. Decoders are concise, composable, and typecheck against our defined types
5860
and interfaces.
59-
```
61+
62+
```typescript
6063
import {Decoder, object, string, optional, number, boolean} from 'json-type-validation'
6164

6265
const petDecoder: Decoder<Pet> = object({
@@ -70,7 +73,8 @@ const petDecoder: Decoder<Pet> = object({
7073
Finally, we can choose from a number of decoding methods to validate json and
7174
report success or failure. When some json input fails validation the decoder
7275
clearly shows how the data was malformed.
73-
```
76+
77+
```typescript
7478
const lyle: Pet = petDecoder.runWithException(pet1)
7579

7680
const bullwinkle: Pet = petDecoder.runWithException(pet2)

0 commit comments

Comments
 (0)