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

how to add validation to nedb? #7

Closed
akralj opened this issue Dec 30, 2015 · 4 comments
Closed

how to add validation to nedb? #7

akralj opened this issue Dec 30, 2015 · 4 comments

Comments

@akralj
Copy link

akralj commented Dec 30, 2015

hi all. I am trying to include a validator on post requests and I am pretty lost. I don't know how to add a proper response for the browser. i tried it with hooks and all sorts of others things, but no luck.
I get an exception in the browser with the code below.

var apiService, app, bodyParser, errors, feathers, hooks, nedb, port;

feathers = require('feathers');
hooks = require('feathers-hooks');
nedb = require('feathers-nedb');
bodyParser = require('body-parser');
errors = require('feathers-errors').types;

apiService = nedb('api').extend({
  create: function(data, params, cb) {
    var valid;
    if (valid = false) {
      console.log("update", data);
      return this._super(data, params, cb);
    } else {
      //WHAT TO CALL HERE, to get a proper response for the browser
      return cb(new errors.BadRequest("not valid"));
    }
  }
});

app = feathers()
.configure(hooks())
.use(feathers["static"]('./client/dist'))
.configure(feathers.socketio())
.configure(feathers.rest())
.use(bodyParser.json())
.use(bodyParser.urlencoded({extended: true}))
.use('api', apiService)
.use(function(err, req, res, next) {
  console.log(err);
  res.status(404);
  return res.json({
    message: err.message
  });
});

port = 7777;
app.listen(port, function() {
  return console.log('Feathers server listening on port ' + port);
});

and the following client request:

var data, url;

data = {
  username: "franzl",
  date: new Date(),
  type: "stammtisch"
};

url = "/api";

fetch(url, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(data)
}).then(function(res) {
  return console.log(res);
})["catch"](function(err) {
  return console.log(err);
});

Feathers in build error messages work fine when trying a GET to a missing id, but not my own validator.

thanks a lot and happy new year.

@daffl
Copy link
Member

daffl commented Dec 30, 2015

It doesn't look like your extended code would work the way I'd expect it. Did you look at the guide for using hooks for validation?

A simple extended validator can look like this:

apiService = nedb('api').extend({
  create: function(data, params, cb) {
    if(!data.name) {
      return cb(new errors.BadRequest('Name needs to be provided'));
    }

    return this._super(data, params, cb);
  }
});

If you now try to submit data without a name property you should get a BadRequest error.

@akralj akralj closed this as completed Dec 30, 2015
@akralj
Copy link
Author

akralj commented Dec 30, 2015

merci vielmals :)
that was it. My code was working before too, but I did not read my fetch result properly.

@ekryski
Copy link
Member

ekryski commented Dec 31, 2015

🎉 Glad that worked for you!

@akralj I did also notice that you had if (valid = false). That's gonna cause problems 😄

@akralj
Copy link
Author

akralj commented Jan 2, 2016

@ekryski
(if (valid = false)) of course. but that was only for testing.

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