Skip to content

Commit

Permalink
docs: JSONDataType
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Mar 6, 2021
1 parent 0aa047b commit 848d3f2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
5 changes: 2 additions & 3 deletions docs/guide/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const addFormats = require("ajv-formats")

const ajv = new Ajv()
addFormats(ajv)
````
```
</code-block>

<code-block title="TypeScript">
Expand All @@ -24,8 +24,7 @@ import addFormats from "ajv-formats"

const ajv = new Ajv()
addFormats(ajv)
````
```
</code-block>
</code-group>

Expand Down
5 changes: 2 additions & 3 deletions docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const validData = {

const valid = validate(data)
if (!valid) console.log(validate.errors)
````
```
</code-block>

<code-block title="JSON Type Definition">
Expand All @@ -83,8 +83,7 @@ const validData = {

const valid = validate(data)
if (!valid) console.log(validate.errors)
````
```
</code-block>
</code-group>

Expand Down
8 changes: 4 additions & 4 deletions docs/guide/managing-schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ app.post("/user", async (cxt) => {
cxt.status(400)
}
})
````
```
</code-block>

<code-block title="TypeScript">
Expand All @@ -83,7 +83,7 @@ app.post("/user", async (cxt) => {
cxt.status(400)
}
})
````
```
</code-block>
</code-group>

Expand Down Expand Up @@ -148,7 +148,7 @@ app.post("/user", async (cxt) => {
cxt.status(400)
}
})
````
```
</code-block>

<code-block title="users.ts">
Expand All @@ -169,7 +169,7 @@ app.post("/user", async (cxt) => {
cxt.status(400)
}
})
````
```
</code-block>
</code-group>

Expand Down
52 changes: 43 additions & 9 deletions docs/guide/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Ajv takes advantage of TypeScript type system to provide additional functionality that is not possible in JavaScript:

- utility types `JSONSchemaType` and `JTDSchemaType` to convert data type into the schema type to simplify writing schemas, both for [JSON Schema](../json-schema.md) (but without union support) and for [JSON Type Definition](../json-type-definition) (with tagged unions support).
- utility type `JTDDataType` to covert JSON Type Definition schema into the type of data that it defines.
- compiled validation functions are type guards that narrow the type after successful validation.
- validation errors for JSON Schema are defined as tagged unions, for type-safe error handling.
- when utility type is used, compiled JTD serializers only accept data of correct type (as they do not validate that the data is valid) and compiled parsers return correct data type.
Expand All @@ -19,7 +20,7 @@ For the same example as in [Getting started](./getting-started):
<code-block title="JSON Schema">
```typescript
import Ajv, {JSONSchemaType} from "ajv"
const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}
const ajv = new Ajv()

interface MyData {
foo: number
Expand Down Expand Up @@ -54,13 +55,13 @@ if (validate(data)) {
} else {
console.log(validate.errors)
}
````
```
</code-block>

<code-block title="JSON Type Definition">
```typescript
import Ajv, {JTDSchemaType} from "ajv/dist/jtd"
const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}
const ajv = new Ajv()

interface MyData {
foo: number
Expand Down Expand Up @@ -95,13 +96,47 @@ if (validate(data)) {
} else {
console.log(validate.errors)
}
````
```
</code-block>
</code-group>

See [this test](https://github.com/ajv-validator/ajv/tree/master/spec/types/json-schema.spec.ts) for an advanced example.

## Utility type for JTD data type

You can use JTD schema to construct the type of data using utility type `JTDDataType`

```typescript
import Ajv, {JTDDataType} from "ajv/dist/jtd"
const ajv = new Ajv()

const schema = {
properties: {
foo: {type: "int32"}
},
optionalProperties: {
bar: {type: "string"}
}
} as const

type MyData = JTDDataType<typeof schema>

// validate is a type guard for MyData - type is inferred from schema type
const validate = ajv.compile(schema)

const validData = {
foo: 1,
bar: "abc"
}

if (validate(data)) {
// data is MyData here
console.log(data.foo)
} else {
console.log(validate.errors)
}
```

## Type-safe error handling

With [JSON Schema](../json-schema), the validation error type is an open union, but it can be cast to a tagged union (using validation keyword as tag) for easier error handling.
Expand Down Expand Up @@ -133,7 +168,7 @@ if (validate(data)) {
}
}
}
````
```
</code-block>
</code-group>

Expand All @@ -147,7 +182,7 @@ This example uses the same data and schema types as above:
<code-block title="JSON Type Definition">
```typescript
import Ajv, {JTDSchemaType} from "ajv/dist/jtd"
const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}
const ajv = new Ajv()

interface MyData {
foo: number
Expand Down Expand Up @@ -201,7 +236,6 @@ function parseAndLogFoo(json: string): void {
console.log(data.foo)
}
}
````
```
</code-block>
</code-group>

0 comments on commit 848d3f2

Please sign in to comment.