eivindfjeldstad/validate

Object part of schema, validate destroys object

Opened this issue · 3 comments

my schema:

const payloadSchema = new Schema({
  account: {
    type: String,
    required: true,
    length: 12,
  },
  book: {
    type: String,
    required: true,
  },
  application: {
    type: String,
    required: true,
  },
  action: {
    type: String,
    required: true,
  },
  user_id: {
    type: String,
    required: true,
  },
  client: {
    type: String,
    required: true,
  },
  ob: {
    type: Object,
    required: false,
  },
  name: {
    type: String,
    required: false,
    // string must be between 12 and 128 characters, have no spaces, be alphanumeric, and may contain only _+=,.@-=
    match: /^[a-zA-Z0-9_+=,.@-]{12,128}$/,
  },
  long_name: {
    type: String,
    required: false,
    // string must be between 12 and 64 characters, have no spaces, be alphanumeric, and may contain only _+=,.@-
    match: /^[a-zA-Z0-9_+=,.@-]{12,64}$/,
  },

})

module.exports = payloadSchema

When i call payloadSchema.validate(objectToValidate), the field ob in objectToValidate is modified and empty.

to reproduce this pass this object to payloadSchema:

const objectToValidate: {
    account: '123',
    book: 'monkeys are cool',
    application: 'book application',
    action: 'create-book',
    name: 'curious george',
    ob: {
        field: 'hello'
    },
    user_id: 1,
    client: 'a book',
  }

See issue #42

You can work around it by disabling "stripping":

payloadSchema.validate(objectToValidate, { strip: false });

Yeah, maybe we could allow something like

const schema = new Schema({
  prop: {
    type: Object,
    strip: false
  }
})

Okay thanks for the help! Maybe add that use as part of the examples then? When I saw those parameters I wasn't entirely sure on what kind of behavior that would prevent.