-
Notifications
You must be signed in to change notification settings - Fork 300
fix: anyOf compare alternatives #1037
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
Merged
msivasubramaniaan
merged 5 commits into
redhat-developer:main
from
jigx-com:fix/anyOf-compare-alternatives
Apr 25, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e214a0a
fix: anyOf compare alternatives
p-spacek fd8f729
Merge branch 'main' into fix/anyOf-compare-alternatives
p-spacek e259f8f
Merge branch 'main' into fix/anyOf-compare-alternatives
p-spacek ce91530
Merge branch 'main' into fix/anyOf-compare-alternatives
p-spacek 12e9e87
Merge branch 'main' into fix/anyOf-compare-alternatives
p-spacek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -537,6 +537,12 @@ export function findNodeAtOffset(node: ASTNode, offset: number, includeRightBoun | |
return undefined; | ||
} | ||
|
||
interface IValidationMatch { | ||
schema: JSONSchema; | ||
validationResult: ValidationResult; | ||
matchingSchemas: ISchemaCollector; | ||
} | ||
|
||
export class JSONDocument { | ||
public isKubernetes: boolean; | ||
public disableAdditionalProperties: boolean; | ||
|
@@ -870,7 +876,7 @@ function validate( | |
const val = getNodeValue(node); | ||
let enumValueMatch = false; | ||
for (const e of schema.enum) { | ||
if (val === e || (callFromAutoComplete && isString(val) && isString(e) && val && e.startsWith(val))) { | ||
if (val === e || isAutoCompleteEqualMaybe(callFromAutoComplete, node, val, e)) { | ||
enumValueMatch = true; | ||
break; | ||
} | ||
|
@@ -902,10 +908,7 @@ function validate( | |
|
||
if (isDefined(schema.const)) { | ||
const val = getNodeValue(node); | ||
if ( | ||
!equals(val, schema.const) && | ||
!(callFromAutoComplete && isString(val) && isString(schema.const) && schema.const.startsWith(val)) | ||
) { | ||
if (!equals(val, schema.const) && !isAutoCompleteEqualMaybe(callFromAutoComplete, node, val, schema.const)) { | ||
validationResult.problems.push({ | ||
location: { offset: node.offset, length: node.length }, | ||
severity: DiagnosticSeverity.Warning, | ||
|
@@ -1508,23 +1511,11 @@ function validate( | |
node: ASTNode, | ||
maxOneMatch, | ||
subValidationResult: ValidationResult, | ||
bestMatch: { | ||
schema: JSONSchema; | ||
validationResult: ValidationResult; | ||
matchingSchemas: ISchemaCollector; | ||
}, | ||
bestMatch: IValidationMatch, | ||
subSchema, | ||
subMatchingSchemas | ||
): { | ||
schema: JSONSchema; | ||
validationResult: ValidationResult; | ||
matchingSchemas: ISchemaCollector; | ||
} { | ||
if ( | ||
!maxOneMatch && | ||
!subValidationResult.hasProblems() && | ||
(!bestMatch.validationResult.hasProblems() || callFromAutoComplete) | ||
) { | ||
): IValidationMatch { | ||
if (!maxOneMatch && !subValidationResult.hasProblems() && !bestMatch.validationResult.hasProblems()) { | ||
// no errors, both are equally good matches | ||
bestMatch.matchingSchemas.merge(subMatchingSchemas); | ||
bestMatch.validationResult.propertiesMatches += subValidationResult.propertiesMatches; | ||
|
@@ -1545,19 +1536,30 @@ function validate( | |
validationResult: subValidationResult, | ||
matchingSchemas: subMatchingSchemas, | ||
}; | ||
} else if (compareResult === 0) { | ||
} else if ( | ||
compareResult === 0 || | ||
((node.value === null || node.type === 'null') && node.length === 0) // node with no value can match any schema potentially | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. null yaml value is a good match for sub-schemas, similar to |
||
) { | ||
// there's already a best matching but we are as good | ||
bestMatch.matchingSchemas.merge(subMatchingSchemas); | ||
bestMatch.validationResult.mergeEnumValues(subValidationResult); | ||
bestMatch.validationResult.mergeWarningGeneric(subValidationResult, [ | ||
ProblemType.missingRequiredPropWarning, | ||
ProblemType.typeMismatchWarning, | ||
ProblemType.constWarning, | ||
]); | ||
mergeValidationMatches(bestMatch, subMatchingSchemas, subValidationResult); | ||
} | ||
} | ||
return bestMatch; | ||
} | ||
|
||
function mergeValidationMatches( | ||
msivasubramaniaan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bestMatch: IValidationMatch, | ||
subMatchingSchemas: ISchemaCollector, | ||
subValidationResult: ValidationResult | ||
): void { | ||
bestMatch.matchingSchemas.merge(subMatchingSchemas); | ||
bestMatch.validationResult.mergeEnumValues(subValidationResult); | ||
bestMatch.validationResult.mergeWarningGeneric(subValidationResult, [ | ||
ProblemType.missingRequiredPropWarning, | ||
ProblemType.typeMismatchWarning, | ||
ProblemType.constWarning, | ||
]); | ||
} | ||
} | ||
|
||
function getSchemaSource(schema: JSONSchema, originalSchema: JSONSchema): string | undefined { | ||
|
@@ -1595,3 +1597,26 @@ function getSchemaUri(schema: JSONSchema, originalSchema: JSONSchema): string[] | |
function getWarningMessage(problemType: ProblemType, args: string[]): string { | ||
return localize(problemType, ProblemTypeMessages[problemType], args.join(' | ')); | ||
} | ||
|
||
/** | ||
* if callFromAutoComplete than compare value from yaml and value from schema (s.const | s.enum[i]) | ||
* allows partial match for autocompletion | ||
*/ | ||
function isAutoCompleteEqualMaybe( | ||
callFromAutoComplete: boolean, | ||
node: ASTNode, | ||
nodeValue: unknown, | ||
schemaValue: unknown | ||
): boolean { | ||
if (!callFromAutoComplete) { | ||
return false; | ||
} | ||
|
||
// if autocompletion property doesn't have value, then it could be a match | ||
const isWithoutValue = nodeValue === null && node.length === 0; // allows `prop: ` but ignore `prop: null` | ||
if (isWithoutValue) { | ||
return true; | ||
} | ||
|
||
return isString(nodeValue) && isString(schemaValue) && schemaValue.startsWith(nodeValue); | ||
} |
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
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.
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.
(!bestMatch.validationResult.hasProblems() || callFromAutoComplete)
didn't solve the previous problem properlyproblem was when the order of the subschemas in anyOf changed.
this previous fix allows 'issues' only on the previous bestMatch