-
Notifications
You must be signed in to change notification settings - Fork 277
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
fix: anyOf compare alternatives #1037
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -533,6 +533,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; | ||
|
@@ -866,7 +872,7 @@ function validate( | |
const val = getNodeValue(node); | ||
let enumValueMatch = false; | ||
for (const e of schema.enum) { | ||
if (equals(val, e) || (callFromAutoComplete && isString(val) && isString(e) && val && e.startsWith(val))) { | ||
if (equals(val, e) || isAutoCompleteEqualMaybe(callFromAutoComplete, node, val, e)) { | ||
enumValueMatch = true; | ||
break; | ||
} | ||
|
@@ -898,10 +904,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, | ||
|
@@ -1504,23 +1507,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) | ||
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.
|
||
) { | ||
): IValidationMatch { | ||
if (!maxOneMatch && !subValidationResult.hasProblems() && !bestMatch.validationResult.hasProblems()) { | ||
// no errors, both are equally good matches | ||
bestMatch.matchingSchemas.merge(subMatchingSchemas); | ||
bestMatch.validationResult.propertiesMatches += subValidationResult.propertiesMatches; | ||
|
@@ -1541,19 +1532,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( | ||
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. only refactor - move to separate function, no functional change |
||
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 { | ||
|
@@ -1591,3 +1593,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); | ||
} |
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.
null value is a good match in auto-completion