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
65This 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
1515## Motivation
1616
1717Let'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
2020going to help us market our business.
2121
2222We've defined the data we need to track about each client's pet:
23- ```
23+
24+ ``` typescript
2425interface Pet {
2526 name: string ;
2627 species: string ;
@@ -30,24 +31,25 @@ interface Pet {
3031```
3132
3233And 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
4345the responsibility of verifying the type of data onto the user. By assuming that
4446all of our data is correctly formed, we've left ourselves open to unexpected
4547errors 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
5153with that added functionality.
5254[ Type guards] ( https://basarat.gitbooks.io/typescript/docs/types/typeGuard.html )
5355work, but are limited in that they circumvent type inference instead of working
@@ -56,7 +58,8 @@ with it, and can be cumbersome to write.
5658With ` json-type-validation ` we can define decoders that validate untyped json
5759input. Decoders are concise, composable, and typecheck against our defined types
5860and interfaces.
59- ```
61+
62+ ``` typescript
6063import {Decoder , object , string , optional , number , boolean } from ' json-type-validation'
6164
6265const petDecoder: Decoder <Pet > = object ({
@@ -70,7 +73,8 @@ const petDecoder: Decoder<Pet> = object({
7073Finally, we can choose from a number of decoding methods to validate json and
7174report success or failure. When some json input fails validation the decoder
7275clearly shows how the data was malformed.
73- ```
76+
77+ ``` typescript
7478const lyle: Pet = petDecoder .runWithException (pet1 )
7579
7680const bullwinkle: Pet = petDecoder .runWithException (pet2 )
0 commit comments