naz / swagger-express-validator

Lightweight requrest/response validation middleware based on swagger/OpenAPI schema
MIT License
56 stars 24 forks source link

No validation / logging #30

Closed robbiegleeson closed 3 years ago

robbiegleeson commented 5 years ago

Hi guys,

Great work by the way. I am trying to use this on my simple Express.js app but cannot see anything in logs or any validation. server.js is setup as follows:

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passport = require('passport');
const YAML = require('yamljs')
const cors = require('cors');

const config = YAML.load('./swagger.yml')
const apiValidator = require('./validations/swaggerValidator')

const app = express();

const users = require('./routes/api/users');
const ping = require('./routes/api/ping');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
app.use(apiValidator(config))

const db = require('./config/keys').mongoURI;
mongoose
  .connect(db)
  .then(() => console.log('MongoDB Connected'))
  .catch(err => console.log(err));

app.use(passport.initialize());

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

require('./config/passport.js')(passport);

//routes
app.use('/api/users', users);
app.use('/api', ping);

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

My Validator is as follows:

const util = require('util')
const validator = require('swagger-express-validator')

const apiValidator = schema => {
  console.log('schema', schema)
  const config = {
    schema,
    validateRequest: true,
    validateResponse: true,
    requestValidationFn: (req, data, errors) => {
      console.log('Made it requestValidationFn')
      console.log(`failed request validation: ${req.method} ${req.originalUrl}\n ${util.inspect(errors)}`)
    },
    responseValidationFn: (req, data, errors) => {
      console.log('Made it responseValidationFn')
      console.log(`failed response validation: ${req.method} ${req.originalUrl}\n ${util.inspect(errors)}`)
    },
  }
  return validator(config)
}

module.exports = apiValidator

Not seeing any of the console logs when I run through postman

kibertoad commented 5 years ago

@robbiegleeson requestValidationFn and responseValidationFn are only invoked when validation fails. Are you saying you don't see them invoked even when that happens?

kibertoad commented 5 years ago

If you also don't see validation being applied, it's most likely because there is no match between request path and endpoints defined in schema. Common reasons for that is absent or excessive '/' in the path and method mismatch. Try debugging inside validator and see if it resolved correct branch of schema or just skips validation due no unknown path.

kibertoad commented 5 years ago

@robbiegleeson Could you please provide an update on your situation?

robbiegleeson commented 5 years ago

@kibertoad Apologies - I have not had time to try debugging inside the validator yet - been mad busy at work. I'll get to it over the weekend and will let you know

kibertoad commented 5 years ago

@robbiegleeson Any updates?

kibertoad commented 5 years ago

@robbiegleeson Are you still observing this issue?

naz commented 3 years ago

Closing due to low activity. Happy to reopen if there's a need