diff --git a/docs/guide/formats.md b/docs/guide/formats.md index c2eb1cb4d..ccd4077f4 100644 --- a/docs/guide/formats.md +++ b/docs/guide/formats.md @@ -14,7 +14,7 @@ const addFormats = require("ajv-formats") const ajv = new Ajv() addFormats(ajv) -```` +``` @@ -24,8 +24,7 @@ import addFormats from "ajv-formats" const ajv = new Ajv() addFormats(ajv) -```` - +``` diff --git a/docs/guide/getting-started.md b/docs/guide/getting-started.md index e5182787e..c05ea9505 100644 --- a/docs/guide/getting-started.md +++ b/docs/guide/getting-started.md @@ -56,7 +56,7 @@ const validData = { const valid = validate(data) if (!valid) console.log(validate.errors) -```` +``` @@ -83,8 +83,7 @@ const validData = { const valid = validate(data) if (!valid) console.log(validate.errors) -```` - +``` diff --git a/docs/guide/managing-schemas.md b/docs/guide/managing-schemas.md index 084f6159e..7eb3a8fd5 100644 --- a/docs/guide/managing-schemas.md +++ b/docs/guide/managing-schemas.md @@ -60,7 +60,7 @@ app.post("/user", async (cxt) => { cxt.status(400) } }) -```` +``` @@ -83,7 +83,7 @@ app.post("/user", async (cxt) => { cxt.status(400) } }) -```` +``` @@ -148,7 +148,7 @@ app.post("/user", async (cxt) => { cxt.status(400) } }) -```` +``` @@ -169,7 +169,7 @@ app.post("/user", async (cxt) => { cxt.status(400) } }) -```` +``` diff --git a/docs/guide/typescript.md b/docs/guide/typescript.md index 1a4bfad97..a51da5f4e 100644 --- a/docs/guide/typescript.md +++ b/docs/guide/typescript.md @@ -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. @@ -19,7 +20,7 @@ For the same example as in [Getting started](./getting-started): ```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 @@ -54,13 +55,13 @@ if (validate(data)) { } else { console.log(validate.errors) } -```` +``` ```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 @@ -95,13 +96,47 @@ if (validate(data)) { } else { console.log(validate.errors) } -```` - +``` 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 + +// 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. @@ -133,7 +168,7 @@ if (validate(data)) { } } } -```` +``` @@ -147,7 +182,7 @@ This example uses the same data and schema types as above: ```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 @@ -201,7 +236,6 @@ function parseAndLogFoo(json: string): void { console.log(data.foo) } } -```` - +```