Skip to content

Commit

Permalink
Update TypeScript docs in README (#103)
Browse files Browse the repository at this point in the history
`as const` doesn't always work, so the `AllowedSchema` type from
this package must be used.
  • Loading branch information
simonplend authored Jan 20, 2022
1 parent e4d7264 commit 8fdd411
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,29 @@ Coming from `express-jsonschema`? Read the [migration notes](docs/migrating-from

### Schemas in TypeScript

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

```typescript
const addressSchema = {
import { AllowedSchema } from "express-json-validator-middleware";

const addressSchema: AllowedSchema = {
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.
This is required so TypeScript doesn't attempt to widen the types of values
in the schema object. If you omit this type, TypeScript will raise an error.

See issues [#39](https://github.com/simonplend/express-json-validator-middleware/issues/39)
and [#102](https://github.com/simonplend/express-json-validator-middleware/issues/102)
for more background.

## Error handling

Expand Down

0 comments on commit 8fdd411

Please sign in to comment.