express-json-validator-middleware
Advanced tools
Comparing version 3.0.0-beta.0 to 3.0.0
{ | ||
"name": "express-json-validator-middleware", | ||
"version": "3.0.0-beta.0", | ||
"version": "3.0.0", | ||
"description": "An Express middleware to validate requests against JSON Schemas", | ||
@@ -46,4 +46,7 @@ "main": "src/index.js", | ||
"@types/json-schema": "^7.0.4", | ||
"ajv": "^8.8.2" | ||
"ajv": "^8.11.0" | ||
}, | ||
"engines": { | ||
"node": ">=14" | ||
}, | ||
"husky": { | ||
@@ -50,0 +53,0 @@ "hooks": { |
# Express JSON Validator Middleware | ||
> [Express](https://github.com/expressjs/express/) middleware for validating | ||
requests against JSON schemas. | ||
requests against JSON schemas with Ajv. | ||
@@ -14,11 +14,14 @@ [![npm version](https://img.shields.io/npm/v/express-json-validator-middleware.svg)](https://www.npmjs.com/package/express-json-validator-middleware) | ||
- **Simple** - JSON schemas are a simple and expressive way to describe a data structure. | ||
- **Standard** - JSON schemas are not specific to JavaScript. In fact, they are used just about everywhere. | ||
- **Fail-Fast** - Catch errors early in your logic, evading confusing errors later. | ||
- **Separate Validation** - Keep your routes clean. Validation logic doesn't need to be defined in your route handlers. | ||
- **Error Messaging** - Ajv provides you with rich error objects that can easily be transformed into human-readable format. | ||
- **Documentation** - Creating a JSON Schema is another way of documenting your application. | ||
- **Expressive** — JSON schemas are an expressive way to describe data structures. | ||
- **Standard** — JSON schemas are portable. There are validator implementations in many languages. | ||
- **Separate validation** — Avoid the need to handle request validation in your route handlers. | ||
- **Error messaging** — Ajv provides rich and descriptive error objects. | ||
- **Documentation** — Schemas can help document your application. | ||
## Install | ||
## Requirements | ||
- [Node.js](https://nodejs.org/en/download/) >= v14 | ||
## Installation | ||
```sh | ||
@@ -28,2 +31,4 @@ npm install express-json-validator-middleware | ||
If you're upgrading from v2 to v3, make sure you read the [migration notes](#upgrading-from-v2-to-v3). | ||
## Getting started | ||
@@ -76,7 +81,9 @@ | ||
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", | ||
@@ -89,11 +96,12 @@ required: ["street"], | ||
}, | ||
} 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 | ||
@@ -277,11 +285,20 @@ | ||
## Ajv versions | ||
## Upgrading from v2 to v3 | ||
The major version `1.x` of this module uses `ajv@5`, read their changelog and | ||
migration guide [here](https://github.com/ajv-validator/ajv/releases/tag/5.0.0). | ||
v2.x releases of this library use [Ajv v6](https://www.npmjs.com/package/ajv/v/6.6.2). | ||
v3.x of this library uses [Ajv v8](https://www.npmjs.com/package/ajv/v/8.11.0). | ||
Major version `2.x` uses `ajv@6` in order to support draft-07 of JSON Schema. | ||
You have to manually configure Ajv to support **draft-06** schemas | ||
(see https://github.com/ajv-validator/ajv/tree/v6#using-version-6). | ||
Notable changes between Ajv v6 and v8: | ||
- All formats have been moved to [ajv-formats](https://www.npmjs.com/package/ajv-formats). | ||
If you're using formats in your schemas, you must install this package to continue | ||
using them. | ||
- The structure of validation errors has changed. | ||
- Support has been dropped for JSON Schema draft-04. | ||
For full details, read the Ajv migration guide: [Changes from Ajv v6.12.6 to v8.0.0](https://ajv.js.org/v6-to-v8-migration.html). | ||
If you have any Ajv plugins as dependencies, update them to their newest versions. | ||
Older versions of Ajv plugins are less likely to be compatible with Ajv v8. | ||
## Tests | ||
@@ -293,2 +310,3 @@ | ||
npm install | ||
npm test | ||
@@ -295,0 +313,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
14291
0
319
Updatedajv@^8.11.0