Document and allow extending strict schema maps#3116
Open
joshkel wants to merge 1 commit into
Open
Conversation
This uses a module-augmentable "type registry" interface that ObjectPropertiesSchema consults before falling back to its built-in chain. The trick is that the registry needs to map value types to schema types, not key names, so users key entries by an arbitrary tag and the library iterates the keys to find a matching type. Additional notes: 1. Ordering / precedence: Putting the custom lookup first lets users override built-ins but means a buggy augmentation can break the default mapping. Putting it last is safer but means users can't override (e.g.) Date. 2. Subtype collisions: If two registry entries' type fields overlap (e.g. one entry's type is object and another is Instant), the mapped-type iteration yields a union of both schemas. 3. null/undefined handling: The existing NullableType<...> checks accept T | null | undefined. This implementation strips them before comparing, matching the existing behavior. I did not see where the preexisting strict schema validation handling was documented, so I added a section covering that to the API docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I've found Joi's
object's strict schema checking useful to catch errors in TypeScript projects, but it has limitations when combined with Joi extensions: when used with custom classes (such as js-joda's Temporal classes likeInstant), Joi'sStrictSchemaMapexpects a genericJoi.ObjectSchema<Instant>instead of the schema objects returned by my extensions.To address that, this PR uses a module-augmentable "type registry" interface that ObjectPropertiesSchema consults before falling back to its built-in chain. The trick is that the registry needs to map value types to schema types, not key names, so users key entries by an arbitrary tag and the library iterates the keys to find a matching type.
Additional notes:
I did not see where the preexisting strict schema validation handling was documented, so I added a section covering that to the API docs.