Skip to content

Add documentation for schemas in TypeScript #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { Validator } from "express-json-validator-middleware";
/**
* Define a JSON schema.
*/
const addressSchema = {
const addressSchema = {
type: "object",
required: ["street"],
properties: {
Expand Down Expand Up @@ -69,6 +69,29 @@ app.post("/address", validate({ body: addressSchema }), (request, response) => {

Coming from `express-jsonschema`? Read the [migration notes](docs/migrating-from-express-jsonschema.md).

### Schemas in TypeScript

If you are writing JSON schemas in TypeScript, you will need to cast your schema
to the `const` type e.g.

```typescript
const addressSchema = {
type: "object",
required: ["street"],
properties: {
street: {
type: "string",
}
},
} as const;
```

This is required so that TypeScript doesn't attempt to widen the types of values
in the schema object. If you omit the `as const` statement TypeScript will raise
a compilation error. The discussion in
[this issue](https://github.com/simonplend/express-json-validator-middleware/issues/39)
provides further background.

## Error handling

On encountering invalid data, the validator will call `next()` with a
Expand Down