fix: Allow setting type: "object"
when using Boolean JSON Schema combination keywords (allOf
, anyOf
, oneOf
, not
)
#280
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.
This PR removes
hasCombiningKeywords
, and instead always setstype
toattributes.type
. It also adds test cases for the expected behavior.I am using fastify fluent-json-schema for API validation. I noticed an issue where every time the API is started, my logs have multiple warnings related to missing schema properties.
These are coming from Ajv, which has the default option of logging issues that violate the rules for
strictTypes
.My schema responsible for violating
strictTypes
is like this (see theallOf inside parent
group inFluentSchema.integration.test.js
):I know I can silence these warnings if I pass
{ strictTypes: false }
as Ajv options when initializing fastify, but I wanted to see if I could fix the warning instead of modifying the default options.I found that the issue is due to the output from fluent-json-schema missing an expected
type: "object"
under theparent
property.However, I was unable to get it to show up -
S.object().allOf()
,S.raw({ schemaWithTypeObject })
, andS.allOf().raw({ type: "object" })
all were missing the expectedtype
field.This behavior happens due to explicitly setting type to
undefined
if there are any combining keywords.After looking through the commit history, it seems like this behavior has existed since beginning, aside from being moved around files.
I'm not sure what the original reason was for implementing it this way, but I believe that the current behavior should be changed, as combining keywords should be able to specify a type.
For example, the docs for json-schema have this:
And here are some examples from Ajv:
After looking into solving this issue, I found #233. It looks like previous attempt to fix it was closed (#237) which used type unions.
This PR is not trying to determine the types of everything inside of a schema combination; instead, it just fixes the current behavior of fluent-json-schema which prevents the creation of a valid schema, even if
S.raw
is used.As seen in the tests added to
FluentSchema.test.js,
any usage of an operator likeS.allOf()
will remain the same - the only time that type is included is if it is specified likeS.object().allOf()
. This allows the creation of schemas that pass Ajv strict mode, which requirestype: "object"
.While type is not a requirement for any of these boolean operators, it should still be an option if desired, instead of always being forced to be undefined.
Checklist
npm run test
andnpm run benchmark
and the Code of conduct
This PR closes #233