{"library":"metalman-schema","title":"metalman-schema","description":"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.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install metalman-schema"],"cli":null},"imports":["import createSchemaMiddleware from 'metalman-schema';","const createSchemaMiddleware = require('metalman-schema');","const validate = createSchemaMiddleware();"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import metalman from 'metalman';\nimport createSchemaMiddleware from 'metalman-schema';\n\n// Create the validation middleware using the factory\n// You can pass ajvOptions or a custom ajv instance here: createSchemaMiddleware({ ajvOptions: { allErrors: true } })\nconst validate = createSchemaMiddleware();\n\n// Define a metalman command with the validation middleware\nconst commandFactory = metalman([validate]);\n\nconst validateUser = commandFactory({\n  schema: {\n    type: 'object',\n    required: ['name', 'age'],\n    additionalProperties: false,\n    properties: {\n      name: { type: 'string', minLength: 1 },\n      age: { type: 'number', minimum: 18 }\n    }\n  }\n});\n\n// Valid case\nvalidateUser({ name: 'Alice', age: 30 }, (err) => {\n  if (err) {\n    console.error('Validation failed for Alice:', err.message);\n  } else {\n    console.log('Validation successful for Alice.');\n  }\n});\n\n// Invalid case (age too low)\nvalidateUser({ name: 'Bob', age: 16 }, (err) => {\n  if (err) {\n    console.error('Validation failed for Bob:', err.name, '-', err.message);\n  } else {\n    console.log('Validation successful for Bob.');\n  }\n});\n\n// Invalid case (missing required field)\nvalidateUser({ age: 25 }, (err) => {\n  if (err) {\n    console.error('Validation failed (missing name):', err.name, '-', err.message);\n  } else {\n    console.log('Validation successful (missing name).');\n  }\n});","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}