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

Not working with array schemas in [email protected]+ #35

Closed
makinde opened this issue Nov 19, 2019 · 1 comment · Fixed by #36
Closed

Not working with array schemas in [email protected]+ #35

makinde opened this issue Nov 19, 2019 · 1 comment · Fixed by #36

Comments

@makinde
Copy link
Contributor

makinde commented Nov 19, 2019

My schema definition looks like this:

const groupSchema = new mongoose.Schema({
  name: {
    type: String,
    trim: true,
  },
  members: {
    type: [String],
    ref: 'Profile',
    refConditions: inSameOrgRefCondition,
    set: idListSetter,
  },
});

This results in a schemaType that looks like this in [email protected].

SchemaArray {
  casterConstructor:
   { [Function: SchemaString]
     schemaName: 'String',
     _cast: [Function: castString],
     cast: [Function: cast],
     get: [Function],
     _checkRequired: [Function],
     checkRequired: [Function] },
  caster:
   SchemaString {
     enumValues: [],
     regExp: null,
     path: 'members',
     instance: 'String',
     validators: [],
     getters: [],
     setters: [],
     options: {},
     _index: null },
  '$isMongooseArray': true,
  path: 'members',
  instance: 'Array',
  validators: [],
  getters: [],
  setters: [ [Function: idListSetter], [Function: membersSetter] ],
  options:
   { type: [ [Function: String] ],
     ref: 'Profile',
     refConditions: { org: [Function: orgFromDocument] },
     set: [Function: idListSetter] },
  _index: null,
  defaultValue: { [Function: defaultFn] '$runBeforeSetters': true },
  isRequired: true,
  requiredValidator: [Function],
  originalRequiredValue: true }

The following code doesn't recognize the members field as an array:

        if (schemaType.options && schemaType.options.ref) {
            validateFunction = validateId
            refModelName = schemaType.options.ref
            if (schemaType.options.refConditions) {
                conditions = schemaType.options.refConditions
            }
        } else if (schemaType.caster && schemaType.caster.instance &&
            schemaType.caster.options && schemaType.caster.options.ref) {
            validateFunction = validateIdArray
            refModelName = schemaType.caster.options.ref
            if (schemaType.caster.options.refConditions) {
                conditions = schemaType.caster.options.refConditions
            }
        }
makinde added a commit to makinde/mongoose-id-validator that referenced this issue Nov 19, 2019
Fixes CampbellSoftwareSolutions#35

The old way of detecting if we needed the array validator didn't handle certain schemaType from mongoose 5.4+. This handles those cases.
@martin-css
Copy link
Member

Published in v0.5.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants