Repository navigation
Conversation
Joi.build() parsed example objects with a `type` property as nested
schemas, so describe()/build() round-trips failed for valid examples
such as { type: 'some-type' }. Fixes hapijs#2480.
|
|
||
| if (desc.type) { | ||
| if (desc.type && | ||
| !options.literal) { |
There was a problem hiding this comment.
This check only guards desc.type with options.literal. A single value, ref, regex, buffer, function or override key still unwraps above this line, so build still misreads those examples.
There was a problem hiding this comment.
Good point. Handled options.literal before the single-key descriptor checks so literal objects containing value, ref, regex, override, buffer, or function are preserved as plain objects during rebuild. Added tests covering each of those cases.
There was a problem hiding this comment.
Moving options.literal above the single-key checks makes build return Buffer, RegExp and ref examples as plain objects. Joi.build(Joi.binary().example(Buffer.from('abc')).describe()) gives an object example, where master gives a Buffer.
There was a problem hiding this comment.
Confirmed and fixed in c0ef7e4. Special single-key descriptors are decoded before checking the literal flag again. I withdrew the follow-up that treated reserved descriptor keys as plain objects, keeping this PR scoped to examples containing type. New tests verify actual Buffer, RegExp and reference types, not just describe() equality. Full npm test: 1839 tests, 100% coverage, lint and types clean.
Fixes #2480.
Joi.build()treated every object with atypeproperty as a nested schema description. That is correct for keys, rules, and other schema terms, but examples are values. Rebuilding a schema whose example is{ type: 'some-type' }(or constructing the same spec by hand) threwTypeError: this.joi[desc.type] is not a function.This keeps example terms as literals during rebuild so
describe()/build()round-trips, matching the existing{ name: 'some-type' }case.Testing
example()plus adescribe()round-trip in the manifest builder tests.master(this.joi[desc.type] is not a function) and pass with this change.npm test— 1839 tests, 100% coverage, lint and types clean.