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

Default values #98

Open
catadch opened this issue Nov 2, 2019 · 6 comments
Open

Default values #98

catadch opened this issue Nov 2, 2019 · 6 comments

Comments

@catadch
Copy link

catadch commented Nov 2, 2019

I need a validator that inserts default values in the request body AND honors with discriminators, yours does the latter, but not the former. I have found others that are the other way around.

Am I doing something wrong, or is it a know issue that this package does not insert default values from the schema?

@catadch
Copy link
Author

catadch commented Nov 2, 2019

My init is this, btw:

swaggerValidation.init('./api/openapi.yaml', { 
    ajvConfigParams: { useDefaults: true },
    ajvConfigBody: { useDefaults: true } 
  }
);

@kibertoad
Copy link
Collaborator

It is supported, but you need custom ajv configuration for that. See https://github.com/Zooz/express-ajv-swagger-validation/blob/master/test/express/test-simple-server-with-coercion.js for an example.

@catadch
Copy link
Author

catadch commented Nov 2, 2019

Thanks kibertoad - looks like I'm doing the right thing by passing on useDefaults to ajv, but still no joy. An example of a not used default from my schema is:

        cost:
          type: number
          format: int32
          enum: [ 35 ]
          default: 35

express-openapi-validator also uses ajv and inserts this and other defaults ok, but does not support discriminators.

@catadch
Copy link
Author

catadch commented Nov 2, 2019

Ok. Done some testing.

If I modify the schema so the Pet type is this:

  Pet:
    required:
      - id
      - name
      - tag 
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
      tag:
        type: string
        default: Taggy McTagFace

And create an extra test in test/express/middleware-test.js

        it('request with missing parameter should pass validation thanks to default', function (done) {
            request(app)
                .put('/pets')
                .send([{
                    name: 1,
                    //tag: 'tag',
                }])
                .expect(200, function (err, res) {
                    if (err) {
                        throw err;
                    }
                    expect(res.body.tag).to.equal('Taggy McTagface');
                    done();
                });
        });

The "tag" value shoud be defaulted to 'Taggy McTagface', but it isn't, instead we get:
Uncaught AssertionError: expected undefined to equal 'Taggy McTagface'

As an aside, as 'tag' is now required, this test should fail with a 400.

@kobik
Copy link
Collaborator

kobik commented Nov 3, 2019

@catadch thanks for reporting the issue.

First of all this is a bug, as we should pass useDefaults by default to ajv.

We'll need to investigate this further.

@hilleer
Copy link

hilleer commented Aug 23, 2024

Any news on this topic? 😄

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

No branches or pull requests

4 participants