Skip to content

docs: ✏️ improve docs for server.rules()#4141

Merged
devinivy merged 2 commits intohapijs:masterfrom
damusix:master
Sep 22, 2020
Merged

docs: ✏️ improve docs for server.rules()#4141
devinivy merged 2 commits intohapijs:masterfrom
damusix:master

Conversation

@damusix
Copy link
Copy Markdown
Contributor

@damusix damusix commented Aug 11, 2020

server.rules() was missing a good example and explanation was a bit obscure

server.rules() was missing a good example and explanation was a bit
obscure.
Copy link
Copy Markdown
Member

@devinivy devinivy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot this contribution! I made some notes in here, mostly based around making the example more self-contained. If you're alright with my proposed changes I think this would be good to go just by accepting those, but there are a few additional comments if you're interested and have the time to dig into them.

API.md Outdated
realm. This means the processor defined by the plugin override the config generated by the root
processor if they overlap. The route `config` overrides the rules config if the overlap.
realm. This means the processor defined by the plugin overrides the config generated by the root
processor if they overlap. `route.config` overrides `route.rules` if the resulting processed config overlaps.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I searched for other places where "route.config" is referenced in the docs and didn't find anything, so for consistency I just rephrased this slightly. For some reason I think it's also nice to start a sentence with something other than code.

Suggested change
processor if they overlap. `route.config` overrides `route.rules` if the resulting processed config overlaps.
processor if they overlap. Similarly, the route's own config overrides the config produced by the rules processors.

Comment on lines +2381 to +2383
if (!rules) {
return null;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for illustrating this detail 👍

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant, as it should never trigger for the example. Otherwise hapi has botched the validateSchema validation of the rules object.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kanongil perhaps this entire block could be wrapped in an if block https://github.com/hapijs/hapi/blob/master/lib/route.js#L67

...

const rules = route.rules || config.rules;
let rulesConfig = {};

if (rules) {
    const rulesConfig = internals.rules(rules, { method, path, vhost }, server);
}
...

API.md Outdated
}

if (rules.myCustomPre) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

API.md Outdated
}

if (rules.payload) {
options.validate.payload = Joi.object(rules.payload);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would fail since options.validate doesn't exist.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
options.validate.payload = Joi.object(rules.payload);
options.validate = { payload: Joi.object(rules.payload) };

if (rules.myCustomPre) {

options.pre = [
myPreHelper(...rules.myCustomPre)
Copy link
Copy Markdown
Member

@devinivy devinivy Aug 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to contrive a simple myPreHelper so that this example can be more self-contained?

API.md Outdated
validate: {
entity: 'user'
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
};

API.md Outdated
server.rules(processor, realmRules);

server.route({
...,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to replace this placeholder with some method and path to make this example more complete.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
...,
method: 'GET',
path: '/',

API.md Outdated
auth: Joi.string(),
myCustomPre: Joi.array().min(2).items(Joi.string()),
payload: Joi.object()
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
};

API.md Outdated
Comment on lines +2410 to +2414
const realmRules = {
validate: { schema: validateSchema }
};

server.rules(processor, realmRules);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal here is just to make things more concise and avoid the "realms" reference for those unfamiliar with that hapi concept.

Suggested change
const realmRules = {
validate: { schema: validateSchema }
};
server.rules(processor, realmRules);
server.rules(processor, {
validate: { schema: validateSchema }
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update this when I get a chance some time this week. Thank you for the feedback!

@damusix
Copy link
Copy Markdown
Contributor Author

damusix commented Sep 8, 2020

@devinivy changes have been made!

Copy link
Copy Markdown
Member

@devinivy devinivy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's chatter in slack about rules() and there may be some followup to do here, but for now this is great and is a very useful contribution. Thanks!

@devinivy devinivy merged commit 147b6fd into hapijs:master Sep 22, 2020
Nargonath added a commit that referenced this pull request Oct 28, 2020
* update handlebars dependency version

handlebars is only a devDependency, but update the version due to
a security advisory.

Refs: GHSA-q2c6-c6pm-g3gh

* docs: ✏️ improve docs for server.rules() (#4141)

* docs: ✏️ improve docs for server.rules()

server.rules() was missing a good example and explanation was a bit
obscure.

* docs: ✏️ update requested changes for route rules

Co-authored-by: Danilo Alonso <[email protected]>

* Use response instead of request when marshalling (#4162)

* Use response instead of request when marshalling. Closes #4161

* Cleanup

* Don't change internals.fail()

* Fix info.responding being set on remotely closed requests

* More cleanup

* 20.0.1

* upgrade to labv24 (#4167)

* Update joi documentation link (#4174)

Co-authored-by: cjihrig <[email protected]>
Co-authored-by: Danilo Alonso <[email protected]>
Co-authored-by: Danilo Alonso <[email protected]>
Co-authored-by: Gil Pedersen <[email protected]>
Co-authored-by: Devin Ivy <[email protected]>
Co-authored-by: Lloyd Benson <[email protected]>
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

Successfully merging this pull request may close these issues.

3 participants