getSubdocumentStrictValue.js 996 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. /**
  3. * Find the `strict` mode setting for the deepest subdocument along a given path
  4. * to ensure we have the correct default value for `strict`. When setting values
  5. * underneath a subdocument, we should use the subdocument's `strict` setting by
  6. * default, not the top-level document's.
  7. *
  8. * @param {Schema} schema
  9. * @param {String[]} parts
  10. * @returns {boolean | 'throw' | undefined}
  11. */
  12. module.exports = function getSubdocumentStrictValue(schema, parts) {
  13. if (parts.length === 1) {
  14. return undefined;
  15. }
  16. let cur = parts[0];
  17. let strict = undefined;
  18. for (let i = 0; i < parts.length - 1; ++i) {
  19. const curSchemaType = schema.path(cur);
  20. if (curSchemaType?.schema) {
  21. strict = curSchemaType.schema.options.strict;
  22. schema = curSchemaType.schema;
  23. cur = curSchemaType.$isMongooseDocumentArray && !isNaN(parts[i + 1]) ? '' : parts[i + 1];
  24. } else {
  25. cur += cur.length ? ('.' + parts[i + 1]) : parts[i + 1];
  26. }
  27. }
  28. return strict;
  29. };