feat(functions): expand xor function and add or function #2765
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.
Replaces PR #2616 with addition of independent or function.
Fixes #2396 . This has now been merged with stoplightio:develop containing prior "xor" PR #2614.
Checklist
Does this PR introduce a breaking change?
(Just saw JeanArhancet's similar pull request, #2614 .)
FDX (see below) has extended XOR here to support from 1 to any number of properties. This would match the XOR logical function, that as long as exactly one of the (1 or more) properties is defined, then the rule passes. With just one property defined in the functionOptions, xor becomes redundant with defined function.
Updated error response messages to account for possibility of schema matching any number of properties or schema not matching any of any number of functionOption properties.
Updated functionOptions error messages to show example arrays containing from one to three ("and etc") properties.
Since then can take an array which implements AND-ing of rule requirements, this PR also defines and adds a new OR function. OR works just like xor for requiring at least one matching property, but then permits more than one matching property as valid and satisfying the rule.
Updated functions documentation for
xor
changes to allow more than two properties and newor
function.Additional context
The Financial Data Exchange (FDX, https://financialdataexchange.org) industry consortium for Open Banking delivers 13 OpenAPI 3.1 files with nearly 17,000 lines, 47 paths, 70 operations and 374 defined schemas, We enforce our FDX API Style Guide using 48 custom spectral rules and 23 overrides over 870 ruleset lines. We wanted to enforce that
type
was defined for each schema, but a schema can get its type in 3 (or 4) ways:type
,$ref
, oroneOf
. (We are not usingallOf
to definetype
.) This was ideal for an unresolved xor rule with propertiestype
,$ref
, oroneOf
. We initially implemented this as a custom function which met our needs, so are now contributing this to the spectral community. Our rule using our customxor
function:Secondly, there is an OR-rule opportunity to ensure that defined schemas contain sufficient documentation, as a style guide requirement. In particular, each schema ought to have its use / purpose described for users, which can be done in any of
title
,summary
, ordescription
fields. This is an ideal place for anor
function, since any one of those can provide the required schema documentation text, but two or three of them is also just as valid (or better!). A similar example (also included in this PR's tests), would be requiring a helpful example for atype: string
schema, which could be provided by any offormat
,example
, orpattern
fields in the API spec, but again more than one is acceptable.