undefinedAsNull.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. /** @typedef {import("ajv").default} Ajv */
  7. /** @typedef {import("ajv").SchemaValidateFunction} SchemaValidateFunction */
  8. /** @typedef {import("ajv").AnySchemaObject} AnySchemaObject */
  9. /** @typedef {import("ajv").ValidateFunction} ValidateFunction */
  10. /**
  11. *
  12. * @param {Ajv} ajv
  13. * @returns {Ajv}
  14. */
  15. function addUndefinedAsNullKeyword(ajv) {
  16. ajv.addKeyword({
  17. keyword: "undefinedAsNull",
  18. before: "enum",
  19. modifying: true,
  20. /** @type {SchemaValidateFunction} */
  21. validate(kwVal, data, metadata, dataCxt) {
  22. if (kwVal && dataCxt && metadata && typeof metadata.enum !== "undefined") {
  23. const idx = dataCxt.parentDataProperty;
  24. if (typeof dataCxt.parentData[idx] === "undefined") {
  25. // eslint-disable-next-line no-param-reassign
  26. dataCxt.parentData[dataCxt.parentDataProperty] = null;
  27. }
  28. }
  29. return true;
  30. }
  31. });
  32. return ajv;
  33. }
  34. var _default = exports.default = addUndefinedAsNullKeyword;