ajv-provider.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. /**
  3. * AJV-based JSON Schema validator provider
  4. */
  5. var __importDefault = (this && this.__importDefault) || function (mod) {
  6. return (mod && mod.__esModule) ? mod : { "default": mod };
  7. };
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. exports.AjvJsonSchemaValidator = void 0;
  10. const ajv_1 = __importDefault(require("ajv"));
  11. const ajv_formats_1 = __importDefault(require("ajv-formats"));
  12. function createDefaultAjvInstance() {
  13. const ajv = new ajv_1.default({
  14. strict: false,
  15. validateFormats: true,
  16. validateSchema: false,
  17. allErrors: true
  18. });
  19. const addFormats = ajv_formats_1.default;
  20. addFormats(ajv);
  21. return ajv;
  22. }
  23. /**
  24. * @example
  25. * ```typescript
  26. * // Use with default AJV instance (recommended)
  27. * import { AjvJsonSchemaValidator } from '@modelcontextprotocol/sdk/validation/ajv';
  28. * const validator = new AjvJsonSchemaValidator();
  29. *
  30. * // Use with custom AJV instance
  31. * import { Ajv } from 'ajv';
  32. * const ajv = new Ajv({ strict: true, allErrors: true });
  33. * const validator = new AjvJsonSchemaValidator(ajv);
  34. * ```
  35. */
  36. class AjvJsonSchemaValidator {
  37. /**
  38. * Create an AJV validator
  39. *
  40. * @param ajv - Optional pre-configured AJV instance. If not provided, a default instance will be created.
  41. *
  42. * @example
  43. * ```typescript
  44. * // Use default configuration (recommended for most cases)
  45. * import { AjvJsonSchemaValidator } from '@modelcontextprotocol/sdk/validation/ajv';
  46. * const validator = new AjvJsonSchemaValidator();
  47. *
  48. * // Or provide custom AJV instance for advanced configuration
  49. * import { Ajv } from 'ajv';
  50. * import addFormats from 'ajv-formats';
  51. *
  52. * const ajv = new Ajv({ validateFormats: true });
  53. * addFormats(ajv);
  54. * const validator = new AjvJsonSchemaValidator(ajv);
  55. * ```
  56. */
  57. constructor(ajv) {
  58. this._ajv = ajv ?? createDefaultAjvInstance();
  59. }
  60. /**
  61. * Create a validator for the given JSON Schema
  62. *
  63. * The validator is compiled once and can be reused multiple times.
  64. * If the schema has an $id, it will be cached by AJV automatically.
  65. *
  66. * @param schema - Standard JSON Schema object
  67. * @returns A validator function that validates input data
  68. */
  69. getValidator(schema) {
  70. // Check if schema has $id and is already compiled/cached
  71. const ajvValidator = '$id' in schema && typeof schema.$id === 'string'
  72. ? (this._ajv.getSchema(schema.$id) ?? this._ajv.compile(schema))
  73. : this._ajv.compile(schema);
  74. return (input) => {
  75. const valid = ajvValidator(input);
  76. if (valid) {
  77. return {
  78. valid: true,
  79. data: input,
  80. errorMessage: undefined
  81. };
  82. }
  83. else {
  84. return {
  85. valid: false,
  86. data: undefined,
  87. errorMessage: this._ajv.errorsText(ajvValidator.errors)
  88. };
  89. }
  90. };
  91. }
  92. }
  93. exports.AjvJsonSchemaValidator = AjvJsonSchemaValidator;
  94. //# sourceMappingURL=ajv-provider.js.map