Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to walk through a schema's paths, recursing on subdocuments and arrays? #13469

Closed
1 task done
JavaScriptBach opened this issue Jun 2, 2023 · 1 comment
Closed
1 task done
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@JavaScriptBach
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.8.4

Node.js version

18.12.0

MongoDB version

6

Operating system

None

Operating system version (i.e. 20.04, 11.3, 10)

No response

Issue

I want to write a test ensuring that if a schema path is an optional enum, then null is included as an allowed value. (That Mongoose doesn't automatically include null for optional enums arguably a design flaw, but that's another topic.)

I've written this code which works for scalar fields:

const path = schema.paths;
for (const pathName of Object.keys(paths)) {
        const schemaType = paths[pathName];
        if (!schemaType.isRequired) {
          const enums = schemaType.options.enum;
          if (enums) {
            assert.ok(
              enums.some((x: any) => x === null),
              `Schema path '${pathName}' from model '${model.modelName}' is an optional enum, but doesn't allow \`null\`. Either make the path required, or allow null.`
            );
          }
        }

But I can't figure out how to recursively handle subdocuments and arrays. I spent an hour trying to figure it out, but there are too many undocumented methods in the API. Any help would be much appreciated. Thanks!

@JavaScriptBach JavaScriptBach added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Jun 2, 2023
@vkarpov15
Copy link
Collaborator

@JavaScriptBach try the following:

'use strict';

const { Schema } = require('mongoose');

const schema = new Schema({
  name: String,
  nested: new Schema({ foo: String }),
  arr: [{ bar: String }]
});

iteratePaths(schema);

function iteratePaths(schema, prefix = '') {
  if (prefix) {
    prefix = prefix + '.';
  }
  schema.eachPath((path, schematype) => {
    console.log(prefix + path);
    if (schematype.schema) {
      iteratePaths(schematype.schema, prefix + path);
    }
  });
}

You are right that null not being a valid value for optional enums is strange, this is the 3rd time I've seen #3044 has popped up in the last year or so, I think it is time we re-open that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants