question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Replace Gavel Validation With Custom Validation

See original GitHub issue

Gavel currently uses a simple text diff to validate HTML response entity bodies. I would like Gavel to support X/HTML validation in the future using XML Schema. However, here is how I am circumventing Gavel validation, and validating a response myself.

var hooks = require('hooks');
var stash = {};
...
/**
 * Validate HTML generated from an API Blueprint. Gavel validates X/HTML using
 * Simple Text Diff. Simple Text Diff is primitive. We can do better.
 * If the HTML passes our validation, then force Gavel to pass by masking the
 * real response body with the expected body, else fail before Gavel validation.
 *
 * @see {@link https://www.relishapp.com/apiary/gavel/docs/expectations}
 */
function validateBlueprintHtml(transaction) {
  var protagonist = require('protagonist');
  var cheerio = require('cheerio');
  var $ = cheerio.load(transaction.real.body);
  var apibName = protagonist.parseSync(transaction.request.body).ast.name;
  var htmlName = $('#apiHeadline').text();
  var valid = apibName === htmlName;

  if(valid) {
    transaction.real.body = transaction.expected.body;
  } else {
    transaction.fail = "Generated HTML Does Not Include An API Name From Its Blueprint.";
  }
}
...
hooks.beforeValidation('Blueprint > Generate Blueprint HTML > Generate Blueprint HTML', validateBlueprintHtml);

As a user, I want to replace Gavel validation in cases where testing a transaction could not feasibly pass because a real response body will never equal an expected response body.

Perhaps there could be a hooks.validation() or hooks.duringValidation() that allows for an alternative to Gavel. Perhaps there could also be transaction.pass, with similar intention as transaction.fail, to pass before or during Gavel validation.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
artem-zakharchenkocommented, Oct 23, 2019

As I understood the original intention is to replace Gavel validation by arbitrary validation. In that case the usage of Gavel as a validator should be opted-out, meaning Gavel must not be called in the first place.

Perhaps, a hooks.validate() hook to perform the validation by yourself? It would be required to meet the Gavel’s unified validation result structure in that case. It’s quite generic at this point, so it’s not coupled with any kind of underlying validator. This way the input to Dredd would be reliable, as there can be multiple things related on the validation result (argument against manual transaction mutation to imitate custom validation).

0reactions
honzajavorekcommented, Nov 3, 2016

Dredd’s life cycle, as it was designed and documented, was broken until now - you could not change the transaction.test object in hooks and in some cases, Dredd would emit test fail events to reporters right after Gavel validation. This is now fixed in https://github.com/apiaryio/dredd/pull/648.

I’m pretty confident we have now, as @netmilk wrote, everything in place. Moreover, everything should also work as expected and documented.

Hence implementing this in hooks should be entirely possible now and not only with afterEachValidation hooks, but also with after and afterEach hooks. This issue can be closed if the use case gets a nice, working example in documentation (and preferably also test).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Validation in ASP.NET Web API with FluentValidation
Now that we have the validation rules in place, we need a way to validate that all requests submitted to this service are...
Read more >
Form validation with React Hooks WITHOUT a library
In this tutorial, I want to show you how you can create a custom hook that you can use to validate all of...
Read more >
Spring MVC Custom Validation - Baeldung
In this tutorial, we'll do just that; we'll create a custom validator to validate a form with a phone number field, and then...
Read more >
Angular Reactive Forms: trigger validation on submit
Validating all fields on submit ... To validate all form fields, we need to iterate throughout all form controls: Object.keys(this.form.controls).
Read more >
How To Use Custom Form Validation in Angular - DigitalOcean
Learn how to create a custom validator in Angular for both template-driven and reactive forms.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found