adonisjs / validator

Schema based validator for AdonisJS
MIT License
115 stars 40 forks source link

object().members() accept properties that are not defined #170

Closed technoknol closed 12 months ago

technoknol commented 1 year ago

Package version

5.9.0

Node.js and npm version

node: 16.15.0 , npm: 8.5.5

Sample Code (to reproduce the issue)

Schema

{
     profile: schema.object().members({
        username: schema.string(),
        avatar_url: schema.string()
      })
}

Request Payload

{
     profile: {
        "asdfasd": "afasd",
        "username": "asfasd",
        "avatar_url": "asfsad"
     }
}

This validation passes and doesn't throw an error even if asdfasd is neither defined nor anyMembers is used.

RomainLanz commented 1 year ago

Hey @technoknol! 👋🏻

This is the expected behavior. The validated data will not have the properties you have not defined in your schema.

const data = {
  username: 'romain.lanz',
  avatar_url: 'http',
  wrong: true
}

const validatedData = await validator.validate({
  schema: schema.create({
    profile: schema.object().members({
      username: schema.string(),
      avatar_url: schema.string()
    }),
  }),
  data,
});

Here, validatedData will not have the wrong property.