handleImmutable.js 661 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. const StrictModeError = require('../../error/strict');
  3. module.exports = function handleImmutable(schematype, strict, obj, key, fullPath, ctx) {
  4. if (schematype == null || !schematype.options || !schematype.options.immutable) {
  5. return false;
  6. }
  7. let immutable = schematype.options.immutable;
  8. if (typeof immutable === 'function') {
  9. immutable = immutable.call(ctx, ctx);
  10. }
  11. if (!immutable) {
  12. return false;
  13. }
  14. if (strict === false) {
  15. return false;
  16. }
  17. if (strict === 'throw') {
  18. throw new StrictModeError(null,
  19. `Field ${fullPath} is immutable and strict = 'throw'`);
  20. }
  21. delete obj[key];
  22. return true;
  23. };