openapi-library / OpenAPIValidators

Use Jest or Chai to assert that HTTP responses satisfy an OpenAPI spec
MIT License
189 stars 35 forks source link

toSatisfySchemaInApiSpec doesn't validate correctly? #256

Closed okeydoke closed 2 years ago

okeydoke commented 2 years ago

Are you using jest or chai? Jest 27.0.6

Are you using OpenAPI 2, 3.0.X, or 3.1.0? 3.0.1

Describe the bug clearly Not sure if I'm doing something wrong but expect({}).toSatisfySchemaInApiSpec("AccountResponse") doesn't seem to validate very well/strictly. And seems to think that an empty object is valid when using the following schema.

AccountResponse looks like below

{
      type: 'object',
      properties: {
        accounts: {
          type: 'array',
          items: {
            type: 'object',
            properties: {
              account_type: {
                type: 'string',
                enum: [ 'credit', debit' ],
                description: 'the account type'
              },
              account_name: {
                type: 'string',
                description: 'the account name',
                example: 'Some account name'
              }
            }
          }
        }
      }
    }

It seems like toSatisfySchemaInApiSpec is true as long as the response is an object. Is there anyway to ensure that the response matches exactly

Steps to reproduce the bug: See above

What did you expect to happen instead? Would of thought toSatisfySchemaInApiSpec() would require the passed response to exactly match the spec

Are you going to resolve the issue? Probably not

jasperkennis commented 2 years ago

I'm running into the same problem. Using open api 3.0.0, jest-openapi@0.14.1 and jest@27.3.1. I don't know what I did but somehow it is now working again...

rwalle61 commented 2 years ago

thanks for raising this!

I think the validator is working correctly though - {} actually does satisfy your schema, because by default, properties are optional. If your schema marks the accounts property as required, does it validate as you expect? (And you may want to mark account_type and account_name as required too in the sub-schema)

I think required is a JSON schema thing - see some examples in the OpenAPI spec

Let me know if that works 🙂

okeydoke commented 2 years ago

Ah thanks I didn't realise I could/needed to add required to the response. It's working once I add that. Many thanks!