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

Object part of schema, validate destroys object #54

Open
bfdykstra opened this issue Jun 14, 2018 · 3 comments
Open

Object part of schema, validate destroys object #54

bfdykstra opened this issue Jun 14, 2018 · 3 comments

Comments

@bfdykstra
Copy link

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',
  }
@doomfalc
Copy link

See issue #42

You can work around it by disabling "stripping":

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

@eivindfjeldstad
Copy link
Owner

Yeah, maybe we could allow something like

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

@bfdykstra
Copy link
Author

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.

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

No branches or pull requests

3 participants