-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Migrate joi
to 17.4.0
and adapt the codebase
#99899
Migrate joi
to 17.4.0
and adapt the codebase
#99899
Conversation
b85a955
to
73bd9f6
Compare
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.
Self-review
"joi": "^13.5.2", | ||
"joi": "^17.4.0", |
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.
💪
expect(() => schema.buffer().validate('abc')).toThrowErrorMatchingInlineSnapshot( | ||
`"expected value of type [Buffer] but got [string]"` | ||
); |
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.
joi.binary
now converts strings to buffers (using the default encoding)
I thought it was fine (at least it didn't break anything), but if we want to forbid that, I can add our own coerce
function for the binary
type
.default(() => undefined, 'undefined') | ||
.default(() => undefined) |
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.
No longer allowed to define a value description when setting a default value. We weren't using that anywhere anyway.
"_maxListeners": undefined, | ||
Symbol(kCapture): false, |
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 snapshot change is caused by a fix in the internal deep copy function joi
was using. Symbols are now properly copied.
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.
Maybe we should refactor the test to assert against Stream
instance?
@@ -71,14 +85,13 @@ describe('#hostname', () => { | |||
expect(hostNameSchema.validate('www.example.com')).toBe('www.example.com'); | |||
expect(hostNameSchema.validate('3domain.local')).toBe('3domain.local'); | |||
expect(hostNameSchema.validate('hostname')).toBe('hostname'); | |||
expect(hostNameSchema.validate('2387628')).toBe('2387628'); |
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.
joi
hostname validation was improved
DOMAIN_INVALID_TLDS_CHARS (last segment must start with alphanum) - https://github.com/sideway/address/blob/master/lib/domain.js#L88
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.
Looking at the changelog for joi 17.0.0, it appears that they also added support for non-ascii chars in hostnames: hapijs/joi#2163 😄
'[disableEmbedding.0]: expected value to equal [false]' | ||
'[disableEmbedding]: expected value to equal [false]' |
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.
single-type alternative, so the error message changed (see #99899 (comment))
validate(value) { | ||
if (value === '0') { | ||
return 'value 0 is not a valid hostname (use "0.0.0.0" to bind to all interfaces)'; | ||
} | ||
}, |
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.
0
is now (correctly) considered as an invalid hostname by joi
, so this custom validation is no longer required (and not invoked anyway if the value is 0
).
We'll have to use a oneOf
here when backporting to 7.x
to allow 0
, as the branch is still supposed to support this value.
hostname: schema.maybe( | ||
schema.string({ | ||
validate(value) { | ||
if (value === '0') { | ||
return 'must not be "0" for the headless browser to correctly resolve the host'; | ||
} | ||
}, | ||
hostname: true, | ||
}) | ||
), | ||
hostname: schema.maybe(schema.string({ hostname: true })), |
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.
@elastic/kibana-reporting-services 0
is now correctly considered as an invalid hostname by joi
. We're loosing the custom error message as the internal validation failing is performed before calling it.
Can you confirm this is acceptable?
const EntrySchemaDependingOnOS = schema.conditional( | ||
const EntriesSchema = schema.conditional( | ||
schema.siblingRef('os'), | ||
OperatingSystem.WINDOWS, | ||
WindowsEntrySchema, | ||
schema.arrayOf(WindowsEntrySchema, entriesSchemaOptions), | ||
schema.conditional( | ||
schema.siblingRef('os'), |
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.
@elastic/security-solution joi
's reference implementation is now less permissive.
You were using a reference from the schema of the type of an array to reference a sibling of the whole array's type
{
os: schema.something(),
entries: schema.arrayOf(schema.conditional(schema.siblingRef('os'), [...]))
}
This is no longer allowed, so I had to move the conditional to wrap the whole arrayOf
type instead. (thanks for the great test coverage on this file btw)
export const internals: JoiRoot = Joi.extend( | ||
{ | ||
name: 'boolean', | ||
|
||
type: 'boolean', | ||
base: Joi.boolean(), |
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.
Pretty much everything in the extension API changed, I will not explain all the changes in this file, but feel free to ask questions if you want info on some specific parts.
Pinging @elastic/kibana-core (Team:Core) |
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.
Code review only, security changes (src/core/server/csp/config.test.ts
) LGTM.
I also had a look at the Joi changelog and the breaking changes between major versions, nothing stood out to me.
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.
kbn-test changes LGTM
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.
} | ||
if (options.convert && typeof value === 'string') { | ||
if (prefs.convert && typeof value === 'string') { |
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.
Are we relying on joi
defaults for convert
? Maybe we shouldn't?
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.
prefs.convert
defaults to true
, and config-schema
doesn't provide any way to change this value, so effectively this part of the condition could be removed. I thought it was better to keep it, just to kinda preserve consistency with how the underlying library is working. But maybe not.
"_maxListeners": undefined, | ||
Symbol(kCapture): false, |
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.
Maybe we should refactor the test to assert against Stream
instance?
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.
🔥
schema.siblingRef('os'), | ||
OperatingSystem.WINDOWS, | ||
WindowsEntrySchema, | ||
schema.arrayOf(WindowsEntrySchema, entriesSchemaOptions), |
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.
LGTM. Thanks @pgayvallet
@dasansol92 FYI ☝️
@pgayvallet I tested the performance impact of this on a dashboard with dozens of panels and found that it was ~30% faster, as you said. I didn't do any code review, but that's great! |
💚 Build SucceededMetrics [docs]Public APIs missing comments
Unknown metric groupsAPI count
References to deprecated APIs
History
To update your PR or re-run it, just comment with: |
* bump joi to 17.4.0, start adapting stuff * remove custom validation rule, adapt instead * fix error handling * fix error handling again * fix strings type & validation * fix buffers and arrays * fix bytes * fix bytes_size type * update conditional_type error messages in tests * fix duration and map types * first attempt to fix union type error messages * revert conditional type assertions back to master state * fix object type * fix record type * fix stream types * rename test files to match sources * fix union type tests * temporary adapt feature/home usages of Joi * fix lint * adapt test assertion * fix http config schema validation * fix @kbn/test Config class * fix config again * fix reporting schema tests * fix security solution schema * adapt url tests * remove useless comment * remove space * typo * review comments # Conflicts: # src/core/server/http/__snapshots__/http_config.test.ts.snap
) * Migrate `joi` to `17.4.0` and adapt the codebase (#99899) * bump joi to 17.4.0, start adapting stuff * remove custom validation rule, adapt instead * fix error handling * fix error handling again * fix strings type & validation * fix buffers and arrays * fix bytes * fix bytes_size type * update conditional_type error messages in tests * fix duration and map types * first attempt to fix union type error messages * revert conditional type assertions back to master state * fix object type * fix record type * fix stream types * rename test files to match sources * fix union type tests * temporary adapt feature/home usages of Joi * fix lint * adapt test assertion * fix http config schema validation * fix @kbn/test Config class * fix config again * fix reporting schema tests * fix security solution schema * adapt url tests * remove useless comment * remove space * typo * review comments # Conflicts: # src/core/server/http/__snapshots__/http_config.test.ts.snap * allow '0' value for server.host * fix config def * update snapshots
* bump joi to 17.4.0, start adapting stuff * remove custom validation rule, adapt instead * fix error handling * fix error handling again * fix strings type & validation * fix buffers and arrays * fix bytes * fix bytes_size type * update conditional_type error messages in tests * fix duration and map types * first attempt to fix union type error messages * revert conditional type assertions back to master state * fix object type * fix record type * fix stream types * rename test files to match sources * fix union type tests * temporary adapt feature/home usages of Joi * fix lint * adapt test assertion * fix http config schema validation * fix @kbn/test Config class * fix config again * fix reporting schema tests * fix security solution schema * adapt url tests * remove useless comment * remove space * typo * review comments
Summary
Bump
joi
from13.5.2
to17.4.0
, and adapt usages, mostly in@kbn/config-schema
Fix #84624
Related to #78351
From the joi upgrade performance benchmark done here gcanti/io-ts-benchmarks#6, we should expect a +~30% perf gain for successful validations, and ~200% for unsuccessful validations.
Checklist