-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Allow object keys to be verified by schema #1499
Conversation
I'd like to stay closer to what currently exists, integrating schemas into the current code should be fairly easy, no need for a new API or casting values, I'd just make |
Ah, I got it in my mind that pattern = regex, but it also makes sense that a schema would essentially still be a pattern for the keys. To be clear, should pattern accept standard regex as it does currently as well as a schema in the first arg to preserve backwards compatibility? |
That would be my intention yes, without casting it to a schema, it would be slower. |
Ok, updated as discussed. I also rebased and started clean so that there weren't a bunch of line diffs that were related to the direction I had previously gone in. Let me know what else you'd like to see. |
API.md
Outdated
@@ -1611,6 +1611,18 @@ const schema = Joi.object({ | |||
}).pattern(/\w\d/, Joi.boolean()); | |||
``` | |||
|
|||
#### `object.pattern(keySchema, valueSchema)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not how we usually document methods with multiple signatures, you should try to make it work with current documentation.
lib/types/object/index.js
Outdated
@@ -637,7 +651,12 @@ internals.Object = class extends Any { | |||
|
|||
for (let i = 0; i < this._inner.patterns.length; ++i) { | |||
const pattern = this._inner.patterns[i]; | |||
description.patterns.push({ regex: pattern.regex.toString(), rule: pattern.rule.describe() }); | |||
if (pattern.regex) { | |||
description.patterns.push({ patternRule: pattern.patternRule.toString(), valueRule: pattern.valueRule.describe() }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not make this a breaking change please.
Good points, updated! |
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. |
I saw this issue: #1382 and thought I might take a shot at it. It adds dynamicKeys to support validating object keys with schema instead of requiring a regex parameter. It also reworks pattern to just use dynamicKeys under the hood, but that might not be a great thing to stick with if there is a large performance gain in testing a single regex vs a schema that has a single regex rule.
As part of this, I also updated describe, but cannot for the life of me figure out how to make the describe test pass. Based on the output, the actual and expected describe results are identical, but the test still fails.edit: I just had to sleep on it, found and fixed.Anyway, I hope this helps or at least gives some folks ideas on how they might want to make this feature happen. If this is even reasonably close to mergeable, I'm happy to keep working on it.