sanitization.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Sanitization = void 0;
  4. const utils_1 = require("../utils");
  5. class Sanitization {
  6. constructor(sanitizer, custom, options = [],
  7. // For testing only.
  8. // Deliberately not calling it `toString` in order to not override `Object.prototype.toString`.
  9. stringify = utils_1.toString) {
  10. this.sanitizer = sanitizer;
  11. this.custom = custom;
  12. this.options = options;
  13. this.stringify = stringify;
  14. }
  15. async run(context, value, meta) {
  16. const { path, location } = meta;
  17. const runCustomSanitizer = async () => {
  18. const sanitizerValue = this.sanitizer(value, meta);
  19. return Promise.resolve(sanitizerValue);
  20. };
  21. if (this.custom) {
  22. const newValue = await runCustomSanitizer();
  23. context.setData(path, newValue, location);
  24. return;
  25. }
  26. const values = Array.isArray(value) ? value : [value];
  27. const newValues = values.map(value => {
  28. return this.sanitizer(this.stringify(value), ...this.options);
  29. });
  30. // We get only the first value of the array if the original value was wrapped.
  31. context.setData(path, values !== value ? newValues[0] : newValues, location);
  32. }
  33. }
  34. exports.Sanitization = Sanitization;