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

Conditional Validation Rules based on Auth #2238

Closed
leore opened this issue Dec 2, 2014 · 3 comments
Closed

Conditional Validation Rules based on Auth #2238

leore opened this issue Dec 2, 2014 · 3 comments
Assignees
Labels
Milestone

Comments

@leore
Copy link

@leore leore commented Dec 2, 2014

We have built our API on top of Hapi. Overtime we have released a few different versions of our API.

In our most recent version we added a new payload parameter.

However, we have found it hard to add conditional validation rules as there is no way to access "request" object from within validate.

Is there a simpler way to do the following:

exports.create = {
  handler: function (request, reply) {
  //this checks if the user's version has the new feature
    if (Version.hasFeature('new parameter', request.auth.credentials)) {
      var result = Joi.validate(request.payload, {
        newParameter: Joi.string().max(30).required()
      }, { allowUnknown: true });

      /* istanbul ignore else */
      if (result.error) {
        return reply(new LobErrors.UnprocessableEntity(result.error.message));
      }
    }

    reply('success');
  },
  validate: {
    payload: {
      oldParameter: Joi.string().required(),
    }
  }
};
@devinivy
Copy link
Member

@devinivy devinivy commented Dec 2, 2014

Seems like you could use a route prerequisite: http://hapijs.com/api#route-prerequisites

@hueniverse hueniverse self-assigned this Dec 2, 2014
@hueniverse hueniverse added this to the 8.0.0 milestone Dec 2, 2014
@hueniverse hueniverse closed this in 6f558b9 Dec 2, 2014
@leore
Copy link
Author

@leore leore commented Dec 2, 2014

thanks @hueniverse

@Marsup Marsup added feature and removed request labels Sep 20, 2019
@lock
Copy link

@lock lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants