-
Notifications
You must be signed in to change notification settings - Fork 0
fix: anyOf compare alternatives #32
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
base: jigx
Are you sure you want to change the base?
Changes from 1 commit
e214a0a
96feb3d
dffbff4
c1cab4d
fd8f729
e259f8f
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 |
---|---|---|
|
@@ -530,6 +530,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; | ||
|
@@ -863,7 +869,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; | ||
} | ||
|
@@ -894,10 +900,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, | ||
|
@@ -1498,23 +1501,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; | ||
|
@@ -1535,19 +1526,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 { | ||
|
@@ -1585,3 +1587,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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1289,7 +1289,7 @@ obj: | |
4, | ||
18, | ||
DiagnosticSeverity.Error, | ||
'yaml-schema: Package', | ||
'yaml-schema: Composer Package', | ||
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. test fix |
||
'https://raw.githubusercontent.com/composer/composer/master/res/composer-schema.json' | ||
) | ||
); | ||
|
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