metalman-schema is a JSON schema validation middleware specifically designed for the `metalman` module. It leverages the `ajv` library for robust JSON schema validation. The current stable version is 4.0.2, released in November 2023, indicating an active but not rapid release cadence, with previous major updates in 2021. This library differentiates itself by providing a `metalman`-compatible middleware, allowing schema definitions to be integrated directly into `metalman` commands for streamlined request validation. It acts as a factory function that returns the actual middleware, providing flexibility to configure the underlying `ajv` instance.
npm install metalman-schemaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `metalman-schema` as a middleware with `metalman` to validate incoming command data against a JSON schema, showing both valid and invalid scenarios.
Review AJV v7 release notes for schema migration guidelines, especially regarding `$id` vs `id` and format validation changes (now requiring `ajv-formats`). Update schemas to comply with JSON Schema draft-06 or later.
Ensure your project uses `metalman@v3` or newer and a Node.js version compatible with ES2015+ features. Update `require` statements to `const` for improved code style.
If type coercion is undesirable, you may need to configure the AJV instance passed to `createSchemaMiddleware` to disable this behavior (e.g., `{ coerceTypes: false }`). Refer to AJV documentation for exact options.If you need to control format validation explicitly, pass a custom `AJV` instance to the `createSchemaMiddleware` factory and configure `ajv-formats` (or omit it) as per your requirements.
Inspect `err.errors` (if available) or `err.message` for details on which part of the input failed validation. Adjust the input data or refine the JSON schema definition.
Ensure that the `metalman` command function, when called, receives an options object that includes a `schema` property with a valid JSON schema definition, e.g., `commandFactory({ schema: { type: 'object', ... } })`.For `ajv` v7+, use `const Ajv = require('ajv').default;` for CommonJS or `import Ajv from 'ajv';` for ESM, then instantiate with `new Ajv()`. Ensure `ajv` is correctly installed.